Skip to content
TypeScript

fix(server): stalled clones say "Stalled" instead of a frozen speed (#22780)

Theo Browne8fe4206decd2
diff --git a/apps/server/src/project/ProjectCloneTracker.test.ts b/apps/server/src/project/ProjectCloneTracker.test.ts
index 4dbbb012175..6a13af9ef94 100644
--- a/apps/server/src/project/ProjectCloneTracker.test.ts
+++ b/apps/server/src/project/ProjectCloneTracker.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from "@effect/vitest";
import {
OrchestrationDispatchCommandError,
ProjectId,
+ projectCloneProgressSummary,
SourceControlRepositoryError,
} from "@t3tools/contracts";
import * as Deferred from "effect/Deferred";
@@ -105,6 +106,45 @@ describe("ProjectCloneTracker", () => {
}).pipe(Effect.provide(harness.layer));
});
+ it.effect("flags a clone stalled when git goes quiet and clears it when git speaks", () => {
+ const release = Deferred.makeUnsafe<void>();
+ const resumed = Deferred.makeUnsafe<void>();
+ const harness = makeHarness({
+ clone: (input, options) =>
+ Effect.gen(function* () {
+ const report = options?.onProgress ?? (() => Effect.void);
+ yield* report({ stage: "receiving", percent: 24, detail: "168.80 MiB | 20.69 MiB/s" });
+ yield* Deferred.await(resumed);
+ yield* report({ stage: "receiving", percent: 25, detail: "170.00 MiB | 1.00 MiB/s" });
+ yield* Deferred.await(release);
+ return { cwd: input.destinationPath, remoteUrl: input.remoteUrl ?? "", repository: null };
+ }),
+ });
+ return Effect.gen(function* () {
+ const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
+ yield* tracker.start(startInput, harness.hooks);
+ yield* Effect.yieldNow;
+ expect((yield* tracker.get(projectId))?.stalled).toBe(false);
+
+ yield* TestClock.adjust("19 seconds");
+ expect((yield* tracker.get(projectId))?.stalled).toBe(false);
+ yield* TestClock.adjust("1 second");
+ const stalled = yield* tracker.get(projectId);
+ expect(stalled).toMatchObject({ stalled: true, percent: 24 });
+ expect(projectCloneProgressSummary(stalled!)).toBe("Receiving objects · 24% · Stalled");
+
+ yield* Deferred.succeed(resumed, undefined);
+ yield* Effect.yieldNow;
+ expect(yield* tracker.get(projectId)).toMatchObject({ stalled: false, percent: 25 });
+
+ yield* TestClock.adjust("20 seconds");
+ expect((yield* tracker.get(projectId))?.stalled).toBe(true);
+ yield* Deferred.succeed(release, undefined);
+ yield* Effect.yieldNow;
+ expect(yield* tracker.get(projectId)).toMatchObject({ phase: "done", stalled: false });
+ }).pipe(Effect.provide(harness.layer));
+ });
+
it.effect("keeps a failed clone with git's own explanation and retries it", () => {
let attempts = 0;
const harness = makeHarness({
diff --git a/apps/server/src/project/ProjectCloneTracker.ts b/apps/server/src/project/ProjectCloneTracker.ts
index 9d8896cea0f..411d84fb385 100644
--- a/apps/server/src/project/ProjectCloneTracker.ts
+++ b/apps/server/src/project/ProjectCloneTracker.ts
@@ -20,6 +20,7 @@ import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
+import * as Option from "effect/Option";
import * as PubSub from "effect/PubSub";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
@@ -67,6 +68,10 @@ export interface ProjectCloneHooks {
}
const DONE_RETENTION = "30 seconds";
+// Git prints nothing while a transfer waits on the network, and the kernel can
+// take 15+ minutes to give up on a dead connection. Without this the banner
+// keeps showing the last transfer speed as if data were still arriving.
+const STALL_AFTER = "20 seconds";
function clampText(text: string, maxLength: number): string {
return text.length <= maxLength ? text : `${text.slice(0, maxLength - 1)}…`;
@@ -165,9 +170,28 @@ export const make = Effect.gen(function* () {
percent: update.percent,
detail:
update.detail === null ? null : clampText(update.detail, PROJECT_CLONE_DETAIL_MAX_LENGTH),
+ stalled: false,
},
})).pipe(Effect.asVoid);
+ const markStalled = (projectId: ProjectId) =>
+ modify(projectId, (tracked) => ({
+ ...tracked,
+ snapshot: { ...tracked.snapshot, stalled: true },
+ })).pipe(Effect.asVoid);
+
+ /** Marks the clone stalled once, then waits for git to speak again. */
+ const watchForStall = (projectId: ProjectId, pulses: Queue.Dequeue<void>) =>
+ Queue.take(pulses).pipe(
+ Effect.timeoutOption(STALL_AFTER),
+ Effect.flatMap((pulse) =>
+ Option.isSome(pulse)
+ ? Effect.void
+ : markStalled(projectId).pipe(Effect.andThen(Queue.take(pulses))),
+ ),
+ Effect.forever,
+ );
+
const finish = Effect.fn("ProjectCloneTracker.finish")(function* (
projectId: ProjectId,
phase: "done" | "failed" | "cancelled",
@@ -182,12 +206,29 @@ export const make = Effect.gen(function* () {
phase,
endedAt,
percent: phase === "done" ? 100 : tracked.snapshot.percent,
+ stalled: false,
error: error === null ? null : clampText(error, PROJECT_CLONE_ERROR_MAX_LENGTH),
},
}));
if (phase === "done") yield* scheduleRemoval(projectId);
});
+ const cloneWatchingForStall = Effect.fn("ProjectCloneTracker.cloneWatchingForStall")(function* (
+ projectId: ProjectId,
+ tracked: TrackedClone,
+ ) {
+ const pulses = yield* Queue.sliding<void>(1);
+ yield* watchForStall(projectId, pulses).pipe(Effect.forkScoped);
+ return yield* repositories.cloneRepository(
+ { remoteUrl: tracked.input.cloneUrl, destinationPath: tracked.input.destinationPath },
+ {
+ onProgress: (update) =>
+ Queue.offer(pulses, undefined).pipe(Effect.andThen(progress(projectId, update))),
+ timeoutMs: null,
+ },
+ );
+ }, Effect.scoped);
+
/**
* Runs in its own fiber; the tracker records the outcome through `onExit`,
* which still runs when the fiber is interrupted (a `matchCause` handler
@@ -196,26 +237,21 @@ export const make = Effect.gen(function* () {
* checkout.
*/
const runClone = (projectId: ProjectId, tracked: TrackedClone) =>
- repositories
- .cloneRepository(
- { remoteUrl: tracked.input.cloneUrl, destinationPath: tracked.input.destinationPath },
- { onProgress: (update) => progress(projectId, update), timeoutMs: null },
- )
- .pipe(
- Effect.onExit((exit) =>
- Exit.isSuccess(exit)
- ? finish(projectId, "done", null)
- : Cause.hasInterruptsOnly(exit.cause)
- ? finish(projectId, "cancelled", null)
- : finish(projectId, "failed", describeCloneFailure(exit.cause)),
- ),
- Effect.flatMap(() =>
- tracked.hooks
- .onCloned({ projectId, workspaceRoot: tracked.input.destinationPath })
- .pipe(Effect.ignoreCause({ log: true })),
- ),
- Effect.ignoreCause(),
- );
+ cloneWatchingForStall(projectId, tracked).pipe(
+ Effect.onExit((exit) =>
+ Exit.isSuccess(exit)
+ ? finish(projectId, "done", null)
+ : Cause.hasInterruptsOnly(exit.cause)
+ ? finish(projectId, "cancelled", null)
+ : finish(projectId, "failed", describeCloneFailure(exit.cause)),
+ ),
+ Effect.flatMap(() =>
+ tracked.hooks
+ .onCloned({ projectId, workspaceRoot: tracked.input.destinationPath })
+ .pipe(Effect.ignoreCause({ log: true })),
+ ),
+ Effect.ignoreCause(),
+ );
const launch = Effect.fn("ProjectCloneTracker.launch")(function* (projectId: ProjectId) {
const current = yield* Ref.get(clones);
@@ -244,6 +280,7 @@ export const make = Effect.gen(function* () {
stage: "connecting",
percent: null,
detail: null,
+ stalled: false,
error: null,
startedAt,
endedAt: null,
@@ -346,6 +383,7 @@ export const make = Effect.gen(function* () {
stage: "connecting",
percent: null,
detail: null,
+ stalled: false,
error: null,
startedAt,
endedAt: null,
diff --git a/apps/web/src/components/ProjectCloneToastCoordinator.tsx b/apps/web/src/components/ProjectCloneToastCoordinator.tsx
index 974138bf2f6..79c927d6e2d 100644
--- a/apps/web/src/components/ProjectCloneToastCoordinator.tsx
+++ b/apps/web/src/components/ProjectCloneToastCoordinator.tsx
@@ -45,7 +45,7 @@ interface TrackedToast {
}
function renderKey(clone: ProjectCloneSnapshot): string {
- return `${clone.phase}:${clone.stage}:${clone.percent ?? ""}:${clone.detail ?? ""}:${clone.error ?? ""}`;
+ return `${clone.phase}:${clone.stage}:${clone.percent ?? ""}:${clone.detail ?? ""}:${clone.stalled === true}:${clone.error ?? ""}`;
}
async function runCloneAction(
diff --git a/packages/contracts/src/projectClone.ts b/packages/contracts/src/projectClone.ts
index 7eeada42347..d0e2eee82de 100644
--- a/packages/contracts/src/projectClone.ts
+++ b/packages/contracts/src/projectClone.ts
@@ -31,6 +31,8 @@ export const ProjectCloneSnapshot = Schema.Struct({
stage: ProjectCloneStage,
percent: Schema.NullOr(Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 100 }))),
detail: Schema.NullOr(Schema.String.check(Schema.isMaxLength(PROJECT_CLONE_DETAIL_MAX_LENGTH))),
+ /** Git has printed nothing for a while; `detail` is no longer current. Older servers omit it. */
+ stalled: Schema.optionalKey(Schema.Boolean),
error: Schema.NullOr(Schema.String.check(Schema.isMaxLength(PROJECT_CLONE_ERROR_MAX_LENGTH))),
startedAt: IsoDateTime,
endedAt: Schema.NullOr(IsoDateTime),
@@ -98,10 +100,12 @@ export function projectCloneDisplayName(
}
export function projectCloneProgressSummary(
- snapshot: Pick<ProjectCloneSnapshot, "stage" | "percent" | "detail">,
+ snapshot: Pick<ProjectCloneSnapshot, "stage" | "percent" | "detail" | "stalled">,
): string {
const parts = [projectCloneStageLabel(snapshot.stage)];
if (snapshot.percent !== null) parts.push(`${snapshot.percent}%`);
- if (snapshot.detail) parts.push(snapshot.detail);
+ // The detail carries git's last transfer speed, which a stall makes a lie.
+ if (snapshot.stalled) parts.push("Stalled");
+ else if (snapshot.detail) parts.push(snapshot.detail);
return parts.join(" · ");
}