util: manage SpawnProcess socket descriptors with RAII #395

pull javierpmateos wants to merge 1 commits into bitcoin-core:master from javierpmateos:raii-socket-fds changing 2 files +36 −21
  1. javierpmateos commented at 2:29 PM on November 14, 2026: contributor

    Follow-up to #394, based on ryanofsky's suggestion to use kj::AutoCloseFd instead of handling descriptor cleanup manually.

    SpawnProcess() could leak file descriptors if the second SocketPair(), connect_info_to_args, or MakeArgv() threw an exception.

    This change wraps the descriptors in kj::AutoCloseFd as soon as they're created, releasing them when they're explicitly closed or handed off. It also uses RAII in the SocketPair() fallback for platforms without SOCK_CLOEXEC. The existing close error handling and child behavior are preserved.

    Testing:

    • Added a test that throws from connect_info_to_args and checks that the socket is closed. It fails on master and passes with this change.
    • All 30 mptest cases pass, both normally and with SOCK_CLOEXEC disabled.
    • Checked with strace: when the callback throws, all four descriptors are closed with this change, while master leaves them open.

    There are two minor differences: the fallback's fcntl error now includes the full expression, and on error paths two descriptors are closed after killing the child rather than before. Neither changes the intended behavior.

    This doesn't address unexpected exceptions after fork() that could leave the child running. ReadSpawnResult() can't throw today, so this is only theoretical.

    I used an AI coding assistant for the implementation. I reviewed the diff and ran the tests myself.

  2. util: manage SpawnProcess socket descriptors with RAII
    SpawnProcess could leak file descriptors if the second SocketPair(),
    connect_info_to_args, or MakeArgv threw an exception.
    
    Use kj::AutoCloseFd to take ownership of the descriptors as soon as
    they're created. Release them when closing or returning them, keeping
    the existing error handling and child behavior unchanged. Do the same
    in the SocketPair() fallback path used when SOCK_CLOEXEC is not
    available.
    
    Add a test that throws from the callback and checks that its socket is
    closed.
    2f6e4323d3
  3. DrahtBot commented at 2:29 PM on November 14, 2026: none

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK ViniciusCestarii

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. ViniciusCestarii commented at 4:59 PM on November 16, 2026: contributor

    tACK 2f6e4323d332f7fabac53e72b9f57135e2916e9f

    Tested with mpexamples trying to make it faill closing fds. It is much cleaner with RAII


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/libmultiprocess. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-17 18:30 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me