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

   ### What changes were proposed in this pull request?
   
   At interpreter exit, the exit hook of a session that is not stopped sends 
one `clean_cache` command (SPARK-61753), and, with 
`SPARK_CONNECT_RELEASE_SESSION_ON_EXIT`, then releases and closes the session. 
When the server is gone, the request waits on the retries of the retry policy, 
as in PySpark: over 15 minutes with the default policy, in both clients.
   
   In PySpark 4.3.0, a signal ends that wait:
   - `SparkConnectClient._on_exit` (`client/core.py:2579-2597`) calls 
`_cleanup_ml_cache` (`:2598-2617`).
   - `_cleanup_ml_cache` catches `Exception` only, so the `KeyboardInterrupt` 
of Ctrl-C is raised out of the sleep of the retries (`retries.py:306`) and out 
of `_on_exit`.
   - `atexit` reports it ("Exception ignored in atexit callback") and runs the 
next hook. The session is neither released nor closed.
   
   The drop-in's `PyExitHook::_on_exit` (`crates/pyspark-rs/src/session.rs`) 
ran the requests in a bare `py.detach`, which never checks the signals of the 
process. So Ctrl-C did nothing until the retries ended, and the process could 
only be killed.
   
   The change, in `PyExitHook::_on_exit` only:
   - The `clean_cache`, and the release of the session, go through 
`interrupt::interruptible_outcome`, as `stop()` and the other waits of a 
session do, so a signal ends the wait.
   - The exception of the signal handler that is not an `Exception`, such as 
the `KeyboardInterrupt`, is raised out of the hook, which returns 
`PyResult<()>` now. The release and the close are not made then, as in PySpark.
   - A failure of a request, and a handler's exception that is an `Exception`, 
are ignored, as PySpark's `except Exception: pass` ignores them.
   
   `docs/compatibility.md` says that a signal ends the wait at exit, as it ends 
PySpark's.
   
   ### Why are the changes needed?
   
   A program that exits while its server is gone waits on the retries of its 
exit hook, as in PySpark. With PySpark, Ctrl-C ends that wait. With the 
drop-in, it did not, so the process could only be killed.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A signal ends the wait of the exit hook, as in PySpark. Its 
`KeyboardInterrupt` is reported by `atexit` ("Exception ignored in atexit 
callback: ... KeyboardInterrupt"), and the process exits with its status.
   
   ### How was this patch tested?
   
   - **New test** 
`test_f_a_signal_ends_the_wait_of_the_exit_hook_when_the_server_is_gone` in 
`python/tests/test_exit_cleanup_offline.py`, with and without the release 
variable:
     - a child whose session is live exits while its mock server is gone, and 
is sent SIGINT while its exit hook waits;
     - it exits at once with status 0, and atexit reports the 
`KeyboardInterrupt`.
     - It fails on master, where the child is still waiting after 60 s, and 
passes with the change. The other 23 cases of the file pass.
   - **Probes of both clients** against a port where nothing listens 
(`p_exit_sigint.py`):
   
   | case | pyspark-client 4.3.0 | master | this PR |
   |---|---|---|---|
   | a live session, SIGINT 4 s into the exit | exits after 0.2 s, status 0 | 
still waiting after 45 s | exits after 0.1 s, status 0 |
   | a client of `SparkConnectClient(url)` | exits after 0.2 s | still waiting 
after 45 s | exits after 0.1 s |
   | `SPARK_CONNECT_RELEASE_SESSION_ON_EXIT=true` | exits after 0.5 s | still 
waiting after 45 s | exits after 0.2 s |
   | no signal | still waiting after 900 s | still waiting after 900 s | 
unchanged |
   
   - **The gates:**
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core`;
     - `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it: 13 
passed;
     - ruff 0.14.0, `test_ruff_excludes.py`, `audit_no_stubs.sh`, the stub and 
dispatch generators, the guards and `test_type_stubs_offline.py`;
     - the offline suite of `coverage.yml`: 6337 passed, 77 skipped;
     - `test_dropin_e2e.py` against a Spark 4.3.0 server: 319 passed.
   
   One case stays different. Say a handler raises an `Exception`, not a 
`BaseException`, while PySpark's first plan of a session looks up the 
plan-compression configuration. PySpark's lookup swallows it there, and its 
`clean_cache` goes on waiting. The drop-in has no such lookup at exit, so the 
wait ends, and the exception is ignored, as PySpark's `_cleanup_ml_cache` 
ignores one during the `ExecutePlan`.
   
   ### 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