Fix UAF in adapter lifecycle: serialize Adapters ops with a mutex - #2274
Merged
Conversation
The Adapter::ref_count_ field was a plain int32_t modified without
synchronization, and Adapters::UnloadAdapter() performed a check-then-erase
on the underlying std::unordered_map without a lock. Concurrent
OgaSetActiveAdapter / OgaUnloadAdapter calls could therefore:
* race on ref_count_ (data race, undefined behavior, lost updates), and
* pass the RefCount() > 0 guard in UnloadAdapter while another thread
was mid-AcquireRef(), destroying the adapter while a reference was
still held (use-after-free).
Add a mutex to Adapters and take it in Load/Unload/Acquire/ReleaseAdapter.
Since AcquireRef/ReleaseRef/RefCount on Adapter are only reachable through
these Adapters entry points, the single mutex now serializes both map
mutation and per-adapter ref counting, closing the TOCTOU window and the
data race in one place. ReleaseRef also restores the counter before
throwing so a caught exception does not leave it in a negative state.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency use-after-free hazard in the LoRA adapter lifecycle by serializing all adapter load/unload and ref-count operations behind a single Adapters mutex, preventing races between SetActiveAdapter/acquire and UnloadAdapter/erase.
Changes:
- Add
Adapters::mutex_and document the intended locking/ref-counting invariants. - Guard
LoadAdapter,UnloadAdapter,AcquireAdapter, andReleaseAdapterwithstd::lock_guard<std::mutex>to eliminate the check-then-erase TOCTOU window. - Adjust
Adapter::ReleaseRef()to restore the counter before throwing to avoid leavingref_count_negative after an exception.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/models/adapters.h | Introduces a mutex on Adapters and documents the serialized access contract for adapter ref counting and map mutation. |
| src/models/adapters.cpp | Applies locking to all adapter operations and hardens ReleaseRef() against leaving ref counts negative on exception. |
…ncy regression test - Make Adapter::AcquireRef/ReleaseRef/RefCount private and grant Adapters friend access so all ref-count mutations must go through Adapters::mutex_. This prevents future call sites from silently bypassing the mutex and reintroducing the data race / TOCTOU that motivated this fix. - Add CAPITests.AdaptersConcurrentLoadUnload, a multithreaded regression test that hammers LoadAdapter/UnloadAdapter to guard against future regressions of the race and to give TSAN/ASAN a signal.
apsonawane
enabled auto-merge (squash)
July 7, 2026 22:34
kunal-vaishnavi
approved these changes
Jul 14, 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.
The Adapter::ref_count_ field was a plain int32_t modified without
synchronization, and Adapters::UnloadAdapter() performed a check-then-erase
on the underlying std::unordered_map without a lock. Concurrent
OgaSetActiveAdapter / OgaUnloadAdapter calls could therefore:
race on ref_count_ (data race, undefined behavior, lost updates), and
pass the RefCount() > 0 guard in UnloadAdapter while another thread
was mid-AcquireRef(), destroying the adapter while a reference was
still held (use-after-free).
Add a mutex to Adapters and take it in Load/Unload/Acquire/ReleaseAdapter.
Since AcquireRef/ReleaseRef/RefCount on Adapter are only reachable through
these Adapters entry points, the single mutex now serializes both map
mutation and per-adapter ref counting, closing the TOCTOU window and the
data race in one place. ReleaseRef also restores the counter before
throwing so a caught exception does not leave it in a negative state.