fix(file-store): add write lock to prevent concurrent save race condition#1
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an in-process write serialization mechanism to FileTokenStore to mitigate concurrent saveToken / deleteToken races when persisting tokens to disk.
Changes:
- Introduce a
writeLockpromise chain inFileTokenStore. - Add a
withLock()helper and wrapsaveToken()/deleteToken()operations with it.
Comment on lines
36
to
37
| const tokens = await this.read(); | ||
| return tokens.find((t) => t.clientId === clientId && t.orgId === orgId) ?? null; |
There was a problem hiding this comment.
The new write lock only serializes save/delete, but findToken still reads the file concurrently with writeFile. Since writeFile truncates and rewrites the JSON, a concurrent read can observe partial contents and JSON.parse will throw. Consider either (a) using the same lock for findToken/read operations, or (b) making writes atomic (write to a temp file and rename) so reads are always valid JSON.
Suggested change
| const tokens = await this.read(); | |
| return tokens.find((t) => t.clientId === clientId && t.orgId === orgId) ?? null; | |
| return this.withLock(async () => { | |
| const tokens = await this.read(); | |
| return tokens.find((t) => t.clientId === clientId && t.orgId === orgId) ?? null; | |
| }); |
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
FileTokenStorehad a race condition where concurrentsaveTokenordeleteTokencalls could interleave their read-mutate-write cycles, causing one write to clobber another.Fixed by introducing a promise-based write lock via
withLock, which serializes all mutating operations without any external dependencies. Read operations are unaffected.Also tightened up
findTokenin bothFileTokenStoreandRedisTokenStoreto checkexpiresAtand returnnullfor expired tokens rather than leaking stale data to the OAuth layer.