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

   ### What changes were proposed in this pull request?
   
   [SPARK-61250](https://issues.apache.org/jira/browse/SPARK-61250): Run the 
synchronous API inside a current-thread Tokio runtime on another thread.
   
   - `runtime::block_on` runs the future on a scoped thread, while the caller 
waits, when the caller is in a `current_thread` runtime. Inside a multi-thread 
runtime (block in place) and outside any runtime, the path is unchanged.
   - The future is polled on that thread, so the caller's per-thread state is 
carried over: `calling_thread_id()` gives the caller's thread ID there, which 
keys the user context extensions read by each request; the callback depth and 
the recursion failure of the caller are copied in and back out, so a call from 
inside a callback bounds its recursion as on the caller's thread. Operation 
tags are read when the request is built, on the caller's thread, so they are 
unchanged.
   - `block_on` now requires `F: Send` and `F::Output: Send`. Every caller in 
the workspace satisfies them (`cargo check --workspace --all-targets` and 
`--features wasm-udf` are clean).
   - The crate docs and the quickstart say the API is synchronous and may be 
called from any thread, including inside a Tokio runtime of either flavor, and 
that the call blocks the calling thread.
   - Official PySpark has no such limit: its client is a plain blocking client 
that works from any thread.
   
   ### Why are the changes needed?
   
   Inside a `current_thread` runtime, such as the one of `#[tokio::test]` or of 
`#[tokio::main(flavor = "current_thread")]`, every synchronous call panicked 
with "Cannot start a runtime from within a runtime": `collect`, `count`, 
`schema`, `sql` and `stop`. `stop()` marks the session stopped before it 
releases the session, so the panic left the session marked stopped and the 
server session was never released; it lived until the server's idle timeout.
   
   ```rust
   #[tokio::test]
   async fn t() {
       let spark = 
SparkSession::builder().remote("sc://localhost:15002").get_or_create().unwrap();
       spark.range(3).unwrap().count().unwrap(); // panicked before this change
   }
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The synchronous API works inside a `current_thread` runtime instead of 
panicking. The call blocks the calling thread, as it does elsewhere, and the 
work runs on a thread of its own.
   
   ### How was this patch tested?
   
   - `runtime::tests` (new, in `crates/spark-connect-core/src/runtime.rs`): 
`block_on` inside a `current_thread` runtime returns the value; a nested 
`block_on` there works; the caller's thread ID is seen from the other thread; a 
call from inside a callback at the recursion bound fails with `RecursionError` 
there too; a panic propagates to the caller. The first two, and the 
caller-thread and callback-depth checks, fail without the change.
   - `test_sync_api_inside_a_current_thread_runtime` (new, 
`crates/spark-connect/tests/e2e_integration.rs`, before `test_schema`, against 
a live server): a `#[tokio::test]` runs `count`, `collect` and `sql`; the 
requests seen by an execute-plan hook carry the tag and the user context 
extension added on the test thread; a thread interrupts the operation by that 
tag; `stop()` releases the session, the stopped session raises 
`NO_ACTIVE_SESSION`, and a new session works. It fails without the change (the 
first call panics) and with the extension read on the worker thread (the 
request does not carry the extension).
   - Two existing tests 
(`session::tests::version_cache_recovers_after_lookup_panics`, 
`dataframe::cache_tests::schema_cache_recovers_after_analysis_panics`) used the 
removed panic to poison a cache lock. They now poison the lock directly with a 
panic while the guard is held, and keep the same recovery assertions.
   - The sync API is PySpark's behavior; the case has no official-PySpark 
counterpart to compare against, since it is a Rust-only runtime constraint.
   - The tests of the transport crate, and the live Rust tests 
`e2e_integration` (18 passed) against a local Spark 4.3.0 server.
   - The lib tests of both crates (669 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites (2820 passed, 8 skipped), and the 
live drop-in and Arrow suites (232 passed) and live cases (14 passed) against a 
local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines.
   
   ### 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