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

   ### What changes were proposed in this pull request?
   
   [SPARK-61481](https://issues.apache.org/jira/browse/SPARK-61481): Two fixes 
to how signals end the waits of `SparkSession.stop()` and of the streaming 
listener bus. Both are in the Rust API (`crates/spark-connect`), so the Python 
client gets them too.
   
   1. `SparkSession::stop` (`crates/spark-connect/src/session.rs`) now runs 
under a lock of the session that its clones share (`StopLock`). PySpark's 
`stop()` does the same under `SparkSession._lock`, an `RLock` 
(`pyspark/sql/connect/session.py:122, 948`).
      - A `stop()` on another thread waits for the one in progress. If a signal 
interrupts that one before it closes the session, the waiting `stop()` finds 
the session open, releases it and closes it. PySpark's second `stop()` does 
this once the first one's `KeyboardInterrupt` leaves the lock.
      - The wait goes through `block_on_abortable`, so in the Python client a 
signal ends it, as a signal interrupts the acquire of PySpark's lock on POSIX. 
The Rust API installs no signal watch, so it waits as for any lock.
      - The lock is reentrant, as an `RLock` is. A `stop()` on the thread that 
holds it runs inside the first one instead of deadlocking. That happens when a 
signal handler calls `spark.stop()` while the release of `stop()` waits.
      - `stopped` is now set once the release has ended (still before the 
listener bus is cancelled), not when `stop()` starts. So `is_stopped` is false 
while the release is in flight. PySpark's `is_stopped` is `client.is_closed` 
(`session.py:1048`), which its `stop()` sets after the release. An interrupted 
`stop()` no longer has to reset the flag.
      - While the lock is held, the following run: the release RPC (in the 
Python client, also the signal handlers that the wait's polls run), 
`cancel_listener_bus` (a short lock of the bus, no join) and `client.close()` 
(a flag). None of them waits for another thread's `stop()`.
      - The drop-in's resets run after the core's `stop()` returns, with the 
GIL: `forget_session`, the default and active session, and the cached 
SQLContext. They are idempotent, so a second `stop()` that finds the session 
stopped runs them again, as PySpark's second `stop()` does.
      - `close()` (`client.close()`, and `stop()` with 
`release_session_on_close = False`) still takes no lock. It closes at once, as 
PySpark's `client.close()` does.
   
   2. The listener bus (`crates/spark-connect/src/streaming.rs`) waited on a 
`std::sync::Condvar`, which no signal can end. It now wakes its waits with a 
`tokio::sync::Notify` (`BusChanged`, which keeps the existing `notify_all` 
calls). `ListenerBus::wait` waits on it through `block_on_abortable`, then 
locks the state again through `ListenerBus::lock`, so it stays poison-tolerant 
as SPARK-61441 made it. Without a watch (the Rust API) it waits for the change 
as before. In the Python client a signal ends it with an error. PySpark's bus 
does these waits by acquiring its `threading.Lock` (`with self._lock` in 
`append` and `remove`, `pyspark/sql/connect/streaming/query.py:289, 320`). 
`post_to_all` holds that lock while it delivers an event (`query.py:415`). The 
three waits are:
      - the removal of the last listener, and `close()`, waiting for a started 
event that another thread is delivering (the wait of the finding);
      - the removal of one of several listeners, waiting for the deliveries in 
flight;
      - a removal or an add, waiting for another thread's add or last removal 
(`idle_state`).
   
      An interrupted wait leaves the bus as it was:
      - The first and the third wait happen before anything changes.
      - The second removes the listener before it waits. It now puts the 
listener back at its place, unless its subscription ended meanwhile (for 
example, its worker failed and cleared the bus); then the listener is dropped 
after the lock is released, as the bus drops the listeners that leave it 
(`drop_isolated`).
   
      The drop-in's `removeListener`, `addListener` and `streams.close()` then 
raise the `KeyboardInterrupt`, and the listener stays registered, as after 
PySpark's interrupted `with self._lock`.
   
   ### Why are the changes needed?
   
   - Concurrent `stop()`:
     - Core `stop()` set `stopped` before the release, so a `stop()` on another 
thread returned at once and its caller was told that the session was stopped.
     - When a signal then interrupted the first `stop()`, it reset `stopped`, 
and the session stayed open.
     - Probe on the repo's mock server (`stop_probe.py two_stops`): the server 
holds ReleaseSession, the main thread calls `stop()`, another thread calls 
`stop()`, then a SIGINT. Official PySpark 4.3.0 prints:
       ```
       main KeyboardInterrupt after 0.4s
       other before the signal: still waiting
       release calls=2 (second reached: True); is_stopped while held=False; 
other alive=True
       other: returned after 0.5s, is_stopped=True; is_stopped=True; release 
calls=2
       ```
     - With this change, the Python client prints the same four lines.
     - Before it, the second `stop()` returned at once and only one 
ReleaseSession was sent. The new test fails with `assert 1 == 2` on the release 
count.
   - A `stop()` from a signal handler (`reentrant_probe.py`): a SIGINT handler 
calls `spark.stop()` while the release of `stop()` is held.
     - Official PySpark 4.3.0 and the Python client with this change behave the 
same: the handler sees `is_stopped` False, its `stop()` sends a second 
ReleaseSession and leaves the session stopped, and the first `stop()` returns 
after 0.2 s.
     - Before the change, the handler saw `is_stopped` True, and its `stop()` 
returned without releasing the session.
   - The listener bus:
     - PySpark 4.3.0's `StreamingQueryListenerBus` was probed directly, without 
a server (`official_bus_probe.py`): a started event in delivery on another 
thread, and `remove` of the last listener on the main thread. It raises 
KeyboardInterrupt 0.3 s after the SIGINT and keeps the listener.
     - The official client cannot add a listener on the mock server: it checks 
the session ID of the listener-bus responses, which the mock leaves empty. So 
the bus was probed directly.
     - Before the change, the Python client's `removeListener` ignored the 
signal and returned only once `onQueryStarted` returned (5 s in the test).
     - With the change, it raises KeyboardInterrupt after 0.4 s, sends no 
REMOVE and keeps the listener. A later `removeListener` removes it and sends 
the REMOVE.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   - A `stop()` of a session (or of a clone) waits while another thread's 
`stop()` of it is in progress. If that one is interrupted, the waiting `stop()` 
releases and closes the session itself. Before, it returned at once.
   - `is_stopped` is false while the release of `stop()` is in flight. It was 
true.
   - In the Python client, Ctrl-C raises `KeyboardInterrupt` from:
     - a `stop()` that waits for another thread's `stop()`;
     - `removeListener`, `addListener` and `streams.close()` while they wait 
for another thread's delivery or listener add/removal. The listener stays 
registered.
   - In the Rust API, which installs no signal watch, the waits are unchanged.
   
   ### How was this patch tested?
   
   New tests. Each fails with the source changes reverted by hand (keeping the 
test code), and passes with them:
   - Python, offline, in `python/tests/test_dropin_offline.py`, after 
`test_sigint_during_the_release_of_stop_raises_keyboard_interrupt_and_keeps_the_session`,
 on the mock server that holds ReleaseSession:
     - 
`test_a_stop_that_waits_for_an_interrupted_stop_releases_the_session_as_pyspark`:
 two threads stop one session and a SIGINT ends the first `stop()`. The second 
sends the second ReleaseSession, and `is_stopped` stays false until that 
release ends. Without the fix: `assert 1 == 2` (one ReleaseSession). With it: 
0.42 s.
     - 
`test_a_stop_from_a_signal_handler_while_stop_releases_runs_within_it_as_pyspark`:
 a SIGINT handler stops the session while the release is held. Its `stop()` 
runs inside the first one, releases and closes. Without the fix: `assert [True, 
True] == [False, True]`. With it: 0.23 s.
     - 
`test_sigint_during_the_removal_of_the_last_listener_raises_keyboard_interrupt_as_pyspark`:
 `onQueryStarted` blocks on another thread while the main thread calls 
`removeListener` of the last listener. A SIGINT raises KeyboardInterrupt within 
3 s, the listener stays and no REMOVE is sent; a later removal sends it. 
Without the fix, `removeListener` returned after 5.0 s. With it: 0.41 s.
   - Rust:
     - 
`session::tests::a_stop_that_waits_for_an_interrupted_stop_releases_the_session_as_pyspark`,
 after 
`release_session_of_a_stopped_session_raises_no_active_session_as_pyspark`. A 
local port that accepts connections and never answers holds the releases, and a 
watch that aborts ends the first `stop()`. The second `stop()` waits, then 
releases, and the session is not stopped until that release ends. Without the 
fix it fails with "a stop returned while another one released the session".
     - 
`streaming::tests::an_interrupted_wait_of_a_removal_leaves_the_bus_as_it_was_as_pyspark`,
 after `the_listener_event_stream_reports_no_progress_to_the_handlers`. It 
covers three cases: the last listener, one of two, and an add in progress on 
another thread. After the interrupted wait the bus is unchanged, and a later 
removal goes through. Without the fix it fails with "the watch ends the wait of 
the removal: Timeout".
     - Both pass in 1.2 s.
   - Commands and results (release builds, on top of SPARK-61441's 
poison-tolerant bus lock):
     - `cargo test -p apache-spark-connect-core --release --lib`: 197 passed, 
including the 16 `runtime::` tests.
     - `cargo test -p apache-spark-connect --release --lib`: 610 passed, 
including SPARK-61441's four bus tests 
(`a_panicking_callback_does_not_break_later_add_remove_and_dispatch`, 
`a_poisoned_state_lock_does_not_fail_later_calls_on_the_bus`, 
`the_guards_of_the_bus_do_not_abort_when_a_panic_unwinds_them_with_a_poisoned_lock`,
 `a_listener_removed_from_the_bus_is_dropped_without_the_state_lock`).
     - `cargo test -p apache-spark-connect --release --no-run`, and the same 
with `--features wasm-udf`: built without warnings.
     - `cargo fmt --all --check`: clean.
     - `cargo clippy -p apache-spark-connect --release --all-targets` and 
`cargo clippy -p pyspark-rs --release --all-targets`: no diagnostics on the 
changed lines.
     - `ruff check python/tests/test_dropin_offline.py`: passed.
     - Offline Python, `pytest tests/test_dropin_offline.py -k "sigint or stop 
or listener or streaming or interrupt or signal or Streaming"`: 96 passed. This 
includes the listener lifecycle cases of `streaming_listener_cases.py`, with 
SPARK-61443's three new ones.
     - Live, against a local Spark Connect 4.3.0 server 
(`SPARK_REMOTE=sc://localhost:15014`):
       - `pytest tests/test_dropin_e2e.py -k "listener or stop or stream or 
interrupt or sigint or Streaming"`: 15 passed.
       - `cargo test -p apache-spark-connect --release --test e2e_streaming`: 5 
passed.
       - `--test e2e_new_methods session_stop`: 1 passed.
       - `--test e2e_integration`: 20 passed.
     - Official `pyspark/sql/tests/connect/streaming/test_parity_listener.py` 
of 4.3.0, run on the Python client against the same server: 9 passed.
   
   ### 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