viirya opened a new pull request, #511: URL: https://github.com/apache/spark-connect-rust/pull/511
### What changes were proposed in this pull request? Four fixes in the PyO3 extension (`crates/pyspark-rs`), from a review of its concurrency and state handling: - **Progress handlers** (`progress.rs`): the number of handlers kept beside the list is now read from the list and stored under one lock after every change, so the count left after concurrent changes is that of the last one. The registry of the sessions no longer makes Python objects while it is locked. - **`toLocalIterator`** (`dataframe.rs`): `__next__` and `close` take the iterator with `try_borrow_mut`, and a call made while another thread is waiting in it raises `ValueError: generator already executing`. - **Streaming listeners** (`streaming.rs`): the id of each registration is kept in its adapter in the bus, not in a `_rust_listener_ids` attribute of the user's listener. `removeListener` reads the bus of its session and removes the first registration that is the listener or equal to it, and reads the bus again when another call removed that registration first. - **Error conversion** (`errors.rs`): a failure to make the exception of the connect classes, or of the class of the kind of the error, becomes the `__context__` of the exception raised in its place. The context of the done error of a progress handler is set with `PyException_SetContext`. A refused `_operation_id` goes to `sys.unraisablehook`. The review also flagged that `addArtifacts` puts a pyfile zip on `sys.path` before it knows the session is open. That is left as it is: PySpark's `_parse_artifacts` does the `sys.path.insert` before it opens the file and before any RPC, and `test_add_artifacts_puts_a_pyfile_zip_on_the_client_path_before_opening_it` already pins that order. ### Why are the changes needed? The extension declares `gil_used = false`, and several of these paths also run Python code (`__eq__`, `__del__`) between two steps that were meant to be one. - A progress handler registered while another change of the handlers was in flight could be left uncounted and never called. This does not need a second thread: the `__del__` of a handler that `clearProgressHandlers` drops can register one. - The second caller of a running `toLocalIterator` got `RuntimeError: Already borrowed`, where PySpark's generator raises `ValueError: generator already executing`. The state of the iterator was never shared, as the borrow is exclusive. - `addListener` changed the user's listener, failed for an object that cannot take the attribute, and left registrations in the bus when several threads added and removed the same listener, also with the GIL. PySpark's bus keeps its listeners in its own list and removes with `list.remove`. - Failures inside the error conversion were dropped with `.ok()` and `let _ =`, so a broken conversion silently raised a less specific exception. ### Does this PR introduce _any_ user-facing change? Yes. - A listener no longer gets a `_rust_listener_ids` attribute, and `removeListener` also removes a registered listener that is equal to the one given, as PySpark does. - A concurrent `next` or `close` of a running `toLocalIterator` raises `ValueError` instead of `RuntimeError`. - An exception raised in place of one that could not be made has the failure as its `__context__`. ### How was this patch tested? Nine tests added to `python/tests` (`test_dropin_offline.py`, `streaming_listener_cases.py`). Eight fail without the fix; the ninth, the multi-threaded test of the progress handlers, only fails without the GIL, and the `__del__` test covers that fix deterministically. The existing listener tests that read `_rust_listener_ids` now assert on `spark.streams._sqlb._listener_bus`. `test_dropin_offline.py` and `test_ml_offline.py` pass (1934 passed, 8 skipped), as do `cargo fmt --all --check`, `ruff check` and `ruff format --check`. The multi-threaded tests were run on a GIL build of Python 3.11 only. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Fable 5.2) 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]
