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

   ### What changes were proposed in this pull request?
   
   [SPARK-61186](https://issues.apache.org/jira/browse/SPARK-61186): Forget the 
tags and user context extensions of a thread when the thread ends, in the Rust 
and Python clients, as PySpark's thread locals do.
   
   Two maps in the Rust core are keyed by thread id, and neither removed the 
entry of a thread that ended:
   
   - the operation tags of a session (`ThreadTags`, added by SPARK-61166 for 
`inheritable_thread_target`). A thread that shares a copy of the tags leaves an 
entry, even an empty one, when it ends. The copy is shared, so the entry is not 
removed by the thread itself;
   - the user context extensions of a client (`by_thread`, of 
`add_threadlocal_user_context_extension`), which were never removed except by 
the thread that added them.
   
   The new module `spark_connect_core::thread_entries` holds the fix. When a 
thread inserts its entry into a map, `forget_when_thread_ends` registers the 
map with the thread's thread-local state, through a `Weak` reference, so the 
registration does not keep the map alive. When the thread ends, the 
thread-local state drops, and the entry of that thread is removed from every 
registered map that is still alive. Registering again is a no-op, and a 
registration of a dropped map is pruned at the next registration of the thread.
   
   Behavior while a thread lives is unchanged: its tags and extensions are the 
same, a copy shared with other threads is still shared, and a pool thread keeps 
its entry while it lives, as `threading.local()` does. The Python client needs 
no change: it already keeps these in the core.
   
   ### Why are the changes needed?
   
   A program that starts a thread per task, with 
`inheritable_thread_target(session)` or with 
`add_threadlocal_user_context_extension`, grew the maps of the session and the 
client by one entry, about 80 bytes, per finished thread, without bound. 
PySpark's `thread_local` is freed when its thread ends, so official PySpark 
keeps none of these.
   
   The Rust test `finished_threads_leave_no_tags` fails on the code before this 
change: 1000 finished threads that shared an empty copy of the tags left 1000 
entries.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The tags and the user context extensions of a thread are the same while 
it lives, and an ended thread's entries are no longer kept in memory. The new 
module `spark_connect_core::thread_entries` is public, and its 
`forget_when_thread_ends` is for the crate's own maps.
   
   ### How was this patch tested?
   
   - `finished_threads_leave_no_tags` (Rust, `spark-connect`): 1000 finished 
threads that share a copy of the tags and add a tag leave no entry, and the 
main thread has no tags.
   - `a_living_thread_keeps_its_tags_until_it_ends` (Rust, `spark-connect`): a 
thread that shares a copy and adds a tag keeps its entry and its tags while it 
lives, and its entry is gone once it ends; the sharing of 
`threads_given_a_copy_of_tags_share_it` is unchanged.
   - `threadlocal_user_context_extensions_of_finished_threads_are_forgotten` 
and `threadlocal_user_context_extensions_of_a_living_thread_stay_until_it_ends` 
(Rust, `spark-connect-core`): the same for the user context extensions of a 
client.
   - `entries_of_finished_threads_are_removed`, 
`entry_of_a_living_thread_stays_until_it_ends`, 
`registration_does_not_keep_a_map_alive`, 
`the_registration_of_a_dropped_map_is_pruned` (Rust, `spark-connect-core`, 
`thread_entries`).
   - No Python test: the Python client's state is the core's, and the entry of 
an ended thread is not observable from Python. The existing 
`inheritable_thread_target` tests in the offline suite are unchanged and pass.
   - The tests of both crates, e2e included, against a local Spark 4.3.0 server 
(902 passed), the build of all tests, also with `wasm-udf`, the offline Python 
suites (2227 passed, 8 skipped), and the live drop-in, Arrow and ML suites (209 
passed) and live cases (10 passed). rustfmt passes, and clippy reports no 
diagnostics in the changed lines.
   
   ### 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