kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

fix(kap-server): make boot and telemetry shutdown lifecycle complete

Open Correctover opened this issue 4 months ago • 1 comments

Summary

Fixes #2247. Defines and enforces explicit shutdown ordering in kap-server so telemetry producers are quiesced before the final flush boundary.

Changes

Shutdown ordering (close())

  1. Stop HTTP/WS intake — app.close() drains in-flight requests and rejects new connections.
  2. Quiesce telemetry producers — resolve ISessionLifecycleService and close every active session. Each session close stops its agents, which stops emitting lifecycle events. This guarantees no telemetry event is produced after the final flush boundary.
  3. Bounded telemetry flush — shutdownServerTelemetry runs within its deadline; failures are logged but never block core cleanup.
  4. Dispose core scope + release instance registration — the existing best-effort rollback handles partial-boot scenarios.

Partial-boot rollback

  • The existing listen-failure path (try/catch around listenWithPortRetry) already calls close(), which now drains whatever was successfully initialized (sessions, telemetry, core) and releases the instance registration.
  • Session draining is wrapped in try/catch so it does not fail in partial-init scenarios where ISessionLifecycleService is not yet available.

Invariants preserved

  • Telemetry failure never blocks core/instance cleanup.
  • Normal close and partial-boot rollback are idempotent.
  • No change to public CLI shutdown policy.

Correctover avatar Jul 27 '26 10:07 Correctover

🦋 Changeset detected

Latest commit: b62f076faee869dac0b3149a07d2b79932cd7e2b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kap-server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jul 27 '26 10:07 changeset-bot[bot]