GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(terminal): retain and pace slow fallback process queries by juliusmarminge · Pull Request #24640 · pingdotgg/t3code

fix(terminal): retain and pace slow fallback process queries #24640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
juliusmarminge merged 5 commits into main from takeover/pr-16874
Oct 30, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(terminal): retain and pace slow fallback process queries #24640

Changes from 1 commit
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
ba8068c
test(server): cover slow and hung process-table fallback queries
juliusmarminge Oct 25, 2026
cfaccea
fix(server): slow process-table queries keep their results and pace t…
juliusmarminge Oct 25, 2026
a77f9be
fix(server): closeIdle keeps its short process-table query budget
juliusmarminge Oct 25, 2026
cca4625
Merge main into takeover/pr-16874
juliusmarminge Oct 25, 2026
63e4467
Merge main into takeover/pr-16874
juliusmarminge Oct 27, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
fix(server): slow process-table queries keep their results and pace t…
…he next poll

A loaded Linux host or Windows WMI can take seconds to answer the
process-table fallback. The 1s/1.5s timeouts threw those valid tables
away and the loop re-issued the query almost immediately. Both queries
now share a 15s timeout, and the poll loop idles at least four times as
long as the last fallback query took (capped at 60s), on success and on
failure. Native and custom inspection are not delayed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@juliusmarminge @claude
juliusmarminge and claude committed Oct 25, 2026
commit cfacceac44cadb51e1b0eeb9d6307d7b4f903df6

There are no files selected for viewing

128 changes: 72 additions & 56 deletions 128 apps/server/src/terminal/Manager.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Base64Url from "effect/encoding/Base64Url";
import * as Equal from "effect/Equal";
Expand All @@ -56,6 +57,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Result from "effect/Result";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import * as SynchronizedRef from "effect/SynchronizedRef";
Expand Down Expand Up @@ -98,6 +100,9 @@ const MAX_HISTORY_CHUNK_LENGTH = 16 * 1024;
const DEFAULT_PERSIST_DEBOUNCE_MS = 250;
const DEFAULT_SUBPROCESS_POLL_INTERVAL_MS = 1_000;
const MAX_SUBPROCESS_POLL_INTERVAL_MS = 60_000;
// `ps` and WMI can take seconds on a loaded host and their tables are still
// valid then; the timeout only ends a query that is truly stuck.
const PROCESS_TABLE_QUERY_TIMEOUT = "15 seconds";
// Discover children started without terminal input or output.
const QUIET_SUBPROCESS_SNAPSHOT_EVERY_TICKS = 30;
const DEFAULT_PROCESS_KILL_GRACE_MS = 1_000;
Expand Down Expand Up @@ -654,8 +659,17 @@ interface TerminalProcessTableSnapshot {
readonly commandById: ReadonlyMap<number, string>;
}

function subprocessSnapshotPollDelayMs(pollIntervalMs: number, failureCount: number): number {
return Math.min(pollIntervalMs * 2 ** failureCount, MAX_SUBPROCESS_POLL_INTERVAL_MS);
// Idles at least four times as long as the last fallback query took, so a slow
// process table never keeps the poll loop busy most of the time.
function subprocessSnapshotPollDelayMs(
pollIntervalMs: number,
failureCount: number,
queryElapsedMs: number,
): number {
return Math.min(
Math.max(pollIntervalMs * 2 ** failureCount, queryElapsedMs * 4),
MAX_SUBPROCESS_POLL_INTERVAL_MS,
);
}

function parsePosixProcessTable(stdout: string): TerminalProcessTableSnapshot {
Expand Down Expand Up @@ -757,7 +771,7 @@ const posixProcessTableSnapshot = Effect.fn("terminal.posixProcessTableSnapshot"
.run({
command: psCommand,
args: ["-eo", "pid=,ppid=,comm="],
timeout: "1 second",
timeout: PROCESS_TABLE_QUERY_TIMEOUT,
maxOutputBytes: 524_288,
outputMode: "truncate",
timeoutBehavior: "timedOutResult",
Expand Down Expand Up @@ -797,7 +811,7 @@ const windowsProcessTableSnapshot = Effect.fn("terminal.windowsProcessTableSnaps
.run({
command: "powershell.exe",
args: ["-NoProfile", "-NonInteractive", "-Command", command],
timeout: "1500 millis",
timeout: PROCESS_TABLE_QUERY_TIMEOUT,
maxOutputBytes: 262_144,
outputMode: "truncate",
timeoutBehavior: "timedOutResult",
Expand Down Expand Up @@ -1551,54 +1565,67 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
// One process-table snapshot per poll tick, shared across every terminal.
// Per-terminal `pgrep`/`ps` calls multiply spawn load by terminal count and
// can exhaust the PID space on hosts with many sessions (#6332).
// The query is timed whether it succeeds or fails so the poll loop can pace
// the next one from it.
const fallbackProcessTableSnapshot = (
platform === "win32"
? windowsProcessTableSnapshot()
: posixProcessTableSnapshot(yield* resolvePosixPsCommand())
).pipe(Effect.provideService(ProcessRunner.ProcessRunner, processRunner));
const fetchProcessTableSnapshot: Effect.Effect<
{
readonly snapshot: TerminalProcessTableSnapshot;
readonly snapshotSucceeded: boolean;
},
TerminalSubprocessCheckError
> = options.processTable
).pipe(
Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
Effect.result,
Effect.timed,
Effect.map(([elapsed, snapshot]) => ({
snapshot,
queryElapsedMs: Duration.toMillis(elapsed),
})),
);
const fetchProcessTableSnapshot: Effect.Effect<{
readonly snapshot: Result.Result<TerminalProcessTableSnapshot, TerminalSubprocessCheckError>;
readonly snapshotSucceeded: boolean;
readonly queryElapsedMs: number;
}> = options.processTable
? options.processTable.pipe(
Effect.map((entries) => ({
snapshot: processTableSnapshotFromProcesses(entries),
snapshot: Result.succeed(processTableSnapshotFromProcesses(entries)),
snapshotSucceeded: true,
queryElapsedMs: 0,
})),
Effect.catch(() =>
fallbackProcessTableSnapshot.pipe(
Effect.map((snapshot) => ({ snapshot, snapshotSucceeded: false })),
Effect.map((fallback) => ({ ...fallback, snapshotSucceeded: false })),
),
),
)
: fallbackProcessTableSnapshot.pipe(
Effect.map((snapshot) => ({ snapshot, snapshotSucceeded: true })),
Effect.map((fallback) => ({
...fallback,
snapshotSucceeded: Result.isSuccess(fallback.snapshot),
})),
);
const customSubprocessInspector = options.subprocessInspector;
const acquireSubprocessInspector: Effect.Effect<
{
readonly inspector: TerminalSubprocessInspector;
readonly snapshotSucceeded: boolean;
},
TerminalSubprocessCheckError
> =
const acquireSubprocessInspector: Effect.Effect<{
readonly inspector: Result.Result<TerminalSubprocessInspector, TerminalSubprocessCheckError>;
readonly snapshotSucceeded: boolean;
readonly queryElapsedMs: number;
}> =
customSubprocessInspector !== undefined
? Effect.succeed({ inspector: customSubprocessInspector, snapshotSucceeded: true })
? Effect.succeed({
inspector: Result.succeed(customSubprocessInspector),
snapshotSucceeded: true,
queryElapsedMs: 0,
})
: Effect.map(
fetchProcessTableSnapshot,
({
snapshot,
snapshotSucceeded,
}): {
readonly inspector: TerminalSubprocessInspector;
readonly snapshotSucceeded: boolean;
} => ({
inspector: (terminalPid) =>
Effect.succeed(deriveSubprocessInspectResult(snapshot, terminalPid, platform)),
({ snapshot, snapshotSucceeded, queryElapsedMs }) => ({
inspector: Result.map(
snapshot,
(table): TerminalSubprocessInspector =>
(terminalPid) =>
Effect.succeed(deriveSubprocessInspectResult(table, terminalPid, platform)),
),
snapshotSucceeded,
queryElapsedMs,
}),
);
const subprocessPollIntervalMs =
Expand Down Expand Up @@ -2532,39 +2559,27 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
}));

if (runningSessions.length === 0) {
return true;
return { snapshotSucceeded: true, queryElapsedMs: 0 };
}
if (
runningSessions.every(({ session }) => isQuiet(session)) &&
skippedQuietTicks < QUIET_SUBPROCESS_SNAPSHOT_EVERY_TICKS - 1
) {
skippedQuietTicks += 1;
return true;
return { snapshotSucceeded: true, queryElapsedMs: 0 };
}
skippedQuietTicks = 0;

const inspectorOption = yield* acquireSubprocessInspector.pipe(
Effect.asSome,
Effect.catch((reason) =>
Effect.logWarning("failed to snapshot processes for terminal subprocess polling", {
reason,
}).pipe(
Effect.as(
Option.none<{
readonly inspector: TerminalSubprocessInspector;
readonly snapshotSucceeded: boolean;
}>(),
),
),
),
);

if (Option.isNone(inspectorOption)) {
const { inspector, snapshotSucceeded, queryElapsedMs } = yield* acquireSubprocessInspector;
if (Result.isFailure(inspector)) {
yield* Effect.logWarning("failed to snapshot processes for terminal subprocess polling", {
reason: inspector.failure,
});
quietSessions = new WeakMap();
return false;
return { snapshotSucceeded: false, queryElapsedMs };
}

const { inspector: subprocessInspector, snapshotSucceeded } = inspectorOption.value;
const subprocessInspector = inspector.success;
const checkSubprocessActivity = Effect.fn("terminal.checkSubprocessActivity")(function* (
candidate: (typeof runningSessions)[number],
) {
Expand Down Expand Up @@ -2660,7 +2675,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
discard: true,
});

return snapshotSucceeded;
return { snapshotSucceeded, queryElapsedMs };
});

const hasRunningSessions = readManagerState.pipe(
Expand All @@ -2675,13 +2690,14 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
Effect.flatMap((active) =>
active
? pollSubprocessActivity().pipe(
Effect.flatMap((snapshotSucceeded) => {
Effect.flatMap(({ snapshotSucceeded, queryElapsedMs }) => {
subprocessSnapshotFailureCount = snapshotSucceeded
? 0
: Math.min(subprocessSnapshotFailureCount + 1, 30);
const delayMs = subprocessSnapshotPollDelayMs(
subprocessPollIntervalMs,
subprocessSnapshotFailureCount,
queryElapsedMs,
);
return Effect.sleep(delayMs);
}),
Expand Down Expand Up @@ -3347,7 +3363,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
);
// Inspect now instead of trusting the last poll, so a command started
// since then keeps its terminal.
const { inspector } = yield* acquireSubprocessInspector;
const inspector = yield* Effect.fromResult((yield* acquireSubprocessInspector).inspector);
yield* Effect.forEach(
running,
(session) =>
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.