Make NetworkingClient.headers thread-safe - #9
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses a production crash caused by concurrent reads/writes to NetworkingClient.headers by making header storage thread-safe and adding regression tests to prevent reintroducing the data race.
Changes:
- Replaced the stored
headersdictionary with a lock-protected private backing store while preserving the existingheadersget/set API surface. - Added
withHeaders(_:)to support atomic read-modify-write updates for callers that need it (e.g., auth header merges). - Added concurrency regression tests that exercise the previously-crashing access patterns.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Sources/Networking/NetworkingClient.swift | Introduces an NSLock-protected backing store for headers and adds withHeaders(_:) for atomic mutations. |
| Tests/NetworkingTests/HeadersThreadSafetyTests.swift | Adds regression tests that stress concurrent reads/writes and validate atomicity/value semantics. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
headers was a plain stored dictionary read on every request build (and on retries from URLSession/Combine threads) while being rewritten on auth token refresh from arbitrary threads. Swift Dictionary is not thread-safe, so this raced and corrupted the CoW buffer, producing the Crashlytics crashes in NetworkingClient.addHeaders/removeHeaders and NetworkingRequest.__ivar_destroyer (EXC_BAD_ACCESS / EXC_BREAKPOINT / SIGABRT). - back headers with an NSLock-protected private store; the public property keeps the exact same get/set surface, so existing consumers compile and behave unchanged - add withHeaders(_:) for callers that need an atomic read-modify-write (e.g. merging auth headers) instead of racy get-modify-set - add regression tests; the concurrent hammer test segfaults (signal 11) against the previous implementation and passes with the lock Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
logansease
force-pushed
the
logan/thread-safe-headers
branch
from
July 22, 2026 18:44
cbfd82c to
c8a6aca
Compare
btresedder
approved these changes
Jul 22, 2026
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.
headers was a plain stored dictionary read on every request build (and on retries from URLSession/Combine threads) while being rewritten on auth token refresh from arbitrary threads. Swift Dictionary is not thread-safe, so this raced and corrupted the CoW buffer, producing the Crashlytics crashes in NetworkingClient.addHeaders/removeHeaders and NetworkingRequest.__ivar_destroyer (EXC_BAD_ACCESS / EXC_BREAKPOINT / SIGABRT).