gaogaotiantian opened a new pull request, #60259: URL: https://github.com/apache/spark/pull/60259
### What changes were proposed in this pull request? In classic PySpark, the driver's accumulator server thread merges worker profile results into the `SQL_UDF_PROFIER_V2` accumulator through `_ProfileResultsParamV2.addInPlace`, with no lock. `AccumulatorProfilerCollector` returns that same live dict, and `spark.profile.clear()` (`clear_perf_profiles` / `clear_memory_profiles` / `clear_sampling_profiles`) pops entries from it while holding only the collector's own lock, which the merge never takes. This PR adds one module-level `RLock` in `pyspark/sql/profiler.py`: - `_ProfileResultsParamV2.addInPlace` holds it for the whole merge. - `AccumulatorProfilerCollector` uses it as its `_lock`, so its reads and clear methods take the same lock as the merge. The lock is module-level rather than an attribute of the param, so the `AccumulatorParam` stays picklable. It is held only for the in-memory dict merge or clear, with no JVM calls or job waits under it. Merge results and `spark.profile.clear` behavior do not change. Spark Connect is not affected, because `ConnectProfilerCollector._update` already merges under the collector's lock. It keeps its own per-collector lock. The nested acquisition (collector lock, then the module lock inside `addInPlace`) always happens in the same order, so it can't deadlock. The legacy `_ProfileResultsParam` (V1) is not changed. It builds a new dict instead of mutating `value1`, it is only used on workers, and the driver never registers the V1 accumulator id, so the accumulator server drops V1 updates and nothing clears them. ### Why are the changes needed? If a clear lands between the merge's `key in value1` check and its write-back, the merge raises `KeyError` in the accumulator server thread. In the other ordering, a cleared entry can be re-attached. Perf and memory results already share a key across tasks (the UDF `result_id`), so this can happen on master today. It was found during review of #60150, which lets the sampling profiler share keys too: https://github.com/apache/spark/pull/60150#discussion_r4488925635 ### Does this PR introduce _any_ user-facing change? No, other than fixing the race. A `spark.profile.clear()` that runs while profiles are being merged no longer makes the merge fail. ### How was this patch tested? Added `UDFProfiler2Tests.test_clear_during_accumulator_merge`. It runs no Spark job. It does the server's `accumulator += update` with a shared key, pauses that merge midway with a patched `SamplingResultParam.addInPlace`, and calls `spark.profile.clear()` from another thread at that point. Then it checks that no exception escapes and that no results are left. The interleaving is deterministic: without the fix the test failed with `KeyError: 'id'` in 5 of 5 runs, and with it it passed 5 of 5. Its pause is at most 0.1s. Also ran `pyspark.sql.tests.test_udf_profiler` (all classes) and `pyspark.sql.tests.connect.test_parity_udf_profiler`, and both pass. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Isaac This pull request and its description were written by Isaac. -- 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]
