Skip to content

Commit 05f0d9b

Browse files
fix(terminal): retain and pace slow fallback process queries (pingdotgg#24640)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2ba2426 commit 05f0d9b

2 files changed

Lines changed: 293 additions & 110 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@ import {
1010
TerminalProviderInstanceNotFoundError,
1111
} from "@t3tools/contracts";
1212
import * as HostProcess from "@t3tools/shared/HostProcess";
13+
import * as Clock from "effect/Clock";
1314
import * as Deferred from "effect/Deferred";
15+
import * as Duration from "effect/Duration";
1416
import * as Effect from "effect/Effect";
1517
import * as Base64Url from "effect/encoding/Base64Url";
1618
import * as Exit from "effect/Exit";
1719
import * as Fiber from "effect/Fiber";
1820
import * as FileSystem from "effect/FileSystem";
1921
import * as Layer from "effect/Layer";
22+
import * as Option from "effect/Option";
2023
import * as PlatformError from "effect/PlatformError";
2124
import * as Path from "effect/Path";
25+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
2226
import * as Ref from "effect/Ref";
2327
import * as Schema from "effect/Schema";
2428
import * as Scope from "effect/Scope";
2529
import * as Stream from "effect/Stream";
2630
import * as SynchronizedRef from "effect/SynchronizedRef";
31+
import * as TestClock from "effect/testing/TestClock";
2732
import { expect, vi } from "vite-plus/test";
2833

2934
import * as ProcessRunner from "../processRunner.ts";
@@ -756,6 +761,154 @@ it.layer(
756761
}),
757762
);
758763

764+
// Answers process-table queries after `queryDuration`, honoring the caller's
765+
// timeout the way ProcessRunner's `timedOutResult` mode does.
766+
const slowProcessTable = (
767+
platform: "linux" | "win32",
768+
queryDuration: Duration.Input,
769+
queries: Array<{ start: number; end?: number }>,
770+
started: ReadonlyArray<Deferred.Deferred<void>>,
771+
): ProcessRunner.ProcessRunner["Service"] => ({
772+
run: (input) =>
773+
Effect.gen(function* () {
774+
const query: { start: number; end?: number } = { start: yield* Clock.currentTimeMillis };
775+
queries.push(query);
776+
const deferred = started[queries.length - 1];
777+
if (deferred) yield* Deferred.succeed(deferred, undefined);
778+
const completed = yield* Effect.sleep(queryDuration).pipe(
779+
Effect.timeoutOption(input.timeout ?? "1 hour"),
780+
);
781+
query.end = yield* Clock.currentTimeMillis;
782+
return {
783+
stdout: Option.isNone(completed)
784+
? ""
785+
: platform === "win32"
786+
? "100|9000|node.exe\n"
787+
: " 100 9000 node\n",
788+
stderr: "",
789+
code: Option.isSome(completed) ? ChildProcessSpawner.ExitCode(0) : null,
790+
timedOut: Option.isNone(completed),
791+
stdoutTruncated: false,
792+
stderrTruncated: false,
793+
stdoutInvalidUtf8: false,
794+
stderrInvalidUtf8: false,
795+
};
796+
}),
797+
});
798+
799+
const createFallbackManager = (
800+
platform: "linux" | "win32",
801+
runner: ProcessRunner.ProcessRunner["Service"],
802+
) =>
803+
createManager(5, { subprocessPollIntervalMs: 1_000, processKillGraceMs: 0 }).pipe(
804+
Effect.provideService(ProcessRunner.ProcessRunner, runner),
805+
Effect.provide(layerWithHostPlatform(platform)),
806+
);
807+
808+
it.effect.each(["linux", "win32"] as const)(
809+
"reports activity from a slow but successful %s process-table query",
810+
(platform) =>
811+
Effect.gen(function* () {
812+
const started = yield* Deferred.make<void>();
813+
const queries: Array<{ start: number; end?: number }> = [];
814+
const { manager, getEvents } = yield* createFallbackManager(
815+
platform,
816+
slowProcessTable(platform, "5 seconds", queries, [started]),
817+
);
818+
819+
yield* manager.open(openInput());
820+
yield* TestClock.adjust("1 second");
821+
yield* Deferred.await(started);
822+
yield* TestClock.adjust("5 seconds");
823+
824+
expect(
825+
(yield* getEvents).some(
826+
(event) =>
827+
event.type === "activity" && event.hasRunningSubprocess && event.label === "node",
828+
),
829+
).toBe(true);
830+
yield* manager.close({ threadId: "thread-1" });
831+
}).pipe(Effect.provide(TestClock.layer())),
832+
);
833+
834+
it.effect("idles at least as long as a slow process-table query took before the next", () =>
835+
Effect.gen(function* () {
836+
const started = yield* Deferred.make<void>();
837+
const queries: Array<{ start: number; end?: number }> = [];
838+
const { manager } = yield* createFallbackManager(
839+
"linux",
840+
slowProcessTable("linux", "5 seconds", queries, [started]),
841+
);
842+
843+
yield* manager.open(openInput());
844+
yield* TestClock.adjust("1 second");
845+
yield* Deferred.await(started);
846+
yield* TestClock.adjust("5 seconds");
847+
yield* TestClock.adjust(Duration.millis(19_999));
848+
849+
expect(queries).toHaveLength(1);
850+
yield* TestClock.adjust("1 millis");
851+
const [slow, next] = queries;
852+
expect(next?.start).toBe(slow!.end! + 20_000);
853+
yield* manager.close({ threadId: "thread-1" });
854+
}).pipe(Effect.provide(TestClock.layer())),
855+
);
856+
857+
it.effect("ends a hung process-table query and paces the retry", () =>
858+
Effect.gen(function* () {
859+
const started = yield* Deferred.make<void>();
860+
const queries: Array<{ start: number; end?: number }> = [];
861+
const { manager } = yield* createFallbackManager(
862+
"win32",
863+
slowProcessTable("win32", "1 day", queries, [started]),
864+
);
865+
866+
yield* manager.open(openInput());
867+
yield* TestClock.adjust("1 second");
868+
yield* Deferred.await(started);
869+
yield* TestClock.adjust("2 minutes");
870+
yield* TestClock.adjust("2 minutes");
871+
872+
const [hung, next] = queries;
873+
expect(hung!.end! - hung!.start).toBe(15_000);
874+
expect(next!.start - hung!.end!).toBe(60_000);
875+
yield* manager.close({ threadId: "thread-1" });
876+
}).pipe(Effect.provide(TestClock.layer())),
877+
);
878+
879+
it.effect.each([
880+
["linux", 1_000],
881+
["win32", 1_500],
882+
] as const)(
883+
"bounds closeIdle's %s query so other operations on the thread still run",
884+
([platform, budgetMs]) =>
885+
Effect.gen(function* () {
886+
const started = yield* Deferred.make<void>();
887+
const queries: Array<{ start: number; end?: number }> = [];
888+
const { manager, ptyAdapter } = yield* createFallbackManager(
889+
platform,
890+
slowProcessTable(platform, "1 day", queries, [started]),
891+
);
892+
yield* manager.open(openInput());
893+
894+
const closing = yield* manager
895+
.closeIdle({ threadId: "thread-1" })
896+
.pipe(Effect.forkChild({ startImmediately: true }));
897+
yield* Deferred.await(started);
898+
const resizing = yield* manager
899+
.resize({ threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID, cols: 80, rows: 30 })
900+
.pipe(Effect.forkChild({ startImmediately: true }));
901+
yield* TestClock.adjust(Duration.millis(budgetMs));
902+
903+
expect(closing.pollUnsafe()).toBeDefined();
904+
expect(resizing.pollUnsafe()).toBeDefined();
905+
expect(queries[0]!.end! - queries[0]!.start).toBe(budgetMs);
906+
expect(ptyAdapter.processes[0]!.resizeCalls).toEqual([{ cols: 80, rows: 30 }]);
907+
expect(ptyAdapter.processes[0]!.killed).toBe(false);
908+
yield* manager.close({ threadId: "thread-1" });
909+
}).pipe(Effect.provide(TestClock.layer())),
910+
);
911+
759912
it.effect("keeps terminals that get input or output while closeIdle checks them", () =>
760913
Effect.gen(function* () {
761914
const ptyAdapter = new FakePtyAdapter();

0 commit comments

Comments
 (0)