HyukjinKwon opened a new pull request, #838:
URL: https://github.com/apache/spark-connect-rust/pull/838
### What changes were proposed in this pull request?
A follow-up of #835 (SPARK-61971), from a review of the last 100 commits
against pyspark-client 4.3.0.
#835 made the native objects that hold the UDF functions or the DataFrame of
a plan report them to Python's garbage collector, as PySpark's plain Python
objects are seen by it. Three more native objects hold Python objects that the
collector does not see. A reference cycle through them is never collected,
where PySpark's is.
**1. The rows of `toLocalIterator()`.**
- The native rows that the generator of `DataFrame.toLocalIterator()` yields
from (`PyLocalRowIterator`, `crates/pyspark-rs/src/dataframe.rs`) held a clone
of the plan of the DataFrame, not the DataFrame.
- PySpark's `toLocalIterator` is a generator whose frame holds `self` until
it ends (`connect/dataframe.py:2223-2241`).
- So:
- a cycle through the generator and a function of a UDF of the plan, such
as a bound method of an object that keeps the generator, was never freed;
- while the generator lived, the DataFrame did not report its own plan
either, as the clone was not counted by its `crate::gc::Holders`;
- the DataFrame was freed while its generator, not started or not ended,
lived, where PySpark keeps it until the generator ends.
- The rows now hold the DataFrame and a `crate::gc::Share` of the holders of
its plan (`PyDataFrame::plan_share`), as grouped data and the writers do since
#835, and report the DataFrame in `__traverse__`. `__clear__` drops their clone
of the plan.
- The DataFrame now lives until the generator ends, is closed or is freed,
as PySpark's.
**2. `spark.dataSource`.** `DataSourceRegistration` (`datasource.rs`) holds
its session, as PySpark's `DataSourceRegistration(self)` does
(`connect/session.py:1089-1092`), and now reports it. So a session that keeps
its `dataSource`, or a copy or a new registration of it, is freed by the
collector.
**3. The session builder.**
- The builder of `pyspark.sql.connect.session.SparkSession` (`session.rs`)
holds its channel builder and its hook factories, as PySpark's builder holds
`_channel_builder` and `_hook_factories` (`connect/session.py:131-132, 189,
196`).
- It now reports them, and drops them in `__clear__`.
- A copy of the builder shares them, so one that a copy shares is reported
by neither, and no reference is counted twice.
Each new `__traverse__` of a class with a `__dict__` checks
`crate::gc::GcReady` first, as #809 (SPARK-61928) requires. The builder is
`Clone`, so `GcReady` gets a `Clone` of a written value. The rows have no
`__dict__`, so no collection can meet them before their value is written.
`docs/compatibility.md` and `dev/parity/known-differences.md` name these
with the other objects that report to the collector. The known differences also
say that a channel builder or a hook factory that a copy of the builder shares
is not reported.
### Why are the changes needed?
So that these objects, and what they keep alive (a session and its client,
the functions of UDFs and what those hold), are freed as in PySpark.
Long-running programs then do not leak them. A program that keeps the generator
of `toLocalIterator` also keeps its DataFrame, as on PySpark.
### Does this PR introduce _any_ user-facing change?
Yes:
- reference cycles through the generator of `toLocalIterator`, a
`DataSourceRegistration` and the connect session builder are collected;
- the generator of `toLocalIterator` keeps its DataFrame alive until it
ends, as PySpark's. Before, the DataFrame was freed with its last reference
even while its generator lived.
### How was this patch tested?
- **New tests in `python/tests/test_dropin_offline.py`, against the mock
server.** The first three fail on master and pass here:
- `test_cycles_through_the_functions_of_udfs_are_collected_as_pyspark`: a
cycle through the generator of `toLocalIterator` of a UDF frame; a function
shared with a live frame is kept and then collected; a cycle through the frame
and its generator.
-
`test_cycles_through_the_dataframes_of_their_objects_are_collected_as_pyspark`:
a DataFrame that keeps its generator is collected; the generator keeps its
DataFrame alive and releases it when it is closed.
-
`test_cycles_through_the_sessions_and_builders_of_their_objects_are_collected_as_pyspark`,
new: a new session that keeps its `dataSource`, a copy of it or
`DataSourceRegistration(session)`; a channel builder or a hook factory that
keeps its builder.
- `test_objects_traversed_while_they_are_created_do_not_crash` gets 7
makers: `dataSource` and its copy and constructor, the builder of
`SparkSession.builder` and of `Builder()`, a copy of a builder with a channel
builder and a hook, and `toLocalIterator()`.
- **Probes of both clients against a Spark 4.3.0 server.** These cases now
match pyspark-client 4.3.0, where master leaked all of them:
- a cycle through `toLocalIterator()` of a UDF frame, not started,
started, and with its frame;
- a new session that keeps its `dataSource`;
- a channel builder and a hook factory that keep their builder.
- **Lifetime probe.** The DataFrame of a generator that is not started, or
is started, lives; that of a generator that is exhausted or closed is freed.
Both clients give the same four results, where master freed it in all four.
- **Soundness probe.** A reachable generator in a cycle with the UDF
function of its plan iterates `[10, 11, 12]` after collections, started or not,
as on PySpark.
- **The Rust gates:**
- `cargo fmt --all --check`;
- `cargo +1.100.0 clippy -p apache-spark-connect-core -p
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect
-p apache-spark-connect-core`;
- `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it: 13
passed.
- **The Python gates:**
- ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and
`scripts/audit_no_stubs.sh`;
- `gen_type_stubs.py --check` and `gen_fn_dispatch.py --check` against the
v4.3.0 source;
- the offline, e2e and live Rust test guards;
- `test_type_stubs_offline.py` with mypy: 7 passed. The 2 pyright cases
are skipped, as pyright is not installed here.
- **The offline suite of `coverage.yml`:** 6328 passed and 77 skipped. The
skips: 68 "not a public constructor", 8 that need `SPARK_REMOTE`, and
`test_type_stubs_offline.py`, which needs mypy and ran above.
- **`test_dropin_e2e.py` against a Spark 4.3.0 server:** 318 passed.
### 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]