fix(openscience): atomic + locked session writes, distinguish logout from read error#131
Merged
Merged
Conversation
…from read error - saveSession uses atomic temp+rename so a crash or concurrent reader never sees a torn session file. - updateSession serializes its read-modify-write under a lock, so two concurrent patches (interactive last_check_ts vs background cached_v) can't lose each other's field. - getSession distinguishes a MISSING file (genuine logout → null) from a read/parse error (torn file, EMFILE, permissions): the latter is no longer silently mis-read as 'signed out' (which flipped the billing gate to BYOK and diverted usage to the unauthenticated queue for an authenticated user) — it still returns null so callers stay simple, but logs the failure. (#12)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Audit finding #12 (High). The session file was written with a plain
Bun.write(no temp+rename) andupdateSession's read-modify-write held no lock, so two processes writing it concurrently could tear the file — andgetSessioncoerced every read error (including a torn file / EMFILE / permission blip) tonull, i.e. "signed out." That mis-classification flips the billing gate to BYOK, diverts usage to the unauthenticated queue, and makessyncServicesno-op for a genuinely authenticated user.saveSession→ atomic temp+rename (no torn file for a concurrent reader).updateSession→ serialized underLock.writeso concurrent patches (interactivelast_check_tsvs backgroundcached_v) don't lose each other's field.getSession→ a missing file is a real logout (null, silent); a read/parse error is not — it still returns null so callers stay simple, but now logs the failure instead of silently mis-billing.Tests
test/openscience/session-file.test.ts: null when absent, save→get round-trip, clear→null, and api_key-less session treated as none. Full backend suite (952) green.