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

   ### What changes were proposed in this pull request?
   
   On CPython 3.11, Python's garbage collector can traverse an object of the 
extension while the object is being created:
   - PyO3 makes an object of a `#[pyclass(dict)]` class, or of a Python 
subclass of one, with `object.__new__`.
   - `object.__new__` tracks the object that it allocates, and then allocates 
its `__dict__` (`_PyObject_InitializeDict`).
   - When the free list of dicts is empty, that allocation can start a 
collection before PyO3 writes the object's value. The collector then calls the 
class's `__traverse__` on zeroed memory.
   
   The `__traverse__` of four classes read their state there:
   - `DataFrameWriter` and `DataFrameWriterV2` read a null `Arc` 
(`Arc::strong_count` of the kept arguments);
   - `DataFrameReader` and `DataStreamReader` read their zeroed `Option` of the 
unsent format as `Some` of a null `Py`, and gave the collector NULL.
   
   So `df.write`, `df.writeTo(...)`, `spark.read`, `spark.readStream`, or a 
copy of one, crashed the interpreter with SIGSEGV when a collection started at 
that point. `PyColumn` and `PyDataFrame` already guarded their `__traverse__` 
with a flag that zeroed memory leaves false, for this reason.
   
   With this PR:
   - a new `crate::gc::GcReady` is that flag: true in each constructed value, 
false in zeroed memory;
   - the writers and readers check it first, and `PyColumn` and `PyDataFrame` 
use it;
   - `Observation` and `MergeIntoWriter` need no flag: they read only 
`Option<Py<_>>` fields, whose zeroed memory is `None`. A comment at each says 
so;
   - `AGENTS.md` states the rule for a new custom `__traverse__`.
   
   ### Why are the changes needed?
   
   A script whose garbage collection starts at that allocation crashed every 
time. For example, with `gc.set_threshold(1)` and the free list of dicts used 
up, `df.write`, `spark.read` and `spark.readStream` crash on master. 
pyspark-client 4.3.0 has no such crash, as its classes are Python classes.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes: these objects no longer crash the interpreter when a collection meets 
them while they are created.
   
   ### How was this patch tested?
   
   - **New test.** `test_objects_traversed_while_they_are_created_do_not_crash` 
in `test_dropin_offline.py`.
     - It sets the collection threshold to 1, uses up the free list of dicts, 
and shifts the allocations before each object by one to four. So a collection 
meets each kind of object while it is created: the writers, the readers, their 
copies, `mergeInto`, `Observation`, a DataFrame and a Column.
     - Without the fix it crashes with rc 139, 3 of 3 times; with the fix it 
passes 3 of 3.
   - **Repro of each class.** `df.write`, `spark.read` and `spark.readStream` 
crash on a build of master. All four creations pass with the fix.
   - **The cause** was found with lldb on CPython 3.11: `object.__new__` → 
`_PyObject_GC_New` of the dict → collection → `PyDataFrameWriter::__traverse__` 
→ `Arc::strong_count` of a null pointer.
   - **Python.** The offline suite of `coverage.yml` on pandas 2 and 3.
   - **Rust.** `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
   - **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs 
--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