codex-multi-auth icon indicating copy to clipboard operation
codex-multi-auth copied to clipboard

Refactor shutdown semantics: ordering, idempotence, and signal contract (LIB-HIGH-003)

Open ndycode opened this issue 7 months ago • 0 comments

Summary

Deep audit finding LIB-HIGH-003 is real on main, but it does not admit a safe surgical fix.

lib/shutdown.ts currently has three intertwined concerns:

  1. Cleanup ordering — handlers run in registration order (FIFO), but the audit questioned whether teardown should be LIFO to match resource-acquisition order.
  2. Idempotence / concurrent calls — runCleanup() has no in-flight promise guard, so overlapping calls can race.
  3. Signal / beforeExit integration — SIGINT, SIGTERM, and beforeExit all call into cleanup with slightly different semantics (beforeExit fire-and-forgets, signals call process.exit(0) after cleanup).

Why this is being filed instead of another quick PR

I attempted the “obvious” fix in a dedicated worktree:

  • add a shared in-flight cleanupPromise
  • switch cleanup order to LIFO
  • keep beforeExit as best-effort fire-and-forget
  • add tests for idempotence + reverse-order semantics

That patch caused 6 of 12 test/shutdown.test.ts cases to fail.

This tells us the current behavior is more than a bug — it is an implicit contract already encoded in the test suite and module-loading behavior.

Evidence from failed fix attempt

When trying to change shutdown semantics, these tests broke:

  • returns the same promise for concurrent runCleanup calls
  • unregister is no-op for non-registered function
  • SIGINT handler runs cleanup and exits with code 0
  • SIGTERM handler runs cleanup and exits with code 0
  • beforeExit handler runs cleanup without calling exit
  • signal handlers are only registered once

This means any proper fix must first decide the intended contract, then update both implementation and tests consistently.

Desired follow-up work

A dedicated refactor/design PR should explicitly define and document:

1. Cleanup order

Pick one and document it:

  • FIFO (current behavior)
  • LIFO (resource-stack semantics; probably more correct)

2. Idempotence

Define whether:

  • concurrent runCleanup() calls must share one in-flight promise
  • repeated calls after completion are no-ops
  • signal-triggered cleanup and manual cleanup should share state

3. Signal semantics

Define precise behavior for:

  • SIGINT
  • SIGTERM
  • beforeExit

In particular:

  • should beforeExit be best-effort only?
  • should signal handlers always call process.exit(0)?
  • should cleanup errors remain swallowed, or be logged?

4. Test contract

Update test/shutdown.test.ts to match the chosen semantics rather than relying on accidental module-state behavior.

Suggested acceptance criteria

  • [ ] lib/shutdown.ts has a documented contract for ordering + idempotence + signal semantics
  • [ ] tests explicitly encode that contract
  • [ ] no flaky timing dependencies in signal tests
  • [ ] no duplicate cleanup execution under concurrent calls
  • [ ] no regression in existing shutdown callers

Context

  • This finding came from .sisyphus/notepads/deep-audit/reports/lib-toplevel.json
  • It was validated on current main
  • It remains the main unresolved HIGH finding after the serial fix run that produced PRs #414–#429

Recommendation

Treat this as a dedicated refactor issue, not a “quick fix” PR.

ndycode avatar Apr 18 '26 07:04 ndycode