Skip to content

Commit acbbea1

Browse files
authored
chore: retry .NET CLI mutex errors (#356)
An attempt to get rid of mutex errors in the CLI tests. On CI we sometimes see: System.IO.IOException: The system cannot open the device or file specified. : 'NuGet-Migrations' This error can be caused by lack of permissions on a temporary directory, but in our case it's intermittent so my guess is it is caused by multiple invocations of the .NET CLI running in parallel, and trampling on each other creating a Mutex. There is no fix, and it is annoyingly breaking our CI regularly. Retry a couple of times to increase reliability. If we still see this error after this change, maybe it is about file permissions after all. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent cce3aef commit acbbea1

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

packages/aws-cdk/lib/commands/init/init-hooks.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,33 @@ async function dotnetAddProject(targetDirectory: string, context: HookContext, e
8383
const pname = context.placeholder('name.PascalCased');
8484
const slnPath = path.join(targetDirectory, 'src', `${pname}.sln`);
8585
const csprojPath = path.join(targetDirectory, 'src', pname, `${pname}.${ext}`);
86-
try {
87-
await shell(['dotnet', 'sln', slnPath, 'add', csprojPath]);
88-
} catch (e: any) {
89-
throw new ToolkitError(`Could not add project ${pname}.${ext} to solution ${pname}.sln. ${formatErrorMessage(e)}`);
86+
87+
// We retry this command a couple of times. It usually never fails, except on CI where
88+
// we sometimes see:
89+
//
90+
// System.IO.IOException: The system cannot open the device or file specified. : 'NuGet-Migrations'
91+
//
92+
// This error can be caused by lack of permissions on a temporary directory,
93+
// but in our case it's intermittent so my guess is it is caused by multiple
94+
// invocations of the .NET CLI running in parallel, and trampling on each
95+
// other creating a Mutex. There is no fix, and it is annoyingly breaking our
96+
// CI regularly. Retry a couple of times to increase reliability.
97+
//
98+
// - https://github.com/dotnet/sdk/issues/43750
99+
// - https://github.com/dotnet/runtime/issues/80619
100+
// - https://github.com/dotnet/runtime/issues/91987
101+
const MAX_ATTEMPTS = 3;
102+
for (let attempt = 1; ; attempt++) {
103+
try {
104+
await shell(['dotnet', 'sln', slnPath, 'add', csprojPath]);
105+
return;
106+
} catch (e: any) {
107+
if (attempt === MAX_ATTEMPTS) {
108+
throw new ToolkitError(`Could not add project ${pname}.${ext} to solution ${pname}.sln. ${formatErrorMessage(e)}`);
109+
}
110+
111+
// Sleep for a bit then try again
112+
await new Promise(resolve => setTimeout(resolve, 1000));
113+
}
90114
}
91115
}

0 commit comments

Comments
 (0)