Skip to content

Define a graceful drain policy for in-flight turns during shutdown #30

Description

@0disoft

Context

Current dev handles process shutdown in src/cli.ts:

  • SIGINT / SIGTERM call a shutdown handler;
  • the handler calls server.stop(true), removes the pid file, restores native Codex when not running as OCX_SERVICE, then exits.

The request paths already have good client-cancel handling in several places (abortSignal, upstream aborts, WebSocket close aborts, etc.). This issue is specifically about the process shutdown path, not normal client disconnects.

Concern

When the proxy is stopped while a long streaming /v1/responses or WebSocket turn is active, it is not obvious what contract users should expect:

  • should opencodex wait briefly for active turns to finish?
  • should it reject new requests but let existing streams drain up to a deadline?
  • should it actively abort in-flight upstream requests and emit/record an incomplete/aborted terminal outcome?
  • should ocx stop behave differently from Ctrl+C or service stop?

Right now the code appears to stop and exit immediately from the CLI shutdown handler. That is simple, but for Codex users a mid-turn stop can look like a random reconnect/stall rather than an intentional proxy shutdown.

Possible approach

A small, bounded policy might be enough:

  1. Track active HTTP/WebSocket turns in the server.
  2. On shutdown, stop accepting new work.
  3. Wait up to a short configurable deadline, e.g. 5-10 seconds, for active streams to finish.
  4. After the deadline, abort remaining upstream controllers and record/log a terminal shutdown/incomplete outcome where possible.
  5. Keep ocx stop --force or service-manager hard kill behavior available for cases where the process must exit immediately.

This does not need to be a big reliability framework; the main value is making shutdown behavior explicit and preventing in-flight turns from being cut off without any diagnostic signal.

Question

Do you think opencodex should support a bounded graceful drain on shutdown, or is immediate shutdown the intended behavior? If you agree with adding a small drain policy, would you prefer to implement it yourself, or should I prepare a focused PR after the current dev merge settles?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions