HyukjinKwon opened a new pull request, #410: URL: https://github.com/apache/spark-connect-rust/pull/410
### What changes were proposed in this pull request? [SPARK-61208](https://issues.apache.org/jira/browse/SPARK-61208): make a removal of a non-last listener wait for the synchronous started event in delivery, as PySpark does. `start()` and `toTable()` deliver the query-started event to the listeners on the calling thread before they return. The asynchronous event loop already marked its delivery, so `removeListener` from another thread waited for it. The synchronous started delivery was not marked, so such a removal returned at once, and the removed listener still received the started event afterwards. This change: - Marks the started delivery of `start`/`toTable` as in progress (a per-delivery guard recording the delivering thread), taken before the listener snapshot is read, in the Rust core (`DataStreamWriter::_start_internal`). - A removal of a non-last listener from another thread waits until that delivery finishes. A removal from the delivering thread itself (a callback removing a listener) does not wait, so it cannot deadlock. Official PySpark 4.3.0 holds the listener bus lock for the started event as well, so its `removeListener` waits and the removed listener has the event before the call returns. Reproduced with a started callback that sleeps 1 s on one listener, while another thread removes the second listener at 0.3 s: before this change the removal returned at 0.31 s with no started event for the removed listener, and that listener got the event at 1.01 s; now it returns at 1.01 s, after the event, as official PySpark does. ### Why are the changes needed? A removal from another thread could return while a started callback was still running, and the removed listener then received the started event after `removeListener` returned. This breaks the guarantee that a removed listener gets no more events, which the asynchronous path already provides. ### Does this PR introduce _any_ user-facing change? Yes. `removeListener` (and the Rust `remove_listener`) of a non-last listener from another thread now waits for a query-started event that `start()`/`toTable()` is delivering to finish. A removal from inside that callback does not wait. ### How was this patch tested? - `test_streaming_listener_start_removal_waits_for_the_started_event_in_delivery` (Python, mock service, ordered by events and gates): a removal from another thread waits for the started event in delivery and the removed listener receives it before the removal returns; a removal from the started callback returns without waiting. The first part fails without this change and passes with it. - `nonlast_removal_waits_for_a_started_event_in_delivery_from_another_thread` and `nonlast_removal_from_the_started_event_thread_does_not_wait_for_itself` (Rust unit tests). - A live check against a Spark Connect server with a native Rust listener and `start()` gave the same ordering (the removal waited for the started event to reach both listeners). - The same start-removal case matches official PySpark 4.3.0 in timing and delivery. - The lib tests of both crates (660 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2808 passed, 8 skipped), and the live drop-in and Arrow suites (224 passed) and live cases (13 passed) against a local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 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]
