Skip to content

Commit 1de6fcd

Browse files
fix(server): parallel worktree launches no longer fail on .git/config lock
Git takes .git/config.lock without waiting, and every worktree of a repository shares that file. Parallel launches from a remote branch lost the race in `git worktree add -b`, leaving a branch without a worktree, and the base-ref writes after it silently dropped branch bases. Git calls that write the shared config now pass `writesSharedConfig`, which serializes them per repository (keyed by the git common dir). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d73fd12 commit 1de6fcd

5 files changed

Lines changed: 131 additions & 27 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export interface ExecuteGitInput {
5757
/** With appendTruncationMarker, line callbacks continue beyond the buffer cap for progress-only consumers. */
5858
readonly keepLineCallbacksAfterTruncation?: boolean;
5959
readonly progress?: ExecuteGitProgress;
60+
/** Writes .git/config, which every worktree shares and Git locks without waiting. */
61+
readonly writesSharedConfig?: boolean;
6062
}
6163

6264
export interface ExecuteGitResult {

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,70 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
31243124
}),
31253125
);
31263126

3127+
it.effect("creates parallel worktrees in one repository without racing on its config", () =>
3128+
Effect.gen(function* () {
3129+
const cwd = yield* makeTmpDir();
3130+
const { initialBranch } = yield* initRepoWithCommit(cwd);
3131+
const remote = yield* makeTmpDir("git-remote-");
3132+
yield* git(remote, ["init", "--bare"]);
3133+
yield* git(cwd, ["remote", "add", "origin", remote]);
3134+
yield* git(cwd, ["push", "origin", initialBranch]);
3135+
const path = yield* Path.Path;
3136+
const worktrees = yield* makeTmpDir("git-worktrees-");
3137+
3138+
// Git has no lock timeout for .git/config, which every worktree shares: a writer that
3139+
// overlaps another fails, and `worktree add -b` then leaves its branch without a worktree.
3140+
let configWriters = 0;
3141+
let peakConfigWriters = 0;
3142+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3143+
const countingSpawner = ChildProcessSpawner.make((command) =>
3144+
Effect.gen(function* () {
3145+
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
3146+
const writesConfig =
3147+
(args.includes("worktree") && args.includes("add")) ||
3148+
(args[0] === "config" && !args[1]?.startsWith("-"));
3149+
if (!writesConfig) return yield* delegate.spawn(command);
3150+
configWriters += 1;
3151+
peakConfigWriters = Math.max(peakConfigWriters, configWriters);
3152+
const handle = yield* delegate.spawn(command);
3153+
return ChildProcessSpawner.makeHandle({
3154+
...handle,
3155+
exitCode: handle.exitCode.pipe(
3156+
Effect.ensuring(Effect.sync(() => (configWriters -= 1))),
3157+
),
3158+
});
3159+
}),
3160+
);
3161+
const driver = yield* makeGitVcsDriverCore().pipe(
3162+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, countingSpawner),
3163+
Effect.provide(layerServerConfig),
3164+
);
3165+
const branches = Array.from({ length: 8 }, (_, index) => `feature/parallel-${index}`);
3166+
3167+
yield* Effect.forEach(
3168+
branches,
3169+
(branch) =>
3170+
driver.createWorktree({
3171+
cwd,
3172+
path: path.join(worktrees, branch.replace("/", "-")),
3173+
refName: `origin/${initialBranch}`,
3174+
newRefName: branch,
3175+
baseRefName: `origin/${initialBranch}`,
3176+
}),
3177+
{ concurrency: "unbounded" },
3178+
);
3179+
3180+
assert.equal(peakConfigWriters, 1);
3181+
for (const branch of branches) {
3182+
assert.equal(yield* git(cwd, ["config", `branch.${branch}.remote`]), "origin");
3183+
assert.equal(
3184+
yield* git(cwd, ["config", `branch.${branch}.t3-merge-base-ref`]),
3185+
`refs/remotes/origin/${initialBranch}`,
3186+
);
3187+
}
3188+
}),
3189+
);
3190+
31273191
it.effect.each(["release/v2", "refs/heads/release/v2", "deadbeef"])(
31283192
"records the branch name when creating a worktree from %s",
31293193
(baseRef) =>

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

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ interface ExecuteGitOptions {
185185
maxOutputBytes?: number | undefined;
186186
appendTruncationMarker?: boolean | undefined;
187187
progress?: GitVcsDriver.ExecuteGitProgress | undefined;
188+
writesSharedConfig?: boolean | undefined;
188189
}
189190

190191
function parseBranchAb(value: string): { ahead: number; behind: number } {
@@ -1072,6 +1073,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10721073
},
10731074
);
10741075

1076+
const sharedConfigLocks = new Map<string, Semaphore.Semaphore>();
1077+
const withSharedConfigLock = <A, E>(cwd: string, effect: Effect.Effect<A, E>) =>
1078+
resolveRepositoryPaths(cwd).pipe(
1079+
Effect.orElseSucceed(() => null),
1080+
Effect.flatMap((repositoryPaths) => {
1081+
if (repositoryPaths === null) return effect;
1082+
let lock = sharedConfigLocks.get(repositoryPaths.gitCommonDir);
1083+
if (lock === undefined) {
1084+
lock = Semaphore.makeUnsafe(1);
1085+
sharedConfigLocks.set(repositoryPaths.gitCommonDir, lock);
1086+
}
1087+
return lock.withPermit(effect);
1088+
}),
1089+
);
1090+
10751091
const execute: GitVcsDriver.GitVcsDriver["Service"]["execute"] = (input) =>
10761092
executeRaw(input).pipe(
10771093
withMetrics({
@@ -1093,6 +1109,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10931109
"git.args_count": input.args.length,
10941110
},
10951111
}),
1112+
// Outside the process permit: resolving the repository may itself need one.
1113+
(execution) =>
1114+
input.writesSharedConfig ? withSharedConfigLock(input.cwd, execution) : execution,
10961115
);
10971116

10981117
const executeGit = (
@@ -1117,6 +1136,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11171136
? { appendTruncationMarker: options.appendTruncationMarker }
11181137
: {}),
11191138
...(options.progress ? { progress: options.progress } : {}),
1139+
...(options.writesSharedConfig ? { writesSharedConfig: true } : {}),
11201140
}).pipe(
11211141
Effect.flatMap((result) => {
11221142
if (options.allowNonZeroExit || result.exitCode === 0) {
@@ -2602,17 +2622,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
26022622
const publishBranch = yield* resolvePublishBranchName(cwd, branch);
26032623
// Publishing retargets the upstream, so preserve its comparison base first.
26042624
if ((yield* readBranchBase(cwd, branch)) === null) {
2605-
yield* runGit("GitVcsDriver.pushCurrentBranch.recordMergeBase", cwd, [
2606-
"config",
2607-
branchBaseConfigKey(branch),
2608-
currentUpstream.branchName,
2609-
]);
2625+
yield* runGit(
2626+
"GitVcsDriver.pushCurrentBranch.recordMergeBase",
2627+
cwd,
2628+
["config", branchBaseConfigKey(branch), currentUpstream.branchName],
2629+
{ writesSharedConfig: true },
2630+
);
26102631
if (!(yield* readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`))) {
2611-
yield* runGit("GitVcsDriver.pushCurrentBranch.recordComparisonRef", cwd, [
2612-
"config",
2613-
`branch.${branch}.t3-merge-base-ref`,
2614-
`refs/remotes/${currentUpstream.upstreamRef}`,
2615-
]);
2632+
yield* runGit(
2633+
"GitVcsDriver.pushCurrentBranch.recordComparisonRef",
2634+
cwd,
2635+
[
2636+
"config",
2637+
`branch.${branch}.t3-merge-base-ref`,
2638+
`refs/remotes/${currentUpstream.upstreamRef}`,
2639+
],
2640+
{ writesSharedConfig: true },
2641+
);
26162642
}
26172643
}
26182644
yield* runGit(
@@ -3714,6 +3740,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
37143740
{
37153741
fallbackErrorDetail: "git worktree add failed",
37163742
timeoutMs: WORKTREE_ADD_TIMEOUT_MS,
3743+
// Tracking a remote start point writes the new branch's upstream.
3744+
writesSharedConfig: true,
37173745
...(onCheckoutProgress
37183746
? {
37193747
// Git only prints checkout progress when stderr is a tty or the
@@ -3805,18 +3833,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
38053833
const base = yield* resolveBranchBase(input.cwd, input.baseRefName);
38063834
if (base) {
38073835
yield* Effect.gen(function* () {
3808-
yield* runGit("GitVcsDriver.createWorktree.configureBaseRef", input.cwd, [
3809-
"config",
3810-
branchBaseConfigKey(newRefName),
3811-
base.branchName,
3812-
]);
3836+
yield* runGit(
3837+
"GitVcsDriver.createWorktree.configureBaseRef",
3838+
input.cwd,
3839+
["config", branchBaseConfigKey(newRefName), base.branchName],
3840+
{ writesSharedConfig: true },
3841+
);
38133842
if (base.baseRef.startsWith("refs/remotes/")) {
38143843
// The base key names a branch on the host; this one keeps the remote it came from.
3815-
yield* runGit("GitVcsDriver.createWorktree.configureComparisonRef", input.cwd, [
3816-
"config",
3817-
`branch.${input.newRefName}.t3-merge-base-ref`,
3818-
base.baseRef,
3819-
]);
3844+
yield* runGit(
3845+
"GitVcsDriver.createWorktree.configureComparisonRef",
3846+
input.cwd,
3847+
["config", `branch.${input.newRefName}.t3-merge-base-ref`, base.baseRef],
3848+
{ writesSharedConfig: true },
3849+
);
38203850
}
38213851
}).pipe(
38223852
Effect.catchTags({
@@ -4125,13 +4155,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41254155
});
41264156

41274157
const setBranchUpstream: GitVcsDriver.GitVcsDriver["Service"]["setBranchUpstream"] = (input) =>
4128-
runGit("GitVcsDriver.setBranchUpstream", input.cwd, [
4129-
"branch",
4130-
"--set-upstream-to",
4131-
`${input.remoteName}/${input.remoteBranch}`,
4132-
"--",
4133-
input.branch,
4134-
]);
4158+
runGit(
4159+
"GitVcsDriver.setBranchUpstream",
4160+
input.cwd,
4161+
[
4162+
"branch",
4163+
"--set-upstream-to",
4164+
`${input.remoteName}/${input.remoteBranch}`,
4165+
"--",
4166+
input.branch,
4167+
],
4168+
{ writesSharedConfig: true },
4169+
);
41354170

41364171
/**
41374172
* Unlinks links under `target` without following them and removes the

‎packages/source-control-core/src/server/SourceControlHost.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export class SourceControlHost extends Context.Service<
8080
readonly args: ReadonlyArray<string>;
8181
readonly maxOutputBytes?: number;
8282
readonly appendTruncationMarker?: boolean;
83+
/** Serializes with other writers of the repository's shared .git/config. */
84+
readonly writesSharedConfig?: boolean;
8385
}) => Effect.Effect<SourceControlGitOutput, GitCommandError>;
8486
readonly resolveCommit: (input: {
8587
readonly cwd: string;

‎packages/source-control-github/src/server/GitHubSourceControlProvider.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ export const make = Effect.gen(function* () {
895895
operation: "GitHubSourceControlProvider.recordBranchBase",
896896
cwd,
897897
args: ["config", `branch.${branch}.gh-merge-base`, baseBranch],
898+
writesSharedConfig: true,
898899
})
899900
.pipe(
900901
Effect.asVoid,

0 commit comments

Comments
 (0)