MSBuild "MSB3027: could not copy ... locked" Output File in CI
MSBuild could not copy a build output because the destination file is locked by another process. It retries, then fails MSB3027 ("being used by another process") - a still-running app, antivirus scan, or a concurrent build is holding the file.
What this error means
Build fails copying a .dll/.exe into bin/obj with MSB3027 after exhausting copy retries, saying the file is in use. It is often intermittent on Windows runners where a previous test process or scanner still holds the output.
error MSB3027: Could not copy "obj/Release/MyApp.dll" to "bin/Release/MyApp.dll".
Exceeded retry count of 10. Failed.
error MSB3021: The process cannot access the file because it is being used by another process.Common causes
A previous process still holds the output
A test host, a launched app, or a watcher from an earlier step is still running and has the output .dll/.exe open, blocking the copy.
Antivirus or a parallel build locking the file
Real-time antivirus scanning the output, or two builds writing the same bin, can momentarily lock the file and exhaust MSBuild’s copy retries.
How to fix it
Ensure prior processes have exited
Stop any app/test host that may still hold the output before rebuilding.
# wait for / kill a lingering test host before rebuild (Windows)
taskkill /F /IM MyApp.exe /T 2>NUL
dotnet build -c ReleaseAvoid concurrent writes to the same output
- Do not run two builds/tests that write the same
bin/objin parallel on one runner. - Exclude the build output directory from real-time antivirus scanning on the runner.
- Use a clean working directory per job so stale processes cannot hold outputs.
How to prevent it
- Serialize steps that build/run the same project so no process holds the output during a copy.
- Exclude
bin/objfrom antivirus scanning on CI runners. - Give each job an isolated workspace to avoid cross-build file locks.