Skip to content

fix: use process.exitCode instead of process.exit() so runner finally blocks clean up temp files#49

Merged
shreyas-lyzr merged 1 commit intoopen-gitagent:mainfrom
kevinWangSheng:fix/tmp-cleanup-process-exit
Mar 25, 2026
Merged

fix: use process.exitCode instead of process.exit() so runner finally blocks clean up temp files#49
shreyas-lyzr merged 1 commit intoopen-gitagent:mainfrom
kevinWangSheng:fix/tmp-cleanup-process-exit

Conversation

@kevinWangSheng
Copy link
Contributor

What

Replace process.exit(N) with process.exitCode = N; return inside try/finally blocks in all five runners: claude, openai, crewai, nanobot, openclaw.

Why

process.exit() terminates Node.js immediately without running any pending finally blocks. Every runner in this project follows the same pattern:

try {
  const result = spawnSync(...);
  if (result.error) {
    process.exit(1); // finally SKIPPED
  }
  process.exit(result.status ?? 0); // finally SKIPPED
} finally {
  unlinkSync(tmpFile); // never executes
}

Because the finally block is skipped, temporary files written to /tmp — including system prompt content, agent configs, and API credentials passed via environment — are left on disk indefinitely after every invocation.

Using process.exitCode = N; return lets the finally block execute first, removing all temporary files, and then the process exits naturally with the set exit code.

Affected runners:

  • src/runners/claude.ts — system prompt + hooks settings JSON in /tmp
  • src/runners/openai.ts — generated Python script in /tmp
  • src/runners/crewai.ts — generated YAML config in /tmp
  • src/runners/nanobot.ts — config dir + system prompt file in /tmp
  • src/runners/openclaw.ts — full workspace directory in /tmp

How Tested

  • npm run build passes
  • gitagent validate passes on example agents
  • Manual testing (describe below)

Manually confirmed that after gitagent run completes, no leftover /tmp/gitagent-* files remain.

Checklist

  • My code follows the existing style of this project
  • I have added/updated tests (if applicable)
  • I have updated documentation (if applicable)
  • I have read the CONTRIBUTING.md

… blocks clean up temp files

process.exit() terminates the Node.js process immediately without executing
pending finally blocks. All five runners (claude, openai, crewai, nanobot,
openclaw) write temporary files or directories then call process.exit() inside
a try block, so the finally cleanup is always skipped.

Replace process.exit(N) with process.exitCode = N; return so the finally
block executes and removes temporary files before the process exits naturally.
This prevents system prompt content, agent configs, and API key material
written to /tmp from persisting on disk indefinitely.
@kevinWangSheng kevinWangSheng force-pushed the fix/tmp-cleanup-process-exit branch from d0288e1 to 40c19a2 Compare March 25, 2026 15:42
@shreyas-lyzr shreyas-lyzr merged commit fa163ef into open-gitagent:main Mar 25, 2026
@shreyas-lyzr
Copy link
Contributor

Reviewed and merged. Correct fix — process.exit() skips the event loop and bypasses finally blocks, leaving temp directories behind. process.exitCode + return lets the finally cleanup run before Node exits. Applied consistently across 5 runners.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants