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

   ### What changes were proposed in this pull request?
   
   A-1 of the review of #535 (SPARK-61470) and #552, which #564 left out.
   
   PySpark's `StreamingQueryListenerBus.remove` acquires the lock of the bus 
before it sends the REMOVE of the last listener (`query.py:320`). Its 
`post_to_all` holds that lock while it calls the listeners (`:415`). So while a 
callback runs:
   - PySpark's `removeListener` of the last listener, and `streams.close()`, 
wait for the callback.
   - A Ctrl-C interrupts the acquire: `KeyboardInterrupt` at once, no REMOVE, 
and the listener stays registered and keeps getting events.
   
   The Python client sent the REMOVE at once while the callback ran, then 
waited for the worker in the join, which #558 made abortable. After a Ctrl-C 
the listener stayed in the bus, but the server's listener was removed, so it 
got no more events.
   
   `remove_listeners` (`crates/spark-connect/src/streaming.rs`) now makes a 
removal of the last listener from a thread other than the worker wait for the 
delivery of the worker before it removes the server's listener, as the removal 
of another listener already does. The bus is Stopping during that wait, as for 
a removal in flight. So a callback of that delivery keeps the semantics this 
client gives it, where PySpark deadlocks on its non-reentrant lock:
   - a removal or a close coalesces with the waiting removal, which owns the 
result;
   - an add is refused with "cannot change the listener bus from its own 
callback";
   - a stop of the session ends the subscription with the session, which leaves 
the removal nothing to remove.
   
   A signal ends the wait in the Python client before anything is removed or 
sent.
   
   ### Why are the changes needed?
   
   To match PySpark 4.3.0. From the review's probe against a gRPC stub, with 
`onQueryIdle` sleeping 8 s and a SIGINT at 0.5 s during `removeListener`:
   - PySpark raised `KeyboardInterrupt` after 0.51 s, kept the listener, and 
sent no REMOVE.
   - Ours, with #558, raised at once and kept the listener, but had sent the 
REMOVE.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the Python client. A removal of the last listener, or 
`streams.close()`, from another thread waits for a callback in flight before it 
removes the server's listener. A Ctrl-C during that wait leaves the listener 
registered on the server too.
   
   ### How was this patch tested?
   
   - `test_streaming_listener_callback_lifecycle_calls_during_external_close`, 
7 cases, now asserts PySpark's order:
     - no REMOVE while the callback runs;
     - the callback's remove coalesces and its add is refused, as before;
     - the REMOVE comes after the callback returns.
     - With a stop from the callback, no REMOVE is sent, and no removal warning.
   - #558's 
`test_sigint_during_the_join_of_a_busy_listener_worker_raises_keyboard_interrupt`
 becomes 
`test_sigint_during_the_removal_of_a_busy_listener_raises_keyboard_interrupt`: 
no REMOVE at the signal, one at the later removal.
   - All 9 fail on master.
   - All 73 listener tests of `test_dropin_offline.py` pass.
   - `cargo test -p apache-spark-connect --release --lib` (629 passed), `cargo 
build -p pyspark-rs --release`, `cargo fmt --all --check` and `ruff check`.
   
   ### 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