viirya opened a new pull request, #509: URL: https://github.com/apache/spark-connect-rust/pull/509
### What changes were proposed in this pull request? The client-side streaming listener bus (`crates/spark-connect/src/streaming.rs`) keeps its state behind one mutex. This PR makes that lock poison-tolerant and moves the last code of the user out from under it. - The lock is taken only through `ListenerBus::lock` and `ListenerBus::wait`, which recover a poisoned guard with `PoisonError::into_inner`. They replace the about 55 `lock().unwrap()` calls of the file, three of which were in the `Drop` of `StartedDispatch`, `Dispatch` and `FinishGeneration`. - The listeners that leave the bus are dropped after the lock is released, one by one, and a panic of their `Drop` is reported and isolated as a panic of a callback is (`drop_isolated`). This covers the removal of a listener that is not the last one, the end of the generation of a worker, the end of a removal of the last listener and the teardown of the session. The snapshot of a delivery is dropped the same way. - The worker thread is spawned with `std::thread::Builder`, and the event stream is handed to it after the spawn. A thread that cannot be spawned is an error of `add_listener`; the bus records that the server still holds its listener, so that the next add removes it first, and the stream is dropped without the lock. - The locking rule of the bus, and the two groups of the fields of `ListenerBusState`, are documented on the structs. The fields stay behind one lock: the worker marks its delivery and takes its snapshot of the listeners in one critical section, which is what lets a removal either precede the snapshot or see the delivery. Listener callbacks, RPCs and joins of the worker already ran without the lock, and that is unchanged. The bus takes no other lock while it holds its own, so there is no lock order to break. ### Why are the changes needed? A panic of any thread that held the lock poisoned it. Every later call on the bus of the session then panicked, and a guard dropped by an unwinding panic panicked again, which aborts the process. Two things could panic under the lock, or run the code of a user there: - The `Drop` of a listener, when the bus removed the last reference to it. A `Drop` that called back into the bus, such as `listeners()`, also deadlocked on the lock held by its own thread. - `std::thread::spawn`, which panics when the thread cannot be created, and then dropped the event stream under the lock. That drop runs the progress handlers of the session. ### Does this PR introduce _any_ user-facing change? Only on failure paths. A session whose listener bus saw a panic keeps working instead of panicking on every later listener call or aborting, and `add_listener` returns an error instead of panicking when the worker thread cannot be created. ### How was this patch tested? Four unit tests in `streaming.rs`. They are unit tests because `add_listener` needs the acknowledgement of a server, so an integration test cannot register a listener offline. - `a_panicking_callback_does_not_break_later_add_remove_and_dispatch`: callbacks that panic on the started event, on events of the bus and on its terminal error leave the lock unpoisoned; later deliveries, removals and adds behave as without the panic, and the bus is dropped with its session. - `a_poisoned_state_lock_does_not_fail_later_calls_on_the_bus`: with a lock poisoned on purpose, `listeners`, `post_to_all`, `remove_listener_found`, the started-event guard, the worker loop, `add_listener`, `close` and `cancel_listener_bus` all still work. - `the_guards_of_the_bus_do_not_abort_when_a_panic_unwinds_them_with_a_poisoned_lock`: a started-event handler that panics and an event stream that panics unwind the guards with a poisoned lock; the process survives and the state is cleaned up. - `a_listener_removed_from_the_bus_is_dropped_without_the_state_lock`: a listener whose `Drop` checks the lock and then panics is dropped unlocked on each of the four removal paths, and the lock is not poisoned. Run locally: - `cargo test -p apache-spark-connect --release --lib`: 603 passed, 0 failed. - `cargo test -p apache-spark-connect --release --no-run` and the same with `--features wasm-udf` compile. - `cargo fmt --all --check` passes. - `cargo clippy -p apache-spark-connect --lib --tests` reports nothing on the changed lines. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (claude-fable-5-2) 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]
