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

   ### What changes were proposed in this pull request?
   
   A fix of a regression of #792 (SPARK-61881), from a round-3 review against 
pyspark-client 4.3.0.
   
   Since #792, the expressions of a UDF hold its function, not its pickle. So a 
reference cycle through that function and an object that holds it is collected 
only if the object reports the function to Python's garbage collector, and only 
`Column` and `DataFrame` did. In PySpark, these objects are plain Python 
objects that hold the Columns or plans that carry `PythonUDF._func`, so the 
collector frees such cycles. The objects are:
   - GroupedData;
   - cogroups;
   - `asTable()`;
   - WindowSpec;
   - `df.write`, `df.writeTo`, `df.na` and `df.stat`;
   - `mergeInto` and its clauses;
   - `writeStream`.
   
   A probe of `h = Holder(); h.back = build(h); del h; gc.collect()` leaked h 
in 20 of 30 cases, where PySpark collects all 30. The same mechanism leaked the 
DataFrame of such an object (`df.back = df.groupBy("id")`, and likewise of 
write, writeTo, na, stat and cogroup), which predates #792. The helpers held a 
clone of their frame's plan, so the frame stopped reporting the plan's objects 
too.
   
   With this PR:
   - **Each helper reports what it holds** in `__traverse__`, and breaks its 
cycles in `__clear__`, as `Column` and `DataFrame` do:
     - the helpers are grouped data, cogroups, `na`, `stat`, table arguments, 
window specs, the writers, the merge builder and its clauses, and the stream 
writer;
     - each reports its frame (and its `left`, `right` or `writer`) and the UDF 
functions of its own expressions;
     - each `__traverse__` checks `GcReady` first, as #809 (SPARK-61928) 
requires, and every constructor and copy sets it.
   - **A shared plan is reported by the frame, never by a helper.** A DataFrame 
counts the plan clones held by objects that keep it alive (the new 
`crate::gc::Holders` and `Share`). It reports the plan's objects when no other 
holder exists. Otherwise, a helper in a cycle whose frame is still alive could 
let the collector clear objects that the frame still reaches. The core's 
doc-hidden `exclusive_pickler_objects` and `exclusive_relation_owners` take 
that holder count, and the helpers get doc-hidden `shares_plan_with`, 
`clear_plan` and `exclusive_pickler_objects`.
   - **The merge builder holds its DataFrame,** as PySpark's does through its 
execution callback.
   - **The stream writer keeps the pickles** of its `foreachBatch` and 
`foreach` functions, not the functions, as PySpark's 
`WriteStreamOperationStart` does. So `s.w = sdf.writeStream.foreachBatch(s); 
del s` frees `s` by its last reference, as PySpark's does. Its pickle replays 
that state on a server's worker.
   
   `docs/compatibility.md`, `dev/parity/known-differences.md` and the GC rule 
of `AGENTS.md` describe these.
   
   ### Why are the changes needed?
   
   So that the UDF functions of these objects, and everything that they hold, 
such as models or caches, are freed as in PySpark, and long-running programs do 
not leak them.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - reference cycles through these objects are collected;
   - the merge builder keeps its DataFrame alive;
   - a deep copy of a `foreachBatch` writer carries the pickle of the call, as 
PySpark's.
   
   Remaining differences, documented:
   - **Table arguments.** A table argument holds only the plan, as PySpark's 
does, so a cycle through both it and its live DataFrame is not collected.
   - **The stream writer.** It holds its DataFrame, where PySpark's holds only 
the plan, so `df.back = df.writeStream` is freed by the collector, not at its 
last reference.
   
   ### How was this patch tested?
   
   - **New tests in `test_dropin_offline.py`.**
     - 22 new cases in 
`test_cycles_through_the_functions_of_udfs_are_collected_as_pyspark`, with 
checks that functions shared with a live frame are kept, then collected.
     - 
`test_cycles_through_the_dataframes_of_their_objects_are_collected_as_pyspark`, 
with the merge builder's lifetime.
     - `test_stream_writers_keep_the_pickles_of_their_functions_as_pyspark`.
     - 22 new makers in 
`test_objects_traversed_while_they_are_created_do_not_crash`.
     - `test_worker_reconstructors_replay_every_setting_as_pyspark` checks the 
pickles against a real `WriteStreamOperationStart`.
     - A core unit test in `udf.rs`.
   
     4 of the 5 changed tests fail on master.
   - **Probes of both clients:**
     - all 30 cycle cases now match PySpark (master leaked 20);
     - 30 soundness cases keep the job intact while its frame lives, as 
PySpark's;
     - the stream writer is freed by refcount in all 4 cases.
   - **The GC guard.** A mutant whose `GcReady` always reports ready crashes 
the creation repro in 19 of the 23 new kinds. The real build passes all of them.
   - **Rust.**
     - `cargo test -p apache-spark-connect -p apache-spark-connect-core 
--release`, and `--features wasm-udf`;
     - `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
   - **Python.**
     - The offline suite of `coverage.yml` on pandas 2 and 3.
     - Live `test_dropin_e2e.py`.
     - The type-stub drift check and test, with mypy and pyright.
   - **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs 
-p apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0, 
`audit_no_stubs.sh` and `scripts/tests`.
   
   ### 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]

Reply via email to