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 29105b4c [SPARK-61577][CONNECT][FOLLOWUP] Keep the session open when a 
signal interrupts the backoff of the release of stop(), as PySpark does
29105b4c is described below

commit 29105b4cb20961affbdea41c249200111ab470e8
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Fri Nov 13 10:43:49 2026 +0900

    [SPARK-61577][CONNECT][FOLLOWUP] Keep the session open when a signal 
interrupts the backoff of the release of stop(), as PySpark does
    
    ### What changes were proposed in this pull request?
    
    A follow-up of #577 (SPARK-61577), from a review of it. Since #577, an 
abortable wait stops its retries at the first interrupt of the signal watch. 
That holds for the release that `SparkSession::stop` waits for through 
`block_on_abortable`, and a running operation on the thread gives the 
interrupt. The release then ends with the error of `wait_aborted()`.
    
    That error went down the `Some(released) => released` arm of `stop()`, 
which marks the session stopped, cancels the listener bus, closes the client 
and returns the error. So a later `stop()` sent no ReleaseSession. `stop()` now 
treats that error like the arm for a signal that ends the wait itself. It 
returns the "the release of the session was interrupted; the session is still 
open" error, and neither marks the session stopped nor closes the client.
    
    ### Why are the changes needed?
    
    To match PySpark 4.3.0. Its `stop()` lets the `KeyboardInterrupt` of a 
signal leave the `except Exception` around the release before `client.close()` 
(pyspark/sql/connect/session.py:948-958). So the session stays open, and a 
later `stop()` releases it, as the existing arm of this client already does for 
a signal that ends the wait.
    
    In the Python client, no route is known that reaches the interrupt of the 
watch here: a progress handler runs after the stream read has marked the thread 
idle. So this hardens the behavior of the wait for any caller, as #548 did for 
`watched_abortable`.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No, for the cases above.
    
    ### How was this patch tested?
    
    - 
`session::tests::a_stop_that_a_signal_interrupts_in_its_backoff_leaves_the_session_open`,
 new. The test server drops each connection, so the release fails with 
UNAVAILABLE and backs off for a minute, and a watch interrupts once. It asserts 
that `stop()` returns an error within 5 seconds, that the session is not 
stopped, and that its client is not closed. It fails without the change and 
passes with it.
    - `cargo test -p apache-spark-connect --release --lib` (648 passed) and 
`cargo test -p apache-spark-connect -p apache-spark-connect-core --release 
--no-run`.
    - The live `e2e_session_catalog` and `e2e_integration` against a Spark 
4.3.0 server, the offline drop-in suites, `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 #595 from HyukjinKwon/scr0b-followup-O1.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 crates/spark-connect/src/session.rs | 105 +++++++++++++++++++++++++++++++++---
 1 file changed, 99 insertions(+), 6 deletions(-)

diff --git a/crates/spark-connect/src/session.rs 
b/crates/spark-connect/src/session.rs
index 8d08c056..cafc0732 100644
--- a/crates/spark-connect/src/session.rs
+++ b/crates/spark-connect/src/session.rs
@@ -1438,16 +1438,21 @@ impl SparkSession {
         let released = if self.client.is_closed() {
             Ok(())
         } else {
+            let interrupted = || {
+                SparkError::connect_msg(
+                    "the release of the session was interrupted; the session 
is still open",
+                )
+            };
             match 
spark_connect_core::runtime::block_on_abortable(self.client.release_session()) {
+                Some(Err(error)) if 
spark_connect_core::client::is_wait_aborted(&error) => {
+                    return Err(interrupted());
+                }
                 Some(released) => released,
                 // The wait was stopped, by a signal of the process in the 
Python client: as
                 // PySpark's `stop()`, which the exception of the signal 
leaves before it closes
-                // the client, the session stays open, and a later `stop()` 
releases it.
-                None => {
-                    return Err(SparkError::connect_msg(
-                        "the release of the session was interrupted; the 
session is still open",
-                    ));
-                }
+                // the client, the session stays open, and a later `stop()` 
releases it. The
+                // release of an interrupted backoff gives the same error, 
which leaves it so.
+                None => return Err(interrupted()),
             }
         };
         // Before the listener bus is cancelled: an add of a listener that the 
cancellation ends
@@ -3469,6 +3474,94 @@ mod tests {
         assert!(spark.is_stopped());
     }
 
+    /// A signal that interrupts the stop in the backoff of its release makes 
the stop raise, and
+    /// leaves the session open for a later stop to release, as PySpark's 
`stop()` does. The
+    /// server closes each connection at once, so the release fails as 
UNAVAILABLE and backs off
+    /// for a minute, unless the signal ends the backoff first.
+    #[test]
+    fn 
a_stop_that_a_signal_interrupts_in_its_backoff_leaves_the_session_open() {
+        use spark_connect_core::runtime::{with_wait_watch, WaitWatch, Watch};
+        use std::sync::mpsc::channel;
+        use std::time::{Duration, Instant};
+
+        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a 
local port");
+        listener
+            .set_nonblocking(true)
+            .expect("a nonblocking listener");
+        let port = listener.local_addr().expect("the local address").port();
+        let accepted = Arc::new(AtomicU64::new(0));
+        let ended = Arc::new(AtomicBool::new(false));
+        let (taken, done) = (Arc::clone(&accepted), Arc::clone(&ended));
+        std::thread::spawn(move || {
+            while !done.load(Ordering::SeqCst) {
+                match listener.accept() {
+                    // The connection is dropped at once, which closes it.
+                    Ok(_) => {
+                        taken.fetch_add(1, Ordering::SeqCst);
+                    }
+                    Err(_) => std::thread::sleep(Duration::from_millis(5)),
+                }
+            }
+        });
+        let builder = 
ChannelBuilder::parse(&format!("sc://127.0.0.1:{port}")).expect("valid URL");
+        let mut client =
+            block_on(SparkConnectClient::connect(&builder)).expect("failed to 
build client");
+        client.set_retry_policy(spark_connect_core::retries::RetryPolicy {
+            initial_backoff_ms: 60_000,
+            max_backoff_ms: Some(60_000),
+            ..spark_connect_core::retries::RetryPolicy::default()
+        });
+        let spark = SparkSession::new(client);
+        // One interrupt, once the release has connected, and half a second 
later, so that the
+        // failed release is backing off by then.
+        let watch: Arc<WaitWatch> = {
+            let taken = Arc::clone(&accepted);
+            let interrupted = Arc::new(AtomicBool::new(false));
+            let since: Arc<Mutex<Option<Instant>>> = Arc::default();
+            Arc::new(move || {
+                if taken.load(Ordering::SeqCst) == 0 {
+                    return Watch::Continue;
+                }
+                let first = since
+                    .lock()
+                    .unwrap()
+                    .get_or_insert_with(Instant::now)
+                    .to_owned();
+                if first.elapsed() >= Duration::from_millis(500)
+                    && !interrupted.swap(true, Ordering::SeqCst)
+                {
+                    Watch::Interrupt(Box::pin(async {}))
+                } else {
+                    Watch::Continue
+                }
+            })
+        };
+        let (stopped, outcome) = channel();
+        let stopping = spark.clone();
+        std::thread::spawn(move || {
+            let _ = stopped.send(with_wait_watch(watch, || stopping.stop()));
+        });
+        let stop = outcome
+            .recv_timeout(Duration::from_secs(5))
+            .expect("the signal ends the backoff of the release");
+        let error = stop.expect_err("the interrupted stop raises");
+        assert!(
+            error
+                .to_string()
+                .contains("the release of the session was interrupted; the 
session is still open"),
+            "the interrupted stop raises the error of an interrupted release: 
{error}"
+        );
+        assert!(
+            !spark.is_stopped(),
+            "an interrupted stop marked the session stopped"
+        );
+        assert!(
+            !spark.client.is_closed(),
+            "an interrupted stop closed the client"
+        );
+        ended.store(true, Ordering::SeqCst);
+    }
+
     /// As PySpark's `stop()`, whose `SparkSession._lock` is one lock of the 
class: the `stop()` of
     /// another session waits for the release of the one in progress, sends 
its own release only
     /// after it, and is not stopped until that release has ended.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to