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

   ### What changes were proposed in this pull request?
   
   A DataFrame, a Column and the other objects that hold a plan or an 
expression reported its Python objects to the garbage collector only when they 
alone held them. Those objects are the functions of UDFs, the plans of the 
Python client, the plan of an error and the Observations. When two holders 
shared one Rust value, neither reported them, so a reference cycle that kept 
two holders was never collected. Examples are a Column and its DataFrame, a 
DataFrame and one derived from it, and a DataFrame and a Column of its 
subquery. PySpark collects such a cycle, because each of its holders is a 
Python object with a reference of its own.
   
   This PR makes the collector see the plans and expressions that holders 
share, as it sees PySpark's plan objects:
   - **`SharedPart`.** Each part of a plan or an expression that holds a Python 
object, directly or below it, gets one Python object, its `SharedPart`, which 
mirrors the part's Python references. Each holder of a reference to the part 
holds a reference to its `SharedPart` and reports it in `__traverse__`.
     - A `SharedPart` reports its references only while every reference to its 
part comes from a holder that reports it. So the collector never frees an 
object that an unreported holder, such as a Rust structure, still reaches.
     - The core keeps account of the references to each part 
(`spark_connect::gc`, `Part::held`). Each holder class keeps a 
`crate::gc::Parts` of its value, reports it, and releases it in `__clear__` and 
before it replaces the value.
   - **Calls without the GIL.** The collector must get the same references of 
an object in each of its passes, and a call that runs without the GIL may clone 
and drop parts meanwhile. So every release of the GIL now goes through 
`crate::gc`, and none of the parts that the call may clone or drop reports a 
reference while it runs:
     - A call over the holders that it names, such as a request of a DataFrame, 
makes only their parts busy (`interrupt::interruptible_of`, `gc::in_use`).
     - A call over values of the core, such as an ML fit's DataFrame or a 
writer's state, makes busy what those values reach (`interruptible_of_values`, 
`in_use_of_values`, `detach_of_values`).
     - A call that clones and drops no part, of sessions, identifiers, 
protobufs and bytes, counts nothing (`detach_without_parts`, 
`interruptible_without_parts`).
     - Only the events that a listener of the Rust API gets make every part 
busy, as their code is unknown to the binding.
     - A new `crates/pyspark-rs/clippy.toml` disallows `Python::detach` and the 
C functions that release the GIL.
     - In the child of a fork, a fork handler keeps only the counts of the 
forking thread.
   - **Free-threaded CPython.** Parts get no `SharedPart` there, as finding a 
`SharedPart` again by its part's address needs the GIL. A new `build.rs` gets 
the `Py_GIL_DISABLED` cfg from `pyo3-build-config`, which is PyO3's own build 
dependency, so no new crate enters the build.
   - **Plans instead of DataFrames.** A stream writer, `copy.copy(df)` and 
`DataFrame(df._plan, session)` hold the plan of `df` rather than `df`, as 
PySpark's hold the plan object. So `df` is freed at its last reference.
   - **Docs.** The "Garbage collection" row of 
`dev/parity/known-differences.md` and its paragraph in `docs/compatibility.md` 
now state what remains, and `AGENTS.md` states the rules for new holders and 
new calls without the GIL. What remains:
     - The native classes of `pyspark.ml.connect` report none of the Python 
objects that they hold in Rust. So a model in a cycle with its summary 
(`model.summary.model = model`), or with a function of a UDF of its summary's 
dataset, is never freed, nor its object in the server's ML cache.
     - Collections during a call without the GIL, as above.
     - Free-threaded builds, as above.
     - The attributes of a session's clients.
   - **Test-only checks.**
     - The core tells the binding of each new reference to a part, and the 
`conftest.py` of the tests fails a test that clones a part without the GIL 
outside a counted call (`crate::gc::check_clone`).
     - An offline test also watches the reported parts while the main APIs run 
(`crate::gc::_watch_reported_parts`).
   
   ### Why are the changes needed?
   
   To close the "Garbage collection" row of the known differences: these 
reference cycles leaked in the drop-in where PySpark collects them.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - Reference cycles through objects that share a plan or an expression are 
collected, as in PySpark.
   - A DataFrame is freed at its last reference when only its stream writer, a 
shallow copy or a DataFrame of its plan is left.
   - `gc.get_referents` of these objects gives their `SharedPart`s.
   
   ### How was this patch tested?
   
   - **New tests.**
     - 9 offline tests in `test_dropin_offline.py` cover the collected cycle 
shapes, the references kept alive while one holder lives, references reported 
once, and holders made and freed on threads while the collector runs. They also 
cover requests that build plans without the GIL, writer copies, and the clone 
check over the main APIs.
     - 1 test in `test_ml_offline.py` covers a model that keeps its dataset.
     - 14 Rust tests cover deep plans, unwinding, forks, finalizers during a 
`SharedPart`'s deallocation, the busy marks and the completeness of the 
references.
   - **Probes against PySpark 4.3.0.** 17 of 17 cycle shapes match PySpark's 
collection, against 5 of 17 on master, and 5 of 5 copy shapes are freed. 
Refcounts and RSS stay flat over 10^5 iterations.
   - **Stress.** A multithreaded stress ran with the clone check and the 
watcher on, under `-X dev`: 300 s on CPython 3.11, and 150 s on 3.12 and 3.13. 
It found 0 violations and collected every cycle.
   - **Review.** Independent reviews of each round of the change.
   - **Performance at 10^5 objects.**
   
     | Case | Before | After |
     |---|---|---|
     | Plain DataFrame | 0.82 µs | 1.35 µs |
     | DataFrame over a UDF plan | 0.82 µs | 1.71 µs |
     | Memory of 10^5 DataFrames, each with its own UDF | 111 MB | 150 MB |
   
     `gc.collect()` takes the same time or less.
   - **The usual checks:**
     - the core tests (release, debug and wasm), the binding's own tests, 
clippy, rustdoc and rustfmt;
     - the native server's crates and the license checks;
     - the tooling tests;
     - the offline suite under coverage;
     - the e2e and ML tests with and without protobuf.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code
   
   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