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

   ### What changes were proposed in this pull request?
   
   [SPARK-61469](https://issues.apache.org/jira/browse/SPARK-61469): 
`runtime::block_on` and `runtime::block_on_abortable` 
(`crates/spark-connect-core/src/runtime.rs`) pin the future they drive on the 
heap (`Box::pin(f)`) before handing it on. The frames that move it to the 
runtime (`drive`, the runtime's `block_on`, the closure of `block_in_place`) 
then hold only a pointer to it, whatever the compiler inlines.
   
   ### Why are the changes needed?
   
   The future of a request holds the state of its gRPC call and of its retries, 
tens of KiB: the `analyze_plan` future is about 45 to 75 KiB. Moved by value, 
each frame between the caller and the runtime keeps a copy of it on the calling 
thread, unless the compiler inlines that frame. Which frames are inlined 
depends on the code of the crate that calls them, so an unrelated change tips 
it.
   
   - On master, on macOS arm64 in release, `DataFrame::schema()` of a plan 
already used about 355 KiB of the stack before the request was cloned.
   - With other open changes on top (#495, #503, #515), `runtime::drive` 
stopped being inlined and became its own frame of 147 KiB. The use then went 
past the 512 KiB thread that SPARK-61400's `deep_request_stack_tests` run on, 
the smallest stack a Python thread may have. Those tests aborted with a stack 
overflow (SIGABRT or SIGBUS), even for `spark.range(1).schema()`. Three 
sessions hit this independently with three different changes.
   
   | Frame, release, macOS arm64 | Before | After |
   |---|---|---|
   | `runtime::block_on` | 147 KiB | 832 B |
   | `runtime::drive` (when not inlined) | 147 KiB | a pointer |
   | the runtime's `block_on` | 73 KiB | 80 B |
   | used up to the request clone in `schema()` | ~355-526 KiB | ~127 KiB |
   
   With the future boxed, `schema()` and `collect()` of shallow and 746-level 
plans pass on threads of 512, 384, 256 and 192 KiB, and fail only at 128 KiB. 
Thanks to spark-0b's measurements. The cost is one heap allocation per blocking 
call, next to a gRPC call.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No behavior change. A Rust API or Python client call made on a thread with a 
small stack (for example, a Python thread created with 
`threading.stack_size(512 * 1024)`) no longer overflows it, whatever else is in 
the build.
   
   ### How was this patch tested?
   
   - New test `test_block_on_holds_no_copy_of_a_large_future_on_a_small_stack` 
in `crates/spark-connect-core/src/runtime.rs`, after 
`test_block_on_async_task`. It drives a future with a 192 KiB state through 
`block_on` on a 512 KiB thread in release, and on an 8 MiB thread in debug, 
where frames are larger, as in SPARK-61400's tests. Without the boxing, it 
overflows the thread.
   - `cargo test -p apache-spark-connect-core --release --lib -- runtime::`, 
`cargo test -p apache-spark-connect --release --lib` (including 
`deep_request_stack_tests`), the build of all tests without warnings, and 
`cargo fmt --all --check`.
   - With this change, the small-stack tests also pass on master with #495, 
#503 or #515 on top, each of which overflowed them before.
   
   ### 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