HyukjinKwon opened a new pull request, #747:
URL: https://github.com/apache/spark-connect-rust/pull/747
### What changes were proposed in this pull request?
This builds on #743 (SPARK-61753) and closes the difference that #743 leaves
at exit. It should merge after #743.
This PR's branch carries #743's two commits under its own; only the last
commit is this change.
PySpark's client sends the `ReleaseExecute` requests of an execution through
a `ThreadPoolExecutor`
(`ExecutePlanResponseReattachableIterator._release_until` and `_release_all`).
Once the interpreter has begun to shut down, `ThreadPoolExecutor.submit` raises
`RuntimeError: cannot schedule new futures after interpreter shutdown`, and on
PySpark that has two effects:
- An execution in an `atexit` handler, such as `spark.range(3).count()`,
raises that `RuntimeError` after its first response and sends no
`ReleaseExecute`. Requests that are not executions, such as the `AnalyzePlan`
of a schema, still work.
- The `clean_cache` of the exit hook ends the same way. `_cleanup_ml_cache`
ignores the error, so the exit sends no `ReleaseExecute` after it.
Before this PR, both succeeded here and released their executions.
This PR mirrors that:
- The core gets a process-wide refusal of releases,
`spark_connect_core::client::refuse_releases` (doc-hidden). Once it is set, a
reattachable execution fails where PySpark's iterator would schedule a release,
with that `RuntimeError`, and sends nothing. That is after a response, at its
failure (as `_has_next` raises the error of its release), or when its initial
request fails. The `RuntimeError` is a new builtin kind of error,
`BuiltinRuntimeError`, that the binding raises as Python's `RuntimeError`.
- The binding sets it when the interpreter begins to shut down, through
`threading._register_atexit`, as `concurrent.futures` registers the shutdown of
its pools. That runs before the `atexit` hooks and before the non-daemon
threads are joined, the same point at which PySpark's pool starts refusing. The
binding registers it when it is imported, or refuses at once if it is imported
after the shutdown began.
- The Rust API never sets it.
`docs/compatibility.md` describes it.
### Why are the changes needed?
To match PySpark 4.3.0 at interpreter exit: on the wire, where the exit's
`clean_cache` is now the last request of a session, as in PySpark; and in user
code that runs Spark executions in `atexit` handlers or in threads that outlive
the main thread.
### Does this PR introduce _any_ user-facing change?
Yes. Once the interpreter has begun to shut down, an execution raises
`RuntimeError: cannot schedule new futures after interpreter shutdown` after
its first response, as in PySpark, where it succeeded before.
### How was this patch tested?
- `python/tests/test_exit_cleanup_offline.py`:
- a new case: an execution in an `atexit` handler raises
`builtins.RuntimeError` with PySpark's message, and the session sends only that
ExecutePlan and the exit's `clean_cache`, with no `ReleaseExecute`;
- the requests that the other cases compare now include `ReleaseExecute`,
so each checks that the exit sends none.
On #743's code without this change, 10 of those 14 cases fail: every case
whose exit sends a `clean_cache`, by the `ReleaseExecute` after it, and the new
case. All 14 pass with it.
- Through a recording proxy on a live server, after a KMeans fit the exits
of official PySpark 4.3.0 and of this client now both end with the
`clean_cache` request, with no `ReleaseExecute`. The same probe of an `atexit`
handler raises the same `RuntimeError` on both clients, and both still answer
the schema there.
- The CI-order offline suite passes in one process (5697 passed), with no
added time at exit: wall 643 s, pytest 641.8 s.
- `cargo fmt --check`, `cargo +1.100.0 clippy --workspace --all-targets --
-D warnings`, `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]