Skip to content

Commit 7b54d04

Browse files
Yuruzuuclaude
andcommitted
fix(server): count status fetches when the process exits
A waiter can time out while the fetch keeps running, so counting at that point records the fetch before refs move and can skip the local re-read after it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 73da637 commit 7b54d04

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
2+
import * as NodeChildProcess from "node:child_process";
23
import * as NodeFS from "node:fs";
34
import * as NodeOS from "node:os";
5+
import * as NodePath from "node:path";
46
import * as NodeURL from "node:url";
57
import * as NodeServices from "@effect/platform-node/NodeServices";
68
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -3411,6 +3413,39 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
34113413
}),
34123414
);
34133415

3416+
it.effect("does not count a status fetch that is still running after its wait times out", () =>
3417+
Effect.gen(function* () {
3418+
const cwd = yield* makeTmpDir();
3419+
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
3420+
const control = yield* makeTmpDir("git-vcs-driver-control-");
3421+
const { initialBranch } = yield* initRepoWithCommit(cwd);
3422+
yield* git(remote, ["init", "--bare"]);
3423+
yield* git(cwd, ["remote", "add", "origin", remote]);
3424+
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
3425+
3426+
const started = NodePath.join(control, "started");
3427+
const release = NodePath.join(control, "release");
3428+
NodeChildProcess.execFileSync("mkfifo", [started, release]);
3429+
const uploadPack = NodePath.join(control, "upload-pack.sh");
3430+
NodeFS.writeFileSync(
3431+
uploadPack,
3432+
`#!/bin/sh\necho started > "${started}"\ncat "${release}" > /dev/null\nexec git-upload-pack "$@"\n`,
3433+
{ mode: 0o755 },
3434+
);
3435+
yield* git(cwd, ["config", "remote.origin.uploadpack", uploadPack]);
3436+
3437+
const driver = yield* GitVcsDriver.GitVcsDriver;
3438+
const initial = yield* driver.statusUpstreamFetchCount(cwd);
3439+
const status = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);
3440+
yield* Effect.promise(() => NodeFS.promises.readFile(started));
3441+
yield* TestClock.adjust("6 seconds");
3442+
yield* Fiber.join(status);
3443+
3444+
assert.equal(yield* driver.statusUpstreamFetchCount(cwd), initial);
3445+
yield* Effect.promise(() => NodeFS.promises.writeFile(release, "go"));
3446+
}),
3447+
);
3448+
34143449
it.effect("does not start Git auto-maintenance from background upstream fetches", () =>
34153450
Effect.gen(function* () {
34163451
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13261326
);
13271327
});
13281328

1329+
const statusUpstreamFetches = new Map<string, number>();
13291330
const fetchRemoteForStatus = Effect.fn("GitVcsDriver.fetchRemoteForStatus.wait")(function* (
13301331
gitCommonDir: string,
13311332
remoteName: string,
@@ -1374,6 +1375,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13741375
Effect.onExit(() =>
13751376
Effect.sync(() => {
13761377
statusFetches.delete(key);
1378+
// Counted at exit, not when a waiter stops waiting: a waiter can time out before refs
1379+
// move. Failures count too, because a failed fetch can still move some refs.
1380+
statusUpstreamFetches.set(
1381+
gitCommonDir,
1382+
(statusUpstreamFetches.get(gitCommonDir) ?? 0) + 1,
1383+
);
13771384
}),
13781385
),
13791386
Effect.forkIn(serviceScope, { startImmediately: false, uninterruptible: false }),
@@ -1621,20 +1628,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16211628
}
16221629
}
16231630
});
1624-
const statusUpstreamFetches = new Map<string, number>();
16251631
const refreshStatusRemoteCacheEntry = Effect.fn("refreshStatusRemoteCacheEntry")(function* (
16261632
cacheKey: StatusRemoteRefreshCacheKey,
16271633
) {
16281634
return yield* fetchRemoteForStatus(cacheKey.gitCommonDir, cacheKey.remoteName).pipe(
1629-
// Counted on failure too: a fetch can move some refs before it fails.
1630-
Effect.ensuring(
1631-
Effect.sync(() => {
1632-
statusUpstreamFetches.set(
1633-
cacheKey.gitCommonDir,
1634-
(statusUpstreamFetches.get(cacheKey.gitCommonDir) ?? 0) + 1,
1635-
);
1636-
}),
1637-
),
16381635
Effect.as(true as const),
16391636
);
16401637
});

0 commit comments

Comments
 (0)