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 9258b64c [SPARK-61498][CONNECT][FOLLOWUP] End the abortable wait at
the second Ctrl-C while its interrupt hangs, as the wait of an execution does
9258b64c is described below
commit 9258b64cd14665613b40354ba7cb08c8021de6cb
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Thu Nov 12 16:14:57 2026 +0900
[SPARK-61498][CONNECT][FOLLOWUP] End the abortable wait at the second
Ctrl-C while its interrupt hangs, as the wait of an execution does
### What changes were proposed in this pull request?
A follow-up of #545 (SPARK-61498), from a review of it. #545 made the wait
of an execution (`watched` in `crates/spark-connect-core/src/runtime.rs`) end
at the second Ctrl-C while the interrupt that the first one started hangs. Its
sibling `watched_abortable`, the wait of `block_on_abortable`, still stopped
polling the signal watch once the watch had returned `Watch::Interrupt`: its
tick arm was off while an interrupt ran, and once its future had ended it
awaited the interrupt with no [...]
`watched_abortable` now does what `watched` does:
- The polls go on after the first interrupt is spawned. A later
`Watch::Abort` cancels the interrupt and ends the wait with `None`, as an abort
does before it.
- When the future has ended while the interrupt runs, the interrupt is
awaited with the polls, and a `Watch::Abort` cancels it. The output of the
future is still returned.
- The first signal is unchanged: one interrupt is spawned, and the future
is driven on.
### Why are the changes needed?
The two waits behaved differently for the same signals.
`block_on_abortable` has one caller, the release of `SparkSession::stop`. Its
watch returns `Watch::Interrupt` only while the calling thread waits for an
execution. So in the Python client this is reached only when `stop()` runs
inside such a wait. A `stop()` from a progress handler does not reach it, since
the handlers run after the wait of the stream read. The wait now holds for any
caller, as `watched` does, rather than hang u [...]
### Does this PR introduce _any_ user-facing change?
No for the cases above. The Rust API installs no signal watch, and in the
Python client the path needs a `stop()` inside the wait of an execution.
### How was this patch tested?
- Two tests in `runtime.rs`, next to #545's tests of `watched`. They run
the wait under a scripted watch on a thread of their own, with a 5-second
bound, so that a wait that does not end fails the test instead of hanging it:
-
`test_a_second_signal_ends_an_abortable_wait_while_its_interrupt_hangs`: an
interrupt that never ends, then an abort. The wait ends with `None`, and the
interrupt is cancelled.
-
`test_a_second_signal_ends_an_abortable_wait_while_its_future_waits_after_the_interrupt`:
the same while the future still waits after the interrupt. It also checks that
the output is returned after a finished interrupt when no second signal comes.
Both fail at the 5-second bound without the change and pass with it.
- `cargo test -p apache-spark-connect-core --release --lib` (218 passed),
`cargo test -p apache-spark-connect -p apache-spark-connect-core --release
--no-run`, `cargo build -p pyspark-rs --release`, the offline drop-in and ML
suites (2005 passed), the live `e2e_session_catalog` and `e2e_reattach_drop`
against a Spark 4.3.0 server, the `stop`/`sigint`/`interrupt`/`release` subset
of `test_dropin_e2e.py`, `cargo fmt --all -- --check`, and
`scripts/audit_no_stubs.sh`.
### 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 #548 from HyukjinKwon/SPARK-61498-followup-abortable-wait.
Authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
crates/spark-connect-core/src/runtime.rs | 139 +++++++++++++++++++++++++++++--
1 file changed, 132 insertions(+), 7 deletions(-)
diff --git a/crates/spark-connect-core/src/runtime.rs
b/crates/spark-connect-core/src/runtime.rs
index 6a5c7ebc..c16d883f 100644
--- a/crates/spark-connect-core/src/runtime.rs
+++ b/crates/spark-connect-core/src/runtime.rs
@@ -187,8 +187,10 @@ where
}
/// `f`, polled as it is, and `watch` called every [`POLL_INTERVAL`]: `None`
at once when it
-/// returns [`Watch::Abort`], else the output of `f`. An interrupt is spawned
once and awaited
-/// with `f`, as in [`watched`].
+/// returns [`Watch::Abort`], else the output of `f`. The first interrupt that
`watch` returns is
+/// spawned and awaited with `f`, as in [`watched`]. The polls go on after it,
so that a second
+/// signal ends the wait even while the interrupt hangs, and the running
interrupt is cancelled
+/// with the wait.
async fn watched_abortable<F: Future>(f: F, watch: Arc<WaitWatch>) ->
Option<F::Output> {
tokio::pin!(f);
let mut ticks = tokio::time::interval(POLL_INTERVAL);
@@ -196,14 +198,35 @@ async fn watched_abortable<F: Future>(f: F, watch:
Arc<WaitWatch>) -> Option<F::
loop {
tokio::select! {
output = &mut f => {
- if let Some(interrupt) = interrupt {
- let _ = interrupt.await;
+ // `f` has ended, so the wait only awaits the interrupt; a
second signal, polled
+ // meanwhile, ends that too.
+ if let Some(mut interrupt) = interrupt {
+ loop {
+ tokio::select! {
+ _ = &mut interrupt => break,
+ _ = ticks.tick() => {
+ if let Watch::Abort = watch() {
+ interrupt.abort();
+ break;
+ }
+ }
+ }
+ }
}
return Some(output);
}
- _ = ticks.tick(), if interrupt.is_none() => match watch() {
- Watch::Abort => return None,
- Watch::Interrupt(sent) => interrupt = Some(tokio::spawn(sent)),
+ _ = ticks.tick() => match watch() {
+ Watch::Abort => {
+ if let Some(interrupt) = interrupt {
+ interrupt.abort();
+ }
+ return None;
+ }
+ Watch::Interrupt(sent) => {
+ if interrupt.is_none() {
+ interrupt = Some(tokio::spawn(sent));
+ }
+ }
Watch::Continue => {}
},
}
@@ -553,6 +576,108 @@ mod tests {
);
}
+ /// The output of `block_on_abortable(f)` under `watch`, run on a thread
of its own, or None
+ /// when the wait has not ended within 5 s: a wait that is not ended then
fails the test,
+ /// rather than hanging it.
+ fn abortable_wait_under<F>(watch: Arc<WaitWatch>, f: F) ->
Option<Option<F::Output>>
+ where
+ F: std::future::Future + Send + 'static,
+ F::Output: Send + 'static,
+ {
+ let (sender, receiver) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ let output = with_wait_watch(watch, || block_on_abortable(f));
+ let _ = sender.send(output);
+ });
+ receiver
+ .recv_timeout(std::time::Duration::from_secs(5))
+ .ok()
+ }
+
+ #[test]
+ fn test_a_second_signal_ends_an_abortable_wait_while_its_interrupt_hangs()
{
+ // As `watched`: the first signal interrupts, and the interrupt never
ends. The second
+ // signal aborts the wait, which ends at once without the output of
its future, and the
+ // interrupt is cancelled.
+ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
+ struct Dropped(Arc<AtomicBool>);
+ impl Drop for Dropped {
+ fn drop(&mut self) {
+ self.0.store(true, Ordering::SeqCst);
+ }
+ }
+ let polls = Arc::new(AtomicUsize::new(0));
+ let cancelled = Arc::new(AtomicBool::new(false));
+ let (counter, flag) = (Arc::clone(&polls), Arc::clone(&cancelled));
+ let watch: Arc<WaitWatch> =
+ Arc::new(move || match counter.fetch_add(1, Ordering::SeqCst) {
+ 0 => {
+ let dropped = Dropped(Arc::clone(&flag));
+ Watch::Interrupt(Box::pin(async move {
+ let _dropped = dropped;
+ std::future::pending::<()>().await
+ }))
+ }
+ 1 => Watch::Abort,
+ _ => Watch::Continue,
+ });
+ let output = abortable_wait_under(watch, std::future::pending::<u8>());
+ assert_eq!(
+ output,
+ Some(None),
+ "the wait did not end at the second signal"
+ );
+ assert_eq!(polls.load(Ordering::SeqCst), 2);
+ let deadline = std::time::Instant::now() +
std::time::Duration::from_secs(5);
+ while !cancelled.load(Ordering::SeqCst) && std::time::Instant::now() <
deadline {
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ assert!(
+ cancelled.load(Ordering::SeqCst),
+ "the interrupt was not cancelled"
+ );
+ }
+
+ #[test]
+ fn
test_a_second_signal_ends_an_abortable_wait_while_its_future_waits_after_the_interrupt()
{
+ // The interrupt ends at once, and the future then waits, as a release
that the server
+ // does not answer: the first signal alone does not end the wait, the
second one does.
+ let polls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let counter = Arc::clone(&polls);
+ let watch: Arc<WaitWatch> =
+ Arc::new(
+ move || match counter.fetch_add(1,
std::sync::atomic::Ordering::SeqCst) {
+ 0 => Watch::Interrupt(Box::pin(async {})),
+ 1 => Watch::Continue,
+ _ => Watch::Abort,
+ },
+ );
+ let output = abortable_wait_under(watch, std::future::pending::<u8>());
+ assert_eq!(
+ output,
+ Some(None),
+ "the wait did not end at the second signal"
+ );
+ assert_eq!(polls.load(std::sync::atomic::Ordering::SeqCst), 3);
+
+ // Without a second signal, the output of the future is still given
after the interrupt.
+ let watch: Arc<WaitWatch> = Arc::new({
+ let first = std::sync::atomic::AtomicBool::new(true);
+ move || {
+ if first.swap(false, std::sync::atomic::Ordering::SeqCst) {
+ Watch::Interrupt(Box::pin(async {}))
+ } else {
+ Watch::Continue
+ }
+ }
+ });
+ let ended = abortable_wait_under(watch, async {
+ tokio::time::sleep(POLL_INTERVAL * 2).await;
+ 7_u8
+ });
+ assert_eq!(ended, Some(Some(7)));
+ }
+
#[test]
fn
test_an_abortable_wait_ends_when_its_watch_aborts_and_another_keeps_waiting() {
// A watch that aborts ends an abortable wait at once, without the
output of its future.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]