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

   ### What changes were proposed in this pull request?
   
   PySpark's `SparkConnectClient` registers an `_on_exit` with `atexit`. At 
interpreter exit, it cleans the ML cache of each client that is not closed with 
one `clean_cache` command, ignoring its errors. When 
`SPARK_CONNECT_RELEASE_SESSION_ON_EXIT` is `true` or `1` (read when the client 
is made), it then releases and closes the session. This client had no such 
hook, so a session that was not stopped left its cached models, summaries and 
other ML objects on the server until the session expired.
   
   This PR adds the hook:
   
   - Each session registers one exit hook when its first Python object is made, 
as PySpark's client does when it is made. The hook holds the core session, not 
the Python object, so a session object that is dropped is still freed, and its 
hook still runs at exit.
   - At exit, a stopped session sends nothing, as PySpark skips a closed 
client. A session that is not stopped sends one `clean_cache`. Its errors are 
ignored, and when the server is gone the exit waits on that request's retries, 
as PySpark's does.
   - With `SPARK_CONNECT_RELEASE_SESSION_ON_EXIT` set to `true` or `1`, in any 
case, the session is then released and closed. The variable is read when the 
session is made, so a change after that has no effect, as in PySpark.
   - The test suites leave sessions open to servers that are gone. A doc-hidden 
`_close_sessions_at_exit`, called from `conftest.py`'s `pytest_sessionfinish`, 
closes them without a release, so the test process does not wait on their 
retries. The child processes of `test_dropin_offline.py` that end with a 
session open to such a server now stop it without a release, for the same 
reason.
   
   `docs/compatibility.md` describes the exit.
   
   One known difference is left for a follow-up. PySpark's at-exit 
`clean_cache` sends no `ReleaseExecute` after it. Its reattachable iterator 
schedules each release on a `ThreadPoolExecutor`, which refuses new work once 
interpreter shutdown has begun, so the iterator raises `RuntimeError: cannot 
schedule new futures after interpreter shutdown` after the first response, and 
`_cleanup_ml_cache` swallows it. Here the `clean_cache` is followed by its two 
`ReleaseExecute`. The same refusal makes any `ExecutePlan` in a user's atexit 
handler raise that `RuntimeError` on PySpark (for example 
`spark.range(3).count()`), while it succeeds here. The follow-up will make this 
client refuse releases after interpreter shutdown in the same way.
   
   ### Why are the changes needed?
   
   To match PySpark 4.3.0's client, which frees the server's ML cache of a 
session at exit.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. At exit, a session that is not stopped now cleans its ML cache, and 
with `SPARK_CONNECT_RELEASE_SESSION_ON_EXIT` it is also released, as in 
PySpark. If the server is gone, the exit now waits on the retries of that 
request, as PySpark's does.
   
   ### How was this patch tested?
   
   - New `python/tests/test_exit_cleanup_offline.py`, 13 cases. Each runs a 
child interpreter against the mock server and checks the requests that the 
child's exit sends:
     - a live session sends one `clean_cache`, and a stopped one sends nothing;
     - the variable's values (`true`, `1`, `TRUE`; `false`, `yes`, empty), and 
that it is read when the session is made;
     - two sessions send one `clean_cache` each;
     - a stopped session exits promptly with status 0 after the server is gone, 
with and without the variable;
     - the seam `_close_sessions_at_exit`.
   
     It is added to the offline gate in `coverage.yml`.
   - The mock server records the order of the requests it receives.
   - Through a recording proxy on a live server, the exits of official PySpark 
4.3.0 and of this client after a KMeans fit both end with the same 
`clean_cache` request (this client then sends the two `ReleaseExecute` 
described above).
   - Before the rebase onto the current master, the CI-order offline suite 
passed in one process (5691 passed), with no added time at exit: wall 780 s, 
pytest 779.96 s. After the rebase, `test_dropin_offline.py` and 
`test_exit_cleanup_offline.py` pass (2242 passed), with no test slower than 4 s.
   - Live, `test_dropin_e2e.py` and `test_ml_api_e2e.py` pass (341 passed), 
with no added time at exit (wall 527 s, pytest 526.6 s). One persist test 
failed there because another client cached the same plan on the shared server 
at the same time. It passes when run alone, twice.
   - `cargo fmt --check`, `cargo +1.100.0 clippy --workspace --all-targets -- 
-D warnings`, `scripts/audit_no_stubs.sh`, `ruff check` and `ruff format 
--check` are clean.
   
   ### 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