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
Remove() can race with a concurrent AddOrReplace() on the same persona, leaving a "removed" credential fully live in memory #159
CredentialCache.cs implements AddOrReplace and Remove as two independent, non-atomic operations against the same two pieces of state (the in-memory _credentials map and Store):
// AddOrReplace, lines 149-157: store first, then cachepublicvoidAddOrReplace(PersonaGUIDpersona,Credentialcredential){...Store.Save(persona,credential);_credentials[persona]=credential;}// Remove, lines 163-171: cache first, then storepublicboolRemove(PersonaGUIDpersona){...
bool removedInMemory=_credentials.TryRemove(persona,out_);boolremovedFromStore=Store.Remove(persona);returnremovedInMemory||removedFromStore;}
AddOrReplace deliberately persists before caching (per its own doc comment, and per the fix in #145). Remove does the opposite order — cache first, then store — and the two operations are not synchronized against each other.
Concrete failure scenario
Thread A calls Remove(persona). It completes _credentials.TryRemove(persona, out _) (succeeds) and is preempted before calling Store.Remove(persona).
Thread B calls AddOrReplace(persona, newCredential) for the same persona. It writes newCredential to Store, then to _credentials.
Thread A resumes and calls Store.Remove(persona), deleting the credential Thread B just persisted.
End state: _credentials[persona] == newCredential (fully readable via TryGet, since it hits the in-memory dictionary before ever touching Store), but the backing store has nothing for that persona. Remove() returns true, signaling a clean removal, while the credential remains live and usable in memory until the process exits. For a caller using Remove as a logout/revoke primitive, this means a "revoked" credential keeps working — exactly the state Remove's own doc comment ("Removes ... from both the in-memory cache and the backing store") promises can't happen.
The existing concurrency test (CredentialCacheTests.cs, CredentialCacheIsThreadSafeUnderConcurrentAccess) doesn't catch this because every thread operates on its own unique PersonaGUID — Remove and AddOrReplace are never interleaved on the same key.
Suggested fix
Either:
Make Remove store-first to mirror AddOrReplace's persist-then-cache invariant (Store.Remove before _credentials.TryRemove), which narrows but doesn't fully close the window, or
Serialize mutation per persona (e.g. a per-key lock, or route both methods through a single lock/mutex keyed on persona) so AddOrReplace and Remove for the same persona can never interleave.
Add a regression test that runs AddOrReplace and Remove concurrently against the samePersonaGUID and asserts the final state is consistent (either present in both cache and store, or absent from both) rather than only testing disjoint keys.
Activity