Skip to content

fix: tolerate EPERM when unlinking broker teardown files - #650

Open
SomSamantray wants to merge 3 commits into
openai:mainfrom
SomSamantray:fix/broker-teardown-eperm-guard
Open

fix: tolerate EPERM when unlinking broker teardown files#650
SomSamantray wants to merge 3 commits into
openai:mainfrom
SomSamantray:fix/broker-teardown-eperm-guard

Conversation

@SomSamantray

@SomSamantray SomSamantray commented Aug 15, 2026

Copy link
Copy Markdown

Summary

Fixes #633, Fixes #626: teardownBrokerSession and clearBrokerSession can throw when unlinking broker files that another process holds open. On Windows, deleting a locked file fails with EPERM, and because ensureBrokerSession calls teardown on its stale-broker replacement path with no try/catch, the failure propagated up to the caller and the background job was marked failed before any Codex work began.

The three unguarded unlinkSync calls are now wrapped in the same bare try { ... } catch { // Ignore ... } guard the other cleanup steps in the same function already use:

  • teardownBrokerSession: pid-file unlink, log-file unlink
  • clearBrokerSession: broker state-file (broker.json) unlink — same unguarded pattern one call later on the same propagation path, so the same symptom class would otherwise persist

Teardown remains best-effort: a failed unlink never prevents the remaining cleanup steps from running, and success-path behavior is unchanged.

Testing

  • New tests/broker-lifecycle.test.mjs with 6 unit scenarios: happy path, EPERM on pid file (log still removed), EPERM on log file, missing files, remaining-cleanup integration, and EPERM on the broker state file. The four EPERM scenarios failed before the change (red) and pass after.
  • npm test: 97/97 passing.
  • npm run build (prebuild + tsc): clean.

Note: the EPERM failure mode is simulated via an fs.unlinkSync patch, so the test is platform-independent; a manual Windows re-run of the issue's repro is recommended to confirm the reported symptom is gone, but is not blocking.

Security Disclosure

No security-relevant changes.

Agent Disclosure

This change was implemented with the assistance of an AI coding agent (opencode), reviewed and tested by the author before submission.

@SomSamantray
SomSamantray requested a review from a team August 15, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment