dongjoon-hyun commented on code in PR #60259:
URL: https://github.com/apache/spark/pull/60259#discussion_r4519126861


##########
python/pyspark/sql/tests/test_udf_profiler.py:
##########
@@ -829,6 +831,32 @@ def setUp(self) -> None:
         super().setUp()
         self.spark._profiler_collector._accumulator._value = {}
 
+    def test_clear_during_accumulator_merge(self):
+        # The accumulator server thread merges worker updates into the same 
dict that
+        # spark.profile.clear() pops from. Pause a merge midway and clear from 
another thread.
+        accumulator = self.spark._profiler_collector._accumulator
+        merging, cleared = threading.Event(), threading.Event()
+        add_in_place = SamplingResultParam.addInPlace
+
+        def paused_add_in_place(value1, value2):
+            merging.set()
+            # Without a shared lock, the clear runs while the merge is waiting 
here.
+            cleared.wait(timeout=0.1)
+            return add_in_place(value1, value2)
+
+        def clear():
+            merging.wait()
+            self.spark.profile.clear()
+            cleared.set()
+
+        accumulator += {"id": {"sampling": {"count": 1}}}
+        with mock.patch.object(SamplingResultParam, "addInPlace", 
paused_add_in_place):
+            thread = threading.Thread(target=clear)

Review Comment:
   nit: If the patched `addInPlace` is never reached (e.g. the merge raises 
earlier or a future change skips this branch), `merging.wait()` blocks forever, 
and this non-daemon thread hangs `thread.join()` or the interpreter shutdown 
instead of failing the test. How about `threading.Thread(target=clear, 
daemon=True)` with a timeout on `merging.wait(...)`?



##########
python/pyspark/sql/profiler.py:
##########
@@ -92,6 +92,12 @@ def addInPlace(
 ProfileResultsParam = _ProfileResultsParam()
 
 
+# Guards the profile results map on the driver. The accumulator server thread 
merges worker
+# updates via _ProfileResultsParamV2.addInPlace while user threads read or 
clear the same dict
+# through AccumulatorProfilerCollector. It is module-level so the param stays 
picklable.
+_profile_results_lock = RLock()

Review Comment:
   nit: Could we document the constraint of this lock here? The accumulator 
server thread takes it while the DAGScheduler is waiting for its 
acknowledgement, so holders must keep it short and must not call into the JVM 
or wait on a Spark job while holding it. It is in the PR description, but a 
code comment would protect future changes to the collectors.



##########
python/pyspark/sql/profiler.py:
##########
@@ -569,6 +576,8 @@ def clear_sampling_profiles(self, id: Optional[Union[int, 
str]] = None) -> None:
 class AccumulatorProfilerCollector(ProfilerCollector):
     def __init__(self) -> None:
         super().__init__()
+        # Share the lock with the accumulator merge, which mutates the same 
dict.
+        self._lock = _profile_results_lock

Review Comment:
   Note that the shared lock protects only the top-level map for the reads. 
`show_perf_profiles`, `dump_perf_profiles` and `Profile.render` still call 
`sort_stats` / `print_stats` / `dump_stats` on the live `pstats.Stats` objects 
outside the lock, while a concurrent merge mutates them in place via 
`PStatsParam.addInPlace` -> `Stats.add` (e.g. `RuntimeError: dictionary changed 
size during iteration`). This is a pre-existing issue and out of scope here, so 
could you clarify `its reads ... take the same lock` in the PR description, and 
consider a follow-up which copies the entries under the lock and formats them 
outside it? It would be good to settle this before the sampling show/render of 
#60150, because `_sampling_profile_results` also returns the live nested dicts.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to