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

   ### What changes were proposed in this pull request?
   
   An unpickled Column of the Python client keeps the 
`spark.connect.Expression` that it was pickled with (`ExpressionNode::Sent`). 
Serializing it clones that proto with prost's derived `Clone`, which recurses 
once per level of the expression.
   
   This PR runs that clone on a stack grown for the expression's levels, with 
`stacker::maybe_grow` sized by `sent_expression_stack(levels)`. The clone of a 
`LogicalPlan::Raw` relation (`raw_relation_stack`) already works this way. A 
shallow expression still clones on the caller's stack, because `maybe_grow` 
adds a segment only when less than that is left.
   
   ### Why are the changes needed?
   
   The clone ran on the caller's stack. A Column of 300 levels, pickled, 
unpickled and then used on a Python thread of 512 KiB overflows that thread in 
the clone and crashes the process. For example:
   
   ```python
   column = F.col("id")
   for _ in range(300):
       column = column + 1
   column = pickle.loads(pickle.dumps(column))
   spark.range(1).select(column.alias("x")).schema  # on a thread of 512 KiB: 
SIGBUS
   ```
   
   
`test_dropin_offline.py::test_a_deep_column_unpickles_and_is_used_on_a_thread_of_512_kib`
 was at the edge on macOS x86_64. It passed with a build of 7d989783 and failed 
with a build of 7918be35, which changed only ML modules: the clone's frames 
grew with the codegen of the crate, and the clone took about 560 frames. The 
backtrace at the SIGBUS is `Expression::to_proto_with_origin`, then the 
recursion of `<ExprType as Clone>::clone`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A deep unpickled Column no longer crashes the process when a thread of 
a small stack uses it.
   
   ### How was this patch tested?
   
   - A new unit test, `a_deep_sent_expression_is_serialized_on_a_small_stack`, 
serializes an expression of an unpickled Column with `MAX_EXPRESSION_DEPTH` 
(745) levels on a thread of 512 KiB. It fails without the fix (`thread has 
overflowed its stack`) and passes with it.
   - `test_a_deep_column_unpickles_and_is_used_on_a_thread_of_512_kib` passes 3 
times out of 3 on macOS x86_64, where it failed before. The same steps now 
complete on threads of 384 and 448 KiB too.
   - The core lib tests (692), `cargo fmt --all --check`, and clippy of the 
core (`-D warnings`, Rust 1.100.0).
   
   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