HyukjinKwon opened a new pull request, #368: URL: https://github.com/apache/spark-connect-rust/pull/368
### What changes were proposed in this pull request? [SPARK-61188](https://issues.apache.org/jira/browse/SPARK-61188): raise `RecursionError` instead of crashing the process when progress handlers and hooks run queries without end. - `spark_connect_core::runtime` counts the progress handlers and the hooks that run on a thread (`CallbackGuard`). `check_callback_depth()` fails an execution that starts past `MAX_CALLBACK_DEPTH` (2) nested callbacks with a new `SparkErrorKind::RecursionError`. After such a failure, the callbacks above it fail their queries at once until the outermost callback returns, so the failures do not multiply. - The check is made when a dataframe execution starts (`SparkSession::execute_plan`) and before a hook is called (`SparkConnectClient::hooked`). The progress handlers and the hooks are called inside a guard. - The Python layer raises the error of that kind as the builtin `RecursionError`. Its exceptions in handlers and hooks keep their existing behavior: a handler's exception goes to `sys.unraisablehook`, a hook's exception is raised from the execution that calls it. Official PySpark 4.3.0 with the same unguarded handler: the outer `collect()` raises `SparkConnectGrpcException` ("Exception serializing request!"), since the handler's exception is raised out of the progress loop of the outer query, and the process survives. This change keeps the process alive and returns the outer result. The exception of a nested query is printed by `sys.unraisablehook` (`RecursionError`), because a progress handler's exception does not fail the execution in this client (as before this change, and pinned by `test_progress_handlers_are_called_with_the_progress_of_executions_as_pyspark`). ### Why are the changes needed? ```python spark.registerProgressHandler(lambda **kw: spark.range(4).count() if not kw["done"] else None) spark.range(2000000).selectExpr("sum(id) as t").collect() # exit 139 (SIGSEGV), no Python exception ``` Each nested query runs the handler again, and each level takes native stack, so the stack of the process overflows before Python's recursion limit is reached. Measured: about 100 KiB per level, so four levels crash a 512 KiB thread and about 20 crash a 2 MiB thread. The bound of two levels leaves room for the frames of the thread itself. Guarded handlers and hooks that run one nested query are unaffected. ### Does this PR introduce _any_ user-facing change? Yes. An unguarded progress handler or hook that runs queries without end no longer crashes the process: the query past the bound raises `RecursionError` in the callback, and the outer query returns its result. ### How was this patch tested? - Live `test_progress_handler_that_runs_queries_without_end_raises_recursion_error` runs the unguarded handler above in a subprocess: the return code is 0, the outer result is `[Row(t=1999999000000)]`, and stderr has the `RecursionError`. Without the bound the subprocess dies with -11. - Offline `test_progress_handler_that_runs_queries_without_end_is_bounded` runs the same handler on the mock Connect service: the handler is called for two nested levels, one `RecursionError` goes to `sys.unraisablehook`, and the outer query returns. Without the bound the test process segfaults. - Rust `test_callback_depth_is_bounded_and_released` in `runtime.rs`: the depth check fails at the bound and is released when the callbacks end. - The existing live `test_progress_handler_runs_a_query_of_its_own_as_pyspark` and `test_hook_runs_a_query_of_its_own_as_pyspark` still pass (one nested query). - The tests of both crates, e2e included, against a local Spark 4.3.0 server (895 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2228 passed, 8 skipped), and the live drop-in, Arrow and ML suites (210 passed) and live cases (10 passed); after moving onto master with SPARK-61186, the lib tests of both crates and the progress, hook and tag tests again. rustfmt passes, and 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]
