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

   ### What changes were proposed in this pull request?
   
   A follow-up of the exit hook of SPARK-61852 (#775), from a round-3 review 
against pyspark-client 4.3.0.
   
   In PySpark:
   - SparkConnectClient registers `atexit.register(self._on_exit)` 
(client/core.py:955-959);
   - `_on_exit` (core.py:2579-2597) does nothing for a closed client;
   - `SparkSession.__del__` (connect/session.py:879-887) closes its client.
   
   So a session that the program drops, such as `s2 = spark.newSession(); ...; 
del s2`, is freed by refcount, and nothing is sent for it at exit.
   
   The drop-in's exit hook held the core session strongly. So it sent the 
clean_cache of every session ever made at exit, even of the sessions that the 
program dropped, whose clients and channels lived until then. With the server 
gone, the exit waited on the retries: a script that dropped a `newSession()` 
hung at exit, where PySpark's exits at once.
   
   With this PR:
   - **The exit hook holds its session weakly.** The core gets a doc-hidden 
`SparkSession::downgrade()` and `WeakSparkSession::upgrade()`.
     - Every handle of a session (the session object, a clone, or one that a 
DataFrame, reader, writer, catalog, stream, ML object or conf holds) holds all 
its state, so `upgrade()` gives the session while one is alive, and None once 
they are all dropped.
     - The weak handle keeps none of the state: neither the client, nor the 
handlers, listeners and hooks, which hold Python objects that can hold the 
session.
     - The registry of the hooked sessions is weak too.
   - **A dropped session.** It is dropped with its client, which closes its 
connection when no other session shares it, and its hook does nothing at exit. 
`newSession()` and `cloneSession()` share their parent's channel, as PySpark's 
grpc channels share one connection.
   - **A reachable session still sends PySpark's `_on_exit` requests:** 
clean_cache, and release and close with `SPARK_CONNECT_RELEASE_SESSION_ON_EXIT`.
   - **A bare `SparkConnectClient`,** which no session object owns, is kept 
until the exit (the private `_keep_until_exit`), as PySpark's atexit keeps it. 
That covers the constructor, `client.newSession()` and `client.clone()`, so it 
still sends at exit, as PySpark's does.
   
   Results, by the requests of the second session at exit:
   
   | case | PySpark 4.3.0 | master | this PR |
   |---|---|---|---|
   | `newSession()` dropped | none | clean_cache | none |
   | kept, or a DataFrame, reader, catalog, stream, udf or writer of it kept | 
clean_cache | clean_cache | clean_cache |
   | `SparkConnectClient(url)` dropped, or kept | clean_cache | clean_cache | 
clean_cache |
   | `cloneSession()` dropped | none | clean_cache | none |
   | the release variable set, `newSession()` dropped | none | clean_cache, 
release | none |
   
   `dev/parity/known-differences.md` and `docs/compatibility.md` describe two 
remaining differences:
   - **A reference cycle.** With the garbage collector disabled, reading 
`catalog` or `streams` puts PySpark's session in a reference cycle that only a 
collection frees, so until one its exit still sends. The drop-in has no such 
cycle; this is intended.
   - **A kept `conf` or `client` without the session.** PySpark's `__del__` 
closes the client under a kept `conf` or `client`, so they raise 
`NO_ACTIVE_SESSION` and nothing is sent at exit. The drop-in's `conf` and 
`client` keep their session open.
   
   ### Why are the changes needed?
   
   So that a program that drops its sessions sends nothing for them at exit, 
and does not hang there when the server is gone, as with PySpark; and so that 
their clients are freed.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes: a dropped session sends no clean_cache, or release, at exit, and its 
client is freed.
   
   ### How was this patch tested?
   
   - **New tests in `test_exit_cleanup_offline.py`** (subprocesses against the 
mock server):
     - a dropped `newSession()` sends nothing, with and without the release 
variable;
     - a session held only by a DataFrame still sends;
     - dropped bare clients (the constructor, `newSession`, `clone`) still send;
     - a dropped `newSession()` exits at once when the server is gone, for both 
values of the variable.
   
     The dropped-session and prompt-exit tests fail on master.
   - **New core tests:** 
`a_weak_session_upgrades_while_a_handle_of_it_is_alive` and 
`a_weak_session_keeps_none_of_the_state_of_the_session`.
   - **Probes of both clients:** the requests at exit of each case above, side 
by side.
   - **Rust.**
     - `cargo test -p apache-spark-connect -p apache-spark-connect-core 
--release`, and `--features wasm-udf`;
     - `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
   - **Python.**
     - The offline suite of `coverage.yml`, and `test_exit_cleanup_offline.py` 
on pandas 2 and 3.
     - Live `test_dropin_e2e.py`.
   - **Lint.**
     - `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs -p 
apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0, 
`audit_no_stubs.sh` and `scripts/tests`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps`, and the type-stubs 
drift check and test.
   
   ### 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