Skip to content

Modifying a file and then executing it can fail on Linux #58964

Open

Description

@agocke

golang/go#22315 sets out the issue.

Fundamentally, this seems like an inescapable race in multi-threaded programs on Linux.

My understanding of the failure is this:

  1. A process with two threads wants to write a file and then execute it.
  2. Thread 1 opens a file for writing
  3. Thread 2 issues a call to create a new process and completes a fork call.
  4. The new process inherits all file descriptors open in Process 1, including the file opened by Thread 1.
  5. Thread 1 finishes writing and closes the file descriptor.
  6. Thread 1 attempts to execute the file.
  7. 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

  1. 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

  1. ghost added
    untriaged
    on Sep 10, 2021
  2. jkotas commented on Sep 10, 2021

    @jkotas
    Member

    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...

  3. jkotas commented on Sep 10, 2021

    @jkotas
    Member

    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.

  4. tmds commented on Sep 13, 2021

    @tmds
    Member

    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.

    Have you seen the issue occur? On what platform?

  5. added this to the Future milestone on Sep 13, 2021
  6. removed
    untriaged
    on Sep 13, 2021
  7. agocke commented on Sep 13, 2021

    @agocke
    MemberAuthor

    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.

  8. tmds commented on Sep 13, 2021

    @tmds
    Member

    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.

    That should be a way to fix this.

    You can assign this to me.

  9. ghost added
    in-pr
    on Sep 14, 2021
  10. ghost removed
    in-pr
    on Oct 19, 2021
  11. 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
  12. agocke commented on Aug 20, 2026

    @agocke
    MemberAuthor
  13. agocke commented on Aug 20, 2026

    @agocke
    MemberAuthor

    Never mind, apparently that fix was reverted in a later commit. No way around this for now.

  14. reopened this on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions