Skip to content

utils/shutdown.ts createShutdownHandler never clears its forceExit timer if server.close's callback throws #118

Description

@Jagadeeshftw

📌 Description

In createShutdownHandler, clearTimeout(forceExit) only runs inside server.close's callback, after which exit(...) is called. If the callback body were to throw before reaching clearTimeout (or if exit itself throws, e.g. a test's injected exit mock), the forceExit timer is left armed and will still fire exit(1) later — potentially force-killing a process that already exited cleanly, or double-calling exit.

🧩 Requirements and context

  • Move clearTimeout(forceExit) to run unconditionally before any code that could throw, or wrap the callback body in a try/finally.
  • Ensure exit is still called with the correct code (0 on success, 1 on error) after the timer is safely cleared.
  • Do not change the default timeoutMs (10s) or the public ShutdownOptions signature.

🛠️ Suggested execution

  • Modify src/utils/shutdown.ts's createShutdownHandler to guard the server.close callback with try/finally around clearTimeout.
  • Add a regression test in src/utils/shutdown.test.ts using a fake exit that throws on its first call, asserting the forceExit timer doesn't later fire a second exit call.

✅ Acceptance criteria

  • forceExit is always cleared once server.close's callback runs, even if subsequent code throws.
  • New regression test passes; existing shutdown tests are unaffected.

🔒 Security notes

Low risk operational robustness fix — prevents a delayed, unintended forced-exit call after a clean shutdown already completed.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Labels

GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial Campaign | FWC26GrantFox official campaign issuebackendBackend service workbugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions