Repository navigation
Release the thread store when its root scope ends - #72
Merged
Merged
Conversation
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
5 open issues
pthread_setspecificcan fail (non-zero return). If it fails here, the TSD slot may still point to… · New BecausebytesInUse()is process-wide and tests run in parallel,bytesInUse() - beforecan go… · New BecausebytesInUse()is process-wide and tests run in parallel,bytesInUse() - beforecan go… · Newnonisolated(unsafe)here is bypassing safety checks, andgrowthis being mutated from a… · New BecausebytesInUse()is process-wide and tests run in parallel,bytesInUse() - beforecan go… · New
What changed in this PR
Fixes a leak where thread-specific RawStore instances were retained beyond the end of root scopes on non-task threads by ensuring clearTSD() releases the stored value.
Changes:
- Update
RawStore.clearTSD()to remove the TSD value and release the retainedRawStore. - Add a Darwin-only regression test that checks memory growth after ending many root scopes on a separate thread.
| File | Description |
|---|---|
| Tests/ImplicitsTests/ImplicitTests.swift | Adds a regression test that detects leaked per-thread stores via process memory statistics. |
| Sources/Implicits/RawStore.swift | Ensures clearTSD() releases the RawStore that was retained in thread-specific data. |
Review effort: Lite
Comment on lines
201
to
207
| private static func clearTSD() { | ||
| guard let ptr = pthread_getspecific(key) else { | ||
| return | ||
| } | ||
| pthread_setspecific(key, nil) | ||
| Unmanaged<RawStore>.fromOpaque(ptr).release() | ||
| } |
Comment on lines
+266
to
+267
| // Byte counts are process-wide and tests run in parallel, so the smallest growth | ||
| // of several attempts is the one least disturbed by other tests. |
| for _ in 0..<10000 { | ||
| rootScope() | ||
| } | ||
| growth = min(growth, bytesInUse() - before) |
Comment on lines
+268
to
+282
| nonisolated(unsafe) var growth = Int.max | ||
| let finished = DispatchSemaphore(value: 0) | ||
| let thread = Thread { | ||
| for _ in 0..<5 { | ||
| let before = bytesInUse() | ||
| for _ in 0..<10000 { | ||
| rootScope() | ||
| } | ||
| growth = min(growth, bytesInUse() - before) | ||
| } | ||
| finished.signal() | ||
| } | ||
| thread.start() | ||
| finished.wait() | ||
|
|
| finished.wait() | ||
|
|
||
| // A store left behind is over 100 bytes: more than a megabyte for these scopes. | ||
| #expect(growth < 256 * 1024) |
zienag
force-pushed
the
thread-store-leak
branch
5 times, most recently
from
October 26, 2026 17:02
abae482 to
311c924
Compare
zienag
force-pushed
the
thread-store-leak
branch
from
October 26, 2026 17:08
311c924 to
c8d6ca0
Compare
zienag
enabled auto-merge (squash)
October 26, 2026 17:12
Copilot stopped reviewing on behalf of
zienag due to an error
October 26, 2026 18:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Every root scope that ended on a thread outside a task leaked its store.
setToTSDkeeps the store in thread-specific data with a retain, butclearTSDonly set the slot to nil and never released it. The key's destructor would release it, but it runs only for a value still set when the thread exits, so it never did. Code that opens top-level scopes on the main thread lost a store, about 256 bytes, each time.CI now runs the tests under LeakSanitizer, which catches this leak and any later one.
Changes
clearTSD()takes the store out of thread-specific data and releases it.pthread_setspecificstops the process with its error code, as a failedpthread_key_createalready does: either error means a corrupted key or no memory, and continuing would leave the store's ownership unknown.--sanitize=addresson Linux, once: the build step builds the tests too, and the test step skips the build. LeakSanitizer fails the run on memory nothing references at exit.--disable-xctest: the tests use Swift Testing only, and the XCTest runner found no tests.Tests/lsan-suppressions.txtsuppresses what LeakSanitizer reports inside Swift's regex libraries, about 28 KB in every run, none of it from this package.AGENTS.mdsays how CI checks for leaks.Test plan
In a
swift:6.2Linux container, running the two CI steps as written:RawStore.onRootScopeCreation. Existing tests that open root scopes on threads catch it.