-
-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Fix: bug(gateguard): race condition in session state read-modify-write path #1442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,14 +76,37 @@ function pruneCheckedEntries(checked) { | |
|
|
||
| function saveState(state) { | ||
| try { | ||
| state.last_active = Date.now(); | ||
| state.checked = pruneCheckedEntries(state.checked); | ||
| fs.mkdirSync(STATE_DIR, { recursive: true }); | ||
| // Atomic write: temp file + rename prevents partial reads | ||
| const tmpFile = STATE_FILE + '.tmp.' + process.pid; | ||
| fs.writeFileSync(tmpFile, JSON.stringify(state, null, 2), 'utf8'); | ||
|
|
||
| // 1. Prepare base state from what's currently in memory | ||
| let mergedChecked = state.checked || []; | ||
| let mergedLastActive = state.last_active || 0; | ||
|
|
||
| // 2. Merge with current disk state to prevent overwriting concurrent updates (Option 2: Merge-style saves) | ||
| try { | ||
| if (fs.existsSync(STATE_FILE)) { | ||
| const disk = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8')); | ||
| if (Array.isArray(disk.checked)) { | ||
| // Union of checked items, preserving order (disk items first, then memory items) | ||
| mergedChecked = Array.from(new Set([...disk.checked, ...mergedChecked])); | ||
| } | ||
| if (typeof disk.last_active === 'number') { | ||
| mergedLastActive = Math.max(mergedLastActive, disk.last_active); | ||
| } | ||
| } | ||
| } catch (_) { /* ignore disk read errors, proceed with memory state */ } | ||
|
|
||
| // 3. Finalize state: update heartbeat and prune to bounded size | ||
| const finalState = { | ||
| checked: pruneCheckedEntries(mergedChecked), | ||
| last_active: Math.max(mergedLastActive, Date.now()) | ||
| }; | ||
|
|
||
| // 4. Atomic write: temp file + rename prevents partial reads | ||
| const tmpFile = STATE_FILE + '.tmp.' + process.pid + '.' + Math.random().toString(36).slice(2, 6); | ||
| fs.writeFileSync(tmpFile, JSON.stringify(finalState, null, 2), 'utf8'); | ||
| fs.renameSync(tmpFile, STATE_FILE); | ||
|
Comment on lines
+86
to
108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The merge read ( Concrete failure path:
The window is now the disk I/O span of lines 87–97 rather than the entire |
||
| } catch (_) { /* ignore */ } | ||
| } catch (_) { /* ignore all errors to ensure hook never blocks */ } | ||
| } | ||
|
|
||
| function markChecked(key) { | ||
|
|
@@ -262,4 +285,4 @@ function run(rawInput) { | |
| return rawInput; // allow | ||
| } | ||
|
|
||
| module.exports = { run }; | ||
| module.exports = { run }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Concurrent saveState calls can still lose updates because merge-style read-modify-write is not synchronized across processes.
Prompt for AI agents