HyukjinKwon opened a new pull request, #394:
URL: https://github.com/apache/spark-connect-rust/pull/394

   ### What changes were proposed in this pull request?
   
   [SPARK-61208](https://issues.apache.org/jira/browse/SPARK-61208): PySpark's 
`StreamingQueryListenerBus` delivers each event to all its listeners in 
`post_to_all`, while it holds `_lock`, and `remove` takes the same lock. So 
`removeListener` returns only after the event in delivery has reached every 
listener, also the removed one, and the removed listener gets no event after 
that.
   
   The bus of the Rust client (`crates/spark-connect/src/streaming.rs`) takes a 
snapshot of its listeners for each event and delivers it without its lock, and 
it removes a listener that is not the last at once. Now:
   
   - The worker records which generation it is delivering an event for 
(`ListenerBusState::dispatching`), and a guard clears that when the delivery 
ends, also on a panic, and wakes the waiters.
   - A removal of a listener that is not the last, from another thread than the 
worker, waits for the delivery in progress before it returns, as PySpark's lock 
makes it wait.
   - A removal from a callback of the bus does not wait, as it would wait for 
its own delivery. PySpark's `_lock` is not reentrant, so PySpark hangs there.
   - The removal of the last listener, and `close`, already wait for the worker 
to end.
   
   ### Why are the changes needed?
   
   When a listener was removed while another listener's callback ran, the 
removed listener got the event after `removeListener` had returned. Listener A 
is busy in its first progress callback, and another thread removes listener B:
   
   ```
   official 4.3.0: A progress (busy), B progress, removeListener(B) returned
   before:         A progress (busy), removeListener(B) returned, B progress
   after:          A progress (busy), B progress, removeListener(B) returned
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `removeListener` of a listener that is not the last, called while an 
event is being delivered, returns after the delivery, so the removed listener 
gets no event after it returns. The call can now wait for a callback of another 
listener, as it does in PySpark.
   
   ### How was this patch tested?
   
   - `test_streaming_listener_removal_waits_for_the_event_in_delivery` in 
`python/tests/streaming_listener_cases.py`, against the mock server's listener 
bus:
     - The first listener blocks in its callback for an event, and another 
thread removes the second listener.
     - The removal does not return while the first listener blocks.
     - Once the first listener is released, the second listener gets the event 
and then the removal returns.
     - A later event reaches only the first listener.
     - The test fails without the change, where the removal returns at once.
   - The probe above, against a Spark 4.3.0 Connect server: the order of this 
build is the order of official PySpark 4.3.0.
   - The offline streaming listener tests (`-k StreamingListener`, 28 passed).
   - The lib tests of both crates (636 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites (2610 passed, 8 skipped), and the 
live drop-in and Arrow suites (223 passed) and live cases (12 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]

Reply via email to