This is an automated email from the ASF dual-hosted git repository.
HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git
The following commit(s) were added to refs/heads/master by this push:
new abc63391 [SPARK-61540][CONNECT][PYTHON] Let the removal of the last
streaming query listener wait for the delivery in flight before it removes the
server's listener, as PySpark does
abc63391 is described below
commit abc633914fb416149c72eaa1c85b34976f25c95a
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Thu Nov 12 21:13:55 2026 +0900
[SPARK-61540][CONNECT][PYTHON] Let the removal of the last streaming query
listener wait for the delivery in flight before it removes the server's
listener, as PySpark does
### 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.
Closes #569 from HyukjinKwon/SPARK-61540-listener-remove-wait.
Authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
crates/spark-connect/src/streaming.rs | 39 +++++++++++++++++++++++++++-----
python/tests/streaming_listener_cases.py | 18 +++++++++++----
python/tests/test_dropin_offline.py | 13 ++++++-----
3 files changed, 53 insertions(+), 17 deletions(-)
diff --git a/crates/spark-connect/src/streaming.rs
b/crates/spark-connect/src/streaming.rs
index a278d8ca..0010d5bf 100644
--- a/crates/spark-connect/src/streaming.rs
+++ b/crates/spark-connect/src/streaming.rs
@@ -1561,7 +1561,9 @@ impl StreamingQueryManager {
}
/// Remove a listener by id. Unknown ids do nothing. Removing the last
listener drains
- /// the server event stream before returning, as PySpark's removeListener
does.
+ /// the server event stream before returning, as PySpark's removeListener
does. A removal
+ /// from another thread than the worker first waits for the delivery in
flight, as PySpark's
+ /// removal waits for the lock that its bus holds while it calls the
listeners.
/// A callback during another caller's shutdown returns without waiting
for its own
/// worker; that caller owns the final result and may retry a failed
removal.
/// In the Python client, a signal ends the waits of the removal for the
deliveries and the
@@ -1644,11 +1646,9 @@ impl StreamingQueryManager {
}
// The removal of the last listener waits for a started event that
another thread
// delivers, before it removes the server's listener, as PySpark's
bus holds its lock
- // for that delivery. A worker's own event is not awaited: its
callbacks may remove
- // or stop the bus while the removal is in flight. The lock is
released while
- // waiting, so the bus is checked again after it. A signal ends
the wait in the
- // Python client, as it interrupts the acquire of the lock of
PySpark's bus, before
- // anything is removed.
+ // for that delivery. The lock is released while waiting, so the
bus is checked again
+ // after it. A signal ends the wait in the Python client, as it
interrupts the acquire
+ // of the lock of PySpark's bus, before anything is removed.
let current = std::thread::current().id();
if state.worker_id != Some(current)
&& state
@@ -1659,6 +1659,33 @@ impl StreamingQueryManager {
drop(self.bus.wait(state)?);
continue;
}
+ // It waits for the delivery of the worker too, as PySpark's
removal waits for the
+ // lock that its post_to_all holds while it calls the listeners,
before it sends the
+ // REMOVE. The bus is Stopping meanwhile, as for a removal in
flight, so that a callback
+ // of the delivery that removes a listener or closes the bus
coalesces with this
+ // removal, which owns the result, and one that adds a listener is
refused. A signal
+ // ends the wait in the Python client before anything is removed
or sent.
+ if state.worker_id != Some(current) && state.dispatching ==
Some(state.generation) {
+ let generation = state.generation;
+ state.phase = ListenerBusPhase::Stopping;
+ while state.generation == generation
+ && (state.dispatching == Some(generation)
+ || state
+ .started_dispatches
+ .iter()
+ .any(|thread| *thread != current))
+ {
+ state = match self.bus.wait(state) {
+ Ok(state) => state,
+ Err(interrupted) => {
+ let mut state = self.bus.lock();
+ state.phase = ListenerBusPhase::Idle;
+ self.bus.changed.notify_all();
+ return Err(interrupted);
+ }
+ };
+ }
+ }
return self.stop_listener_bus(state);
}
}
diff --git a/python/tests/streaming_listener_cases.py
b/python/tests/streaming_listener_cases.py
index 1b0c084c..7141841c 100644
--- a/python/tests/streaming_listener_cases.py
+++ b/python/tests/streaming_listener_cases.py
@@ -956,15 +956,21 @@ def concurrent_close_case(remove_failure,
callback_action):
assert entered.wait(5)
closing.start()
try:
- assert service.wait_listener_removal()
+ # As PySpark's remove, which waits for the lock that its
post_to_all holds
+ # while the listeners are called: the close sends no
REMOVE while the
+ # callback runs.
+ assert not service.wait_listener_removal(timeout=1)
+ assert closing.is_alive()
release.set()
- # The RPC stays gated until the callback's remove/stop has
returned.
+ # The callback's remove coalesces with the close, and its
add is refused.
assert removed.wait(5)
assert len(callback_threads) == registrations
assert all(thread not in (caller_thread, closing.ident)
for thread in callback_threads)
- assert closing.is_alive()
if callback_action != "stop":
+ # Then the close sends its REMOVE, which stays gated.
+ assert service.wait_listener_removal()
+ assert closing.is_alive()
# The callback's own removal did not consume a
registration.
assert spark.streams._sqlb._listener_bus == [listener]
* registrations
finally:
@@ -986,16 +992,18 @@ def concurrent_close_case(remove_failure,
callback_action):
assert attempted.timestamps == []
else:
assert add_warnings == []
- if remove_failure:
+ if remove_failure and callback_action != "stop":
assert len(close_warnings) == 1
assert "listener removal failed" in close_warnings[0]
else:
assert close_warnings == []
if callback_action == "stop":
+ # The stop of the callback ended the subscription, with the
session, before the
+ # close sent a REMOVE, so the close found nothing left to
remove.
assert spark.is_stopped
assert spark.streams._sqlb._listener_bus == []
assert service.release_session_calls == 1
- assert service.listener_removals == 1
+ assert service.listener_removals == 0
assert service.wait_listener_streams_finished(1)
return
if remove_failure:
diff --git a/python/tests/test_dropin_offline.py
b/python/tests/test_dropin_offline.py
index 06019aa0..3d23550c 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -20146,14 +20146,14 @@ def _blocking_idle_listener(entered, release):
@pytest.mark.parametrize("interrupted_call", ["removeListener", "close"])
-def
test_sigint_during_the_join_of_a_busy_listener_worker_raises_keyboard_interrupt(
+def
test_sigint_during_the_removal_of_a_busy_listener_raises_keyboard_interrupt(
mock_connect,
interrupted_call,
):
- # As PySpark's remove, which sends the REMOVE and then joins the thread of
the listener, a
- # thread that the join waits for on a callback that does not return: a
signal raises
- # KeyboardInterrupt at once, the REMOVE stays sent, and the listener stays
in the bus. A
- # later removal or close joins the worker and empties the bus.
+ # As PySpark's remove, which acquires the lock of the bus that its
post_to_all holds while a
+ # callback runs, here one that does not return: a signal interrupts the
acquire, so it raises
+ # KeyboardInterrupt at once, before the REMOVE is sent, and the listener
stays in the bus. A
+ # later removal or close sends the REMOVE, joins the worker and empties
the bus.
import signal
import threading
@@ -20185,11 +20185,12 @@ def
test_sigint_during_the_join_of_a_busy_listener_worker_raises_keyboard_interr
finally:
timer.cancel()
assert time.monotonic() - started < 3
- assert service.listener_removals == 1
+ assert service.listener_removals == 0
assert spark.streams._sqlb._listener_bus == [listener]
finally:
release.set()
spark.streams.removeListener(listener)
+ assert service.listener_removals == 1
assert spark.streams._sqlb._listener_bus == []
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]