You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f248abb
Browse filesBrowse the repository at this point in the historyBrowse files
Rollup merge of #163799 - ivmarkov:fix-thread-memory-leak, r=joboet
std: keep value of `local_pointer!` until `thread_cleanup` is run
(
**NOTE**: LLM **was** used to write the initial version of these ~26 lines of code, subsequently also edited by me during the review (`LocalPointer::__new` now delegates to the new `LocalPointer::__new_with_dtor`; I did hide the `keep_current` inside the initialization block of `CURRENT`).
)
The PR fixes a memory leak specific for the ESP-IDF target which happens during thread destruction.
The TL;DR is that the current code relies on behavior which is **not** mandated by the POSIX spec, and just happens to work fine on the few major platforms that use that code (bionic, openbsd).
More details [in this comment and the ones that follow](esp-rs/esp-idf#522 (comment)).
Note also that - **according to the LLM** - `glibc` and `musl` would leak too, **if** they were using pthread keys in the first place.
So it is not like ESP-IDF is the outlier here.
In fact, the LLM even suggested `#[cfg(all(target_family = "unix", not(target_thread_local)))]` instead of `#[cfg(target_os = "espidf")]`, but let's take small steps. I can't test on those other platforms anyway, as they are relatively exotic (gnux32, open-harmony - basically `glibc` and `musl` targets without Native-TLS support).
CC @joboet
CC @kyrias
// Test that thread::current is still available in TLS destructors.
355
355
//
356
-
// The test won't currently work without target_thread_local, aka with slow tls.
357
-
// The runtime tries very hard to drop last the TLS variable that keeps the information about the
358
-
// current thread, by using several tricks like deferring the drop to a later round of TLS destruction.
359
-
// However, this only seems to work with fast tls.
360
-
//
361
-
// With slow TLS, it seems that multiple libc implementations will just set the value to null the first
362
-
// time they encounter it, regardless of it having a destructor or not. This means that trying to
363
-
// retrieve it later in a drop impl of another TLS variable will not work.
356
+
// With slow TLS (i.e. without target_thread_local), multiple libc implementations set the value of
357
+
// a key to null the first time they encounter it, regardless of it having a destructor or not. The
358
+
// handle of the current thread is stored in a local pointer, which stays readable until it is
359
+
// dropped in the last round of TLS destruction.
364
360
//
365
361
// ** Apple libc: https://github.com/apple-oss-distributions/libpthread/blob/c032e0b076700a0a47db75528a282b8d3a06531a/src/pthread_tsd.c#L293
366
362
// Sets the variable to null if it has a destructor and the value is not null. However, all variables
367
363
// created with pthread_key_create are marked as having a destructor, even if the fn ptr called with
0 commit comments