util: Make files opened with fdbridge::fopen noninheritable #36677

Open
SirECMG wants to merge 2 commits from u/sirecmg/36668-fopen-inheritable-fix into master AGit
SirECMG commented 2026-10-27 18:51:28 +00:00 (Migrated from github.com)

Fixes #36668

Use the 'e' mode on POSIX to set close-on-exec and the 'N' mode on windows to prevent file handle inheritance by child processes.

This PR is still in WIP... until I add some test cases and verify that it works on Windows and Mac. I've only tested it on Linux (OpenSUSE Tumbleweed)

Fixes #36668 Use the 'e' mode on POSIX to set close-on-exec and the 'N' mode on windows to prevent file handle inheritance by child processes. This PR is still in WIP... until I add some test cases and verify that it works on Windows and Mac. I've only tested it on Linux (OpenSUSE Tumbleweed) <!-- *** Please remove the following help text before submitting: *** Pull requests may be closed immediately if they: - do not have a rationale and clear improvement - do not adhere to doc/AI_POLICY.md GUI-related pull requests should be opened against https://github.com/bitcoin-core/gui first. See CONTRIBUTING.md --> <!-- Please provide clear motivation for your patch and explain how it improves Bitcoin Core user experience or Bitcoin Core developer experience significantly: * Any test improvements or new tests that improve coverage are always welcome. * All other changes should have accompanying unit tests (see `src/test/`) or functional tests (see `test/`). Contributors should note which tests cover modified code. If no tests exist for a region of modified code, new tests should accompany the change. * Bug fixes are most welcome when they come with steps to reproduce or an explanation of the potential issue as well as reasoning for the way the bug was fixed. * Features are welcome, but might be rejected due to design or scope issues. If a feature is based on a lot of dependencies, contributors should first consider building the system outside of Bitcoin Core, if possible. * Refactoring changes are only accepted if they are required for a feature or bug fix or otherwise improve developer experience significantly. For example, most "code style" refactoring changes require a thorough explanation why they are useful, what downsides they have and why they *significantly* improve developer experience or avoid serious programming bugs. Note that code style is often a subjective matter. Unless they are explicitly mentioned to be preferred in the [developer notes](/doc/developer-notes.md), stylistic code changes are usually rejected. --> <!-- Bitcoin Core has a thorough review process and even the most trivial change needs to pass a lot of eyes and requires non-zero or even substantial time effort to review. There is a huge lack of active reviewers on the project, so patches often sit for a long time. -->
DrahtBot commented 2026-10-27 18:51:39 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

No conflicts as of last run.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36677 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36677.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36677#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Review failed because required verification did not complete.

Earlier concerns remain unresolved; see the full report for their current assessments.

Review coverage was incomplete.


Reviewed: 17ea8dd8185c · Full review report · Previous reports

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:6f232c871bca65facadc248ad7ecd634f87b0b5f6936bc30593af890589888f1:0cfbbc826fdd30e42f35c9a5b7d389c0902089c94f2400654bb32123e69823f6 --> <!-- ralph-base:6ed5aad06111177c39f9b9f07a52634476380ae1 --> <!-- ralph-head:17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4 --> <!-- ralph-case:93efc45934ff3bbe1bfa7981bcdc2aace43663a33a4de5bd081f08e1d3a3aed6 --> Review failed because required verification did not complete. Earlier concerns remain unresolved; see the full report for their current assessments. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`17ea8dd8185c`](<https://git.fish.foo/bitcoin/bitcoin/commit/17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4>) · [Full review report](<https://review.fish.foo/traces/36677-17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4-1721-22.html>) · [Previous reports](<https://review.fish.foo/traces/36677/>)
hebasto (Migrated from github.com) reviewed 2026-10-27 21:20:21 +00:00
hebasto (Migrated from github.com) commented 2026-10-27 21:20:09 +00:00

Is "e" available on all non-Windows systems?

Is "e" available on all non-Windows systems?
DrahtBot commented 2026-10-27 22:25:39 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task macOS native: https://github.com/bitcoin/bitcoin/actions/runs/40583887664/job/122476509348
LLM reason (✨ experimental): CI failed because fs_tests/fsbridge_fopen_noninheritable failed its check that the file descriptor has the close-on-exec flag set.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `macOS native`: https://github.com/bitcoin/bitcoin/actions/runs/40583887664/job/122476509348</sub> <sub>LLM reason (✨ experimental): CI failed because `fs_tests/fsbridge_fopen_noninheritable` failed its check that the file descriptor has the close-on-exec flag set.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
hebasto (Migrated from github.com) reviewed 2026-10-28 08:57:37 +00:00
hebasto (Migrated from github.com) commented 2026-10-28 08:57:37 +00:00

Unfortunately, the consecutive fopen and fcntl(..., FD_CLOEXEC) calls are not atomic, which is a race in a multithreaded process.

Unfortunately, the consecutive `fopen` and `fcntl(..., FD_CLOEXEC)` calls are not atomic, which is a race in a multithreaded process.
ValeraFinebits (Migrated from github.com) reviewed 2026-10-28 13:49:56 +00:00
ValeraFinebits (Migrated from github.com) commented 2026-10-28 13:49:56 +00:00

Could we keep the original e approach on Linux and handle macOS separately with open using O_CLOEXEC, followed by fdopen?

Could we keep the original `e` approach on Linux and handle macOS separately with `open` using `O_CLOEXEC`, followed by `fdopen`?
ValeraFinebits commented 2026-10-28 14:07:16 +00:00 (Migrated from github.com)

I tested fsbridge::fopen at ca864d2fc1 with a standalone MSVC test using fs.cpp. The file was opened in append mode ("a"), and handle inheritance was enabled in CreateProcessW. After the parent closed the file, DeleteFileW failed with ERROR_SHARING_VIOLATION on the pre-PR code. With this change, deletion succeeded while the child was still alive.

The compilation errors in fs_tests.cpp prevent test_bitcoin from building on Windows.

I tested `fsbridge::fopen` at `ca864d2fc1` with a standalone MSVC test using `fs.cpp`. The file was opened in append mode (`"a"`), and handle inheritance was enabled in `CreateProcessW`. After the parent closed the file, `DeleteFileW` failed with `ERROR_SHARING_VIOLATION` on the pre-PR code. With this change, deletion succeeded while the child was still alive. The compilation errors in `fs_tests.cpp` prevent `test_bitcoin` from building on Windows.
SirECMG (Migrated from github.com) reviewed 2026-10-28 16:45:36 +00:00
SirECMG (Migrated from github.com) commented 2026-10-28 16:45:36 +00:00

Yes, you are right, I will try to find a solution on how I can make it safe under multi threaded processes.

Yes, you are right, I will try to find a solution on how I can make it safe under multi threaded processes.
SirECMG (Migrated from github.com) reviewed 2026-10-28 18:26:42 +00:00
SirECMG (Migrated from github.com) commented 2026-10-28 18:26:42 +00:00
I changed implementation into three cases 1. linux 3. windows 4. posix ## references/docs - linux - https://man7.org/linux/man-pages/man3/freopen.3.html - windows - https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170 - UNIX - https://www.flyriver.com/g/implement-file-handle-inheritance?auth=1793210997639
ValeraFinebits (Migrated from github.com) reviewed 2026-10-28 18:37:06 +00:00
ValeraFinebits (Migrated from github.com) left a comment

typos

typos
ValeraFinebits (Migrated from github.com) commented 2026-10-28 18:33:27 +00:00
            flags |= (read_write ? O_RDWR :  O_WRONLY) | O_CREAT | O_TRUNC;
```suggestion flags |= (read_write ? O_RDWR : O_WRONLY) | O_CREAT | O_TRUNC; ```
ValeraFinebits (Migrated from github.com) commented 2026-10-28 18:34:23 +00:00
        (void)::lseek(fd, 0, SEEK_END);
```suggestion (void)::lseek(fd, 0, SEEK_END); ```
SirECMG commented 2026-10-28 18:44:25 +00:00 (Migrated from github.com)

Thanks @ValeraFinebits. I fixed the typos. Let me know what you think.

Thanks @ValeraFinebits. I fixed the typos. Let me know what you think.
ValeraFinebits commented 2026-10-28 18:49:41 +00:00 (Migrated from github.com)

Thanks @ValeraFinebits. I fixed the typos. Let me know what you think.

Could we also add <cstring> for std::strchr?

> Thanks @ValeraFinebits. I fixed the typos. Let me know what you think. Could we also add `<cstring>` for `std::strchr`?
SirECMG commented 2026-10-28 19:06:57 +00:00 (Migrated from github.com)

Added the missing include!

Added the missing include!
winterrdog (Migrated from github.com) reviewed 2026-10-28 20:30:43 +00:00
winterrdog (Migrated from github.com) left a comment

could you please squash your commits into two: one for the unit test and one for the fix ?

see: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits

could you please squash your commits into two: _one for the unit test and one for the fix_ ? see: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits
ValeraFinebits commented 2026-10-28 21:07:23 +00:00 (Migrated from github.com)

Tested on Windows at 0c9ab56: the new test passes with the fix and fails on the HANDLE_FLAG_INHERIT check when I revert the Windows change in fs.cpp. Restoring the fix makes it pass again.

Tested on Windows at `0c9ab56`: the new test passes with the fix and fails on the `HANDLE_FLAG_INHERIT` check when I revert the Windows change in `fs.cpp`. Restoring the fix makes it pass again.
SirECMG commented 2026-10-28 21:30:56 +00:00 (Migrated from github.com)

could you please squash your commits into two: one for the unit test and one for the fix ?

see: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits

I performed a squash to clean up the git history, as asked.

> could you please squash your commits into two: _one for the unit test and one for the fix_ ? > > see: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits I performed a squash to clean up the git history, as asked.
winterrdog commented 2026-10-30 11:59:39 +00:00 (Migrated from github.com)

seems like this fixes #36668. you can add Fixes #36668 to the PR description to have it auto-close on merge

seems like this fixes #36668. you can add `Fixes #36668` to the PR description to have it auto-close on merge
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36677
No description provided.