You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Modifying a file and then executing it can fail on Linux #58964
Fundamentally, this seems like an inescapable race in multi-threaded programs on Linux.
My understanding of the failure is this:
A process with two threads wants to write a file and then execute it.
Thread 1 opens a file for writing
Thread 2 issues a call to create a new process and completes a fork call.
The new process inherits all file descriptors open in Process 1, including the file opened by Thread 1.
Thread 1 finishes writing and closes the file descriptor.
Thread 1 attempts to execute the file.
Linux produces ETXTBSY ("Text file is busy") because Process 2 still has an open handle to the file.
I don't see a way to completely prevent this race, as there's no way to avoid executing fork to create a new process on Linux. However, there are two mitigations we could consider:
1. Unix supports a FD_CLOEXEC flag on file descriptors, which indicates that the file descriptor should be closed after a call to exec. Since the "new process" procedure in Unix is fork+exec, this would narrow the window of the race to just after fork and just before exec. It may be possible to always set this flag when opening file descriptors on Linux from managed code -- we don't support fork without exec and inheriting non-tty open file descriptors does not appear to be in any .NET contract. we already do this
Introduce a retry into Process.Create if encountering ETXTBSY. This could workaround the issue, at the expense of taking longer to fail in a real contention situation.
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
It may be possible to always set this flag when opening file descriptors on Linux from managed code
We do that already. We use O_CLOEXEC flag when opening the file if it is supported. If it is not available, we set FD_CLOEXEC bit immediately after opening the file that leaves a small window where the file handle is inheritable, but should not be.
Introduce a retry into Process.Create if encountering ETXTBSY
This is just a tip of the iceberg that we happened to hit in CI. There are likely many other APIs that can fail in a bad way in this situation...
It may be worth double checking that the detection of O_CLOEXEC works as expected in the build. The auto detection scripts under libraries tend to be fragile and hide unexpected failures.
It may be worth double checking that the detection of O_CLOEXEC works as expected in the build. The auto detection scripts under libraries tend to be fragile and hide unexpected failures.
I checked, and it works as expected.
on Linux.
There should be no issue on Linux, since that supports O_CLOEXEC.
I think macOS does not support it, so it is subject to the race mentioned by @jkotas.
Thanks all -- I looked at the pal and did indeed see CLOEXEC but I wasn't certain that code was a bottleneck for the entire product.
Have you seen the issue occur? On what platform?
Yes, this is happening regularly in the Linux installer tests.
To be clear, CLOEXEC does not solve the problem, only minimizes the race. As far as I can tell, there is no solution. The race is an inherent design flaw in the Linux process model.
Got it. The issue is that the file handle is still referenced by processes being started which are between fork and exec.
The unix process implementation has a ReaderWriterLockSlim for which it takes a reader lock while starting processes, and a writer lock while reaping them.
When execve fails with ETXTBSY we can obtain and release the writer lock and then try to fork+exec once more. That way we ensure all pending forks got past exec.
changed the title [-]Modifying a file and then executing it can fail in on Linux[/-][+]Modifying a file and then executing it can fail on Linux[/+]on Jun 22, 2023
Activity
jkotas commentedon Sep 10, 2021
We do that already. We use
O_CLOEXECflag when opening the file if it is supported. If it is not available, we setFD_CLOEXECbit immediately after opening the file that leaves a small window where the file handle is inheritable, but should not be.This is just a tip of the iceberg that we happened to hit in CI. There are likely many other APIs that can fail in a bad way in this situation...
jkotas commentedon Sep 10, 2021
It may be worth double checking that the detection of
O_CLOEXECworks as expected in the build. The auto detection scripts under libraries tend to be fragile and hide unexpected failures.tmds commentedon Sep 13, 2021
I checked, and it works as expected.
There should be no issue on Linux, since that supports
O_CLOEXEC.I think macOS does not support it, so it is subject to the race mentioned by @jkotas.
Have you seen the issue occur? On what platform?
agocke commentedon Sep 13, 2021
Thanks all -- I looked at the pal and did indeed see CLOEXEC but I wasn't certain that code was a bottleneck for the entire product.
Yes, this is happening regularly in the Linux installer tests.
To be clear, CLOEXEC does not solve the problem, only minimizes the race. As far as I can tell, there is no solution. The race is an inherent design flaw in the Linux process model.
tmds commentedon Sep 13, 2021
Got it. The issue is that the file handle is still referenced by processes being started which are between
forkandexec.The unix process implementation has a
ReaderWriterLockSlimfor which it takes a reader lock while starting processes, and a writer lock while reaping them.When
execvefails withETXTBSYwe can obtain and release the writer lock and then try to fork+exec once more. That way we ensure all pending forks got past exec.That should be a way to fix this.
You can assign this to me.
Retry installer Process.Start if ETXTBSY (#59765)
Retry installer Process.Start if ETXTBSY (#59765)
[-]Modifying a file and then executing it can fail in on Linux[/-][+]Modifying a file and then executing it can fail on Linux[/+]fs: don't block i_writecount during exec
fs: don't block i_writecount during exec
fs: don't block i_writecount during exec
agocke commentedon Aug 20, 2026
Closing, as this was resolved in the kernel by https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2a010c412853
agocke commentedon Aug 20, 2026
Never mind, apparently that fix was reverted in a later commit. No way around this for now.