Skip to content

Commit 1bf55a9

Browse files
committed
Treat empty port allocation file as bootstrap signal so concurrent callers re-write the same deterministic value instead of throwing
1 parent 597bad9 commit 1bf55a9

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

application/shared-kernel/SharedKernel/Configuration/PortAllocation.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ public static PortAllocation LoadFrom(string repositoryRoot)
7878
var workspaceDirectory = Path.Combine(repositoryRoot, WorkspaceDirectoryName);
7979
var portFilePath = Path.Combine(workspaceDirectory, PortFileName);
8080

81-
if (!File.Exists(portFilePath))
81+
// Treat "file doesn't exist" and "file exists but is empty" as the same condition: both
82+
// mean we need to (re-)bootstrap. The empty case happens when a concurrent caller has
83+
// opened the file with O_CREAT|O_TRUNC but hasn't written yet — overwriting it with the
84+
// same deterministic value is a harmless no-op.
85+
var content = File.Exists(portFilePath) ? File.ReadAllText(portFilePath).Trim() : "";
86+
87+
if (content.Length == 0)
8288
{
8389
var bootstrapPort = IsWorktree(repositoryRoot)
8490
? FindFreeBasePortForWorktree()
@@ -88,7 +94,6 @@ public static PortAllocation LoadFrom(string repositoryRoot)
8894
return new PortAllocation(bootstrapPort);
8995
}
9096

91-
var content = File.ReadAllText(portFilePath).Trim();
9297
if (!int.TryParse(content, out var basePort) || basePort <= 0)
9398
{
9499
throw new InvalidOperationException(

0 commit comments

Comments
 (0)