@devries-cameron20 / t3code Mirror · read-only / commits / 2b83dd0969
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 11:00 UTC

fix(server): finish closed terminal kills before shutdown (#37113)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 15, 2026 at 10:26 UTC 2b83dd0969f40be89311ad5394da49a4d951ac6e
2 files changed +34 -6
apps/server/src/terminal/Manager.test.ts
+24
@@ -2583,6 +2583,30 @@ it.layer(
2583 }),
2584 );
2585
2586 + it.effect("force-kills a closed terminal that ignores SIGTERM when the manager shuts down", () =>
2587 + Effect.gen(function* () {
2588 + const scope = yield* Scope.make();
2589 + const { manager, ptyAdapter } = yield* createManager(5, {
2590 + processKillGraceMs: 1_000,
2591 + }).pipe(Effect.provideService(Scope.Scope, scope));
2592 + yield* manager.open(openInput());
2593 + const process = ptyAdapter.processes[0]!;
2594 + const sigterm = yield* Deferred.make<void>();
2595 + process.onKill = (signal) => {
2596 + if (signal === "SIGTERM") Deferred.doneUnsafe(sigterm, Effect.void);
2597 + };
2598 + yield* manager.close({ threadId: "thread-1" });
2599 + yield* Deferred.await(sigterm);
2600 +
2601 + const shutdown = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild);
2602 + yield* TestClock.adjust("1 second");
2603 + yield* Fiber.join(shutdown);
2604 +
2605 + expect(process.killSignals).toEqual(["SIGTERM", "SIGKILL"]);
2606 + expect(process.exitListenerCount).toBe(0);
2607 + }).pipe(Effect.provide(TestClock.layer())),
2608 + );
2609 +
2610 it.effect("attaches to a running provider terminal without resolving the provider again", () =>
2611 Effect.gen(function* () {
2612 const providerInstanceId = ProviderInstanceId.make("codex_work");
apps/server/src/terminal/Manager.ts
+10 -6
@@ -3070,10 +3070,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
3070
3071 yield* Effect.addFinalizer(() =>
3072 Effect.gen(function* () {
3073 - const sessions = yield* modifyManagerState(
3073 + const [sessions, pendingKills] = yield* modifyManagerState(
3074 (state) =>
3075 [
3076 - [...state.sessions.values()],
3076 + [[...state.sessions.values()], [...state.killFibers.values()]],
3077 {
3078 ...state,
3079 sessions: new Map(),
@@ -3090,10 +3090,14 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
3090 yield* runKillEscalation(session.process, session.threadId, session.terminalId);
3091 });
3092
3093 - yield* Effect.forEach(sessions, cleanupSession, {
3094 - concurrency: "unbounded",
3095 - discard: true,
3096 - });
3093 + // Closing workerScope next would interrupt escalations of already stopped terminals.
3094 + yield* Effect.all(
3095 + [
3096 + Effect.forEach(sessions, cleanupSession, { concurrency: "unbounded", discard: true }),
3097 + Fiber.awaitAll(pendingKills),
3098 + ],
3099 + { concurrency: "unbounded", discard: true },
3100 + );
3101 }).pipe(Effect.ignoreCause({ log: true })),
3102 );
3103