{
 "number": 36677,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/36677",
 "title": "util: Make files opened with fdbridge::fopen noninheritable",
 "author": "SirECMG",
 "author_association": "FIRST_TIME_CONTRIBUTOR",
 "created_at": "2026-10-27T18:51:28Z",
 "updated_at": "2026-10-30T15:15:05Z",
 "description_hash": "ba1520dbb95c1862",
 "age_days": 5,
 "draft": false,
 "labels": [
  "Utils/log/libs"
 ],
 "milestone": null,
 "base": "master",
 "head_sha": "17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4",
 "head_ref": "u/sirecmg/36668-fopen-inheritable-fix",
 "head_repo": "SirECMG/bitcoin",
 "head_history": [
  {
   "t": "2026-10-27T21:06:05Z",
   "sha": "c354037d4455e41ccf56705fa46187c4a3a09114"
  },
  {
   "t": "2026-10-28T18:35:43Z",
   "sha": "9ebc30b17d67d12669babb06ad3549d68664cb97"
  },
  {
   "t": "2026-10-28T18:40:00Z",
   "sha": "f2eac03b2eed5789c8ee5a4657588f535a5c3b04"
  },
  {
   "t": "2026-10-28T18:42:04Z",
   "sha": "30c974f601a9d4b1d987e0e8d0850faa839d38bb"
  },
  {
   "t": "2026-10-28T18:59:17Z",
   "sha": "f2875ebd401a6c8d0f03aaa39e46cf040a2953bf"
  },
  {
   "t": "2026-10-28T19:03:12Z",
   "sha": "e423eb6810bbffc72ecd3739b88ef5af86b012c3"
  },
  {
   "t": "2026-10-28T19:05:05Z",
   "sha": "55f35a25d390dad5c6182653df87edc1fc963f5b"
  },
  {
   "t": "2026-10-28T21:25:49Z",
   "sha": "6a1ff77cfd71ceb9797b31d4545310163b7f4afa"
  },
  {
   "t": "2026-10-29T17:42:48Z",
   "sha": "17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4"
  }
 ],
 "additions": 88,
 "deletions": 4,
 "changed_files": 2,
 "commit_count": 2,
 "size_bucket": "S",
 "mergeable_state": "clean",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {},
   "conflicts": []
  }
 },
 "acks_parsed": {},
 "acks_tally": {
  "ack": 0,
  "stale_ack": 0,
  "concept_ack": 0,
  "approach_ack": 0,
  "nack": 0,
  "concept_nack": 0,
  "approach_nack": 0
 },
 "reviews": {
  "approved": 0,
  "changes_requested": 0,
  "distinct_reviewers": [
   "ValeraFinebits",
   "hebasto",
   "winterrdog"
  ]
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": false,
  "mergeable_state": "clean",
  "last_author_activity": "2026-10-29T17:42:48Z",
  "last_reviewer_activity": "2026-10-30T11:59:39Z",
  "last_reviewer": "winterrdog",
  "author_silent_days": 3,
  "waiting_on_author_days": 2,
  "days_since_update": 2
 },
 "refs": {
  "mentioned": [
   36668
  ],
  "depends_on": [],
  "fixes": [
   36668
  ],
  "linked_issues": [
   {
    "number": 36668,
    "type": "issue",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "util: Make files opened via `fsbridge::fopen()` not inheritable by child processes"
   }
  ],
  "references": [
   {
    "number": 36668,
    "type": "issue",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "util: Make files opened via `fsbridge::fopen()` not inheritable by child processes"
   }
  ],
  "conflicts": []
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [
  "src/util/fs.cpp"
 ],
 "body": "Fixes #36668\n\nUse the 'e' mode on POSIX to set close-on-exec and the 'N' mode on windows to prevent file handle inheritance by child processes.\n\nThis 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)",
 "commits": [
  {
   "sha": "082b38544571db6809541091820d21d8ca9b1f1b",
   "date": "2026-10-29T17:42:25Z",
   "message": "util: Make fsbridge::fopen files non-inheritable"
  },
  {
   "sha": "17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4",
   "date": "2026-10-29T17:42:33Z",
   "message": "test: Verify fsbridge::fopen opens non-inheritable files"
  }
 ],
 "timeline": [
  {
   "t": "2026-10-27T21:06:05Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "c354037d4455e41ccf56705fa46187c4a3a09114",
   "id": "push:c354037d4455e41ccf56705fa46187c4a3a09114"
  },
  {
   "t": "2026-10-27T21:20:09Z",
   "kind": "review_comment",
   "who": "hebasto",
   "assoc": "MEMBER",
   "path": "src/util/fs.cpp",
   "commit": "c354037d4455e41ccf56705fa46187c4a3a09114",
   "in_reply_to": null,
   "text": "Is \"e\" available on all non-Windows systems?",
   "id": "rc:4394300695",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4394300695",
   "hash": "d2317e1c5c7342e6",
   "edited": "2026-10-27T21:20:21Z"
  },
  {
   "t": "2026-10-28T08:57:37Z",
   "kind": "review_comment",
   "who": "hebasto",
   "assoc": "MEMBER",
   "path": "src/util/fs.cpp",
   "commit": "4451b0f6b2a73d5724ab64ebcddaaf2bcc4d0109",
   "in_reply_to": null,
   "text": "Unfortunately, the consecutive `fopen` and `fcntl(..., FD_CLOEXEC)` calls are not atomic, which is a race in a multithreaded process.",
   "id": "rc:4398624471",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4398624471",
   "hash": "34b8a206a0554208",
   "edited": null
  },
  {
   "t": "2026-10-28T13:49:56Z",
   "kind": "review_comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "path": "src/util/fs.cpp",
   "commit": "c354037d4455e41ccf56705fa46187c4a3a09114",
   "in_reply_to": 4394300695,
   "text": "Could we keep the original `e` approach on Linux and handle macOS separately with `open` using `O_CLOEXEC`, followed by `fdopen`?",
   "id": "rc:4401375200",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4401375200",
   "hash": "0f9afbaa6e91aee2",
   "edited": null
  },
  {
   "t": "2026-10-28T14:07:16Z",
   "kind": "comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "text": "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.\n\nThe compilation errors in `fs_tests.cpp` prevent `test_bitcoin` from building on Windows.",
   "id": "c:6471177983",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6471177983",
   "hash": "8248860f8d96b8c9",
   "edited": null
  },
  {
   "t": "2026-10-28T16:45:36Z",
   "kind": "review_comment",
   "who": "SirECMG",
   "assoc": "NONE",
   "path": "src/util/fs.cpp",
   "commit": "4451b0f6b2a73d5724ab64ebcddaaf2bcc4d0109",
   "in_reply_to": 4398624471,
   "text": "Yes, you are right, I will try to find a solution on how I can make it safe under multi threaded processes.",
   "id": "rc:4403445732",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4403445732",
   "hash": "f672ee642e85f71a",
   "edited": null
  },
  {
   "t": "2026-10-28T18:26:42Z",
   "kind": "review_comment",
   "who": "SirECMG",
   "assoc": "NONE",
   "path": "src/util/fs.cpp",
   "commit": "c354037d4455e41ccf56705fa46187c4a3a09114",
   "in_reply_to": 4394300695,
   "text": "I changed implementation into three cases\n\n1. linux\n3. windows\n4. posix\n\n## references/docs\n- linux\n   - https://man7.org/linux/man-pages/man3/freopen.3.html\n\n- windows\n   - https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170\n\n- UNIX\n   - https://www.flyriver.com/g/implement-file-handle-inheritance?auth=1793210997639",
   "id": "rc:4404499220",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4404499220",
   "hash": "319c39c45944a1cf",
   "edited": "2026-10-28T18:38:01Z"
  },
  {
   "t": "2026-10-28T18:33:27Z",
   "kind": "review_comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "path": "src/util/fs.cpp",
   "commit": "9ebc30b17d67d12669babb06ad3549d68664cb97",
   "in_reply_to": null,
   "text": "```suggestion\n            flags |= (read_write ? O_RDWR :  O_WRONLY) | O_CREAT | O_TRUNC;\n```",
   "id": "rc:4404567090",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4404567090",
   "hash": "eafbb19ae4e8f327",
   "edited": "2026-10-28T18:37:06Z"
  },
  {
   "t": "2026-10-28T18:34:23Z",
   "kind": "review_comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "path": "src/util/fs.cpp",
   "commit": "f2eac03b2eed5789c8ee5a4657588f535a5c3b04",
   "in_reply_to": null,
   "text": "```suggestion\n        (void)::lseek(fd, 0, SEEK_END);\n```",
   "id": "rc:4404576433",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#discussion_r4404576433",
   "hash": "d7b224d51d95f6f7",
   "edited": "2026-10-28T18:37:06Z"
  },
  {
   "t": "2026-10-28T18:35:43Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "9ebc30b17d67d12669babb06ad3549d68664cb97",
   "id": "push:9ebc30b17d67d12669babb06ad3549d68664cb97"
  },
  {
   "t": "2026-10-28T18:37:06Z",
   "kind": "review",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "state": "COMMENTED",
   "commit": "bb15d682c5e153122f0b2151b679a195aeef9202",
   "text": "typos",
   "id": "r:5687773266",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#pullrequestreview-5687773266",
   "hash": "865c2d7a02ef557e",
   "edited": null
  },
  {
   "t": "2026-10-28T18:40:00Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "f2eac03b2eed5789c8ee5a4657588f535a5c3b04",
   "id": "push:f2eac03b2eed5789c8ee5a4657588f535a5c3b04"
  },
  {
   "t": "2026-10-28T18:42:04Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "30c974f601a9d4b1d987e0e8d0850faa839d38bb",
   "id": "push:30c974f601a9d4b1d987e0e8d0850faa839d38bb"
  },
  {
   "t": "2026-10-28T18:44:25Z",
   "kind": "comment",
   "who": "SirECMG",
   "assoc": "NONE",
   "text": "Thanks @ValeraFinebits. I fixed the typos. Let me know what you think.",
   "id": "c:6477587666",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6477587666",
   "hash": "37b703d069f42e3a",
   "edited": null
  },
  {
   "t": "2026-10-28T18:49:41Z",
   "kind": "comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "text": "[quoted text omitted]\n\nCould we also add `<cstring>` for `std::strchr`?",
   "id": "c:6477697524",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6477697524",
   "hash": "a9946054b9ee2a39",
   "edited": "2026-10-28T18:52:29Z"
  },
  {
   "t": "2026-10-28T18:59:17Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "f2875ebd401a6c8d0f03aaa39e46cf040a2953bf",
   "id": "push:f2875ebd401a6c8d0f03aaa39e46cf040a2953bf"
  },
  {
   "t": "2026-10-28T19:03:12Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "e423eb6810bbffc72ecd3739b88ef5af86b012c3",
   "id": "push:e423eb6810bbffc72ecd3739b88ef5af86b012c3"
  },
  {
   "t": "2026-10-28T19:05:05Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "55f35a25d390dad5c6182653df87edc1fc963f5b",
   "id": "push:55f35a25d390dad5c6182653df87edc1fc963f5b"
  },
  {
   "t": "2026-10-28T19:06:57Z",
   "kind": "comment",
   "who": "SirECMG",
   "assoc": "NONE",
   "text": "Added the missing include!",
   "id": "c:6478095764",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6478095764",
   "hash": "757b5c5871214b1c",
   "edited": "2026-10-28T19:07:44Z"
  },
  {
   "t": "2026-10-28T20:30:43Z",
   "kind": "review",
   "who": "winterrdog",
   "assoc": "CONTRIBUTOR",
   "state": "COMMENTED",
   "commit": "0c9ab5633d89cd64c52b90ba27b198d10848f842",
   "text": "could you please squash your commits into two: _one for the unit test and one for the fix_ ?\n\nsee: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits",
   "id": "r:5689284835",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#pullrequestreview-5689284835",
   "hash": "c2e019d2f217e98b",
   "edited": null
  },
  {
   "t": "2026-10-28T21:07:23Z",
   "kind": "comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "text": "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.",
   "id": "c:6480706270",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6480706270",
   "hash": "dd65e58e134a95e7",
   "edited": null
  },
  {
   "t": "2026-10-28T21:25:49Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "6a1ff77cfd71ceb9797b31d4545310163b7f4afa",
   "id": "push:6a1ff77cfd71ceb9797b31d4545310163b7f4afa"
  },
  {
   "t": "2026-10-28T21:30:56Z",
   "kind": "comment",
   "who": "SirECMG",
   "assoc": "NONE",
   "text": "[quoted text omitted]\n\nI performed a squash to clean up the git history, as asked.",
   "id": "c:6481166654",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6481166654",
   "hash": "fa280a61ac6d4106",
   "edited": null
  },
  {
   "t": "2026-10-29T17:42:48Z",
   "kind": "force_push",
   "who": "SirECMG",
   "commit": "17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4",
   "id": "push:17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4"
  },
  {
   "t": "2026-10-30T11:59:39Z",
   "kind": "comment",
   "who": "winterrdog",
   "assoc": "CONTRIBUTOR",
   "text": "seems like this fixes #36668. you can add `Fixes #36668` to the PR description to have it auto-close on merge",
   "id": "c:6522173210",
   "url": "https://github.com/bitcoin/bitcoin/pull/36677#issuecomment-6522173210",
   "hash": "95a6a294dd72650c",
   "edited": null
  }
 ],
 "labels_log": [
  {
   "t": "2026-10-27T18:51:33Z",
   "action": "labeled",
   "label": "Utils/log/libs",
   "who": "DrahtBot"
  },
  {
   "t": "2026-10-27T22:25:32Z",
   "action": "labeled",
   "label": "CI failed",
   "who": "DrahtBot"
  },
  {
   "t": "2026-10-30T12:53:15Z",
   "action": "unlabeled",
   "label": "CI failed",
   "who": "DrahtBot"
  }
 ],
 "state_log": [],
 "text_chars": 2596,
 "text_tokens_estimate": 649,
 "changed_paths": [
  "src/test/fs_tests.cpp",
  "src/util/fs.cpp"
 ],
 "files": [
  {
   "path": "src/test/fs_tests.cpp",
   "add": 41,
   "del": 0
  },
  {
   "path": "src/util/fs.cpp",
   "add": 47,
   "del": 4
  }
 ],
 "test_lines": 41,
 "git": {
  "head": "17ea8dd8185cc4ebcdb0b1fae2a0d892459e04c4",
  "head_matches_backup": true,
  "base": "23288073d4e7723bf7559a904bbf322a52d8af55",
  "patch_id": "b30bf17448865d02",
  "commits": [
   {
    "sha": "082b385445",
    "subject": "util: Make fsbridge::fopen files non-inheritable",
    "files": 1,
    "add": 47,
    "del": 4
   },
   {
    "sha": "17ea8dd818",
    "subject": "test: Verify fsbridge::fopen opens non-inheritable files",
    "files": 1,
    "add": 41,
    "del": 0
   }
  ],
  "patch_truncated": false
 },
 "patch_id": "b30bf17448865d02",
 "input_hash": "ca68fe9ebb2f0ca0",
 "extracted_at": "2026-11-02T01:09:49+00:00"
}