{
 "number": 35932,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/35932",
 "title": "ipc: make ipc::disconnectIncoming wait for in-progress calls to complete",
 "author": "ryanofsky",
 "author_association": "MEMBER",
 "created_at": "2026-08-07T14:13:39Z",
 "updated_at": "2026-09-10T08:38:15Z",
 "age_days": 41,
 "draft": true,
 "labels": [
  "IPC",
  "CI failed"
 ],
 "milestone": null,
 "base": "master",
 "head_sha": "451f5455d6a139f7df2bbcd3f27b5143e9fbc65d",
 "head_ref": "pr/diswait",
 "head_repo": "ryanofsky/bitcoin",
 "head_history": [
  {
   "t": "2026-08-12T20:17:52Z",
   "sha": "451f5455d6a139f7df2bbcd3f27b5143e9fbc65d"
  }
 ],
 "additions": 339,
 "deletions": 21,
 "changed_files": 5,
 "commit_count": 5,
 "size_bucket": "M",
 "mergeable_state": "unstable",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {},
   "conflicts": [
    {
     "number": 36097,
     "title": "mining: replace interrupt methods with cancellation arguments",
     "author": "xyzconstant"
    },
    {
     "number": 32387,
     "title": "ipc: add windows support",
     "author": "ryanofsky"
    },
    {
     "number": 29409,
     "title": "multiprocess: Add capnp wrapper for Chain interface",
     "author": "ryanofsky"
    },
    {
     "number": 19461,
     "title": "multiprocess: Add bitcoin-gui -ipcconnect option",
     "author": "ryanofsky"
    },
    {
     "number": 19460,
     "title": "multiprocess: Add bitcoin-wallet -ipcconnect option",
     "author": "ryanofsky"
    },
    {
     "number": 10102,
     "title": "Multiprocess bitcoin",
     "author": "ryanofsky"
    }
   ]
  }
 },
 "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": []
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": true,
  "mergeable_state": "unstable",
  "last_author_activity": "2026-08-12T20:19:43Z",
  "last_reviewer_activity": null,
  "last_reviewer": null,
  "author_silent_days": 35,
  "waiting_on_author_days": 0,
  "days_since_update": 7
 },
 "refs": {
  "mentioned": [],
  "depends_on": [],
  "fixes": [],
  "linked_issues": [],
  "references": [],
  "conflicts": [
   36097,
   32387,
   29409,
   19461,
   19460,
   10102
  ]
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [],
 "body": "This fixes an antithesis bug reported https://github.com/bitcoin/bitcoin/issues/35845 and similar bug reported in https://github.com/bitcoin/bitcoin/issues/33387 where if asynchronous IPC mining calls are made when the node is shutting down it's possible for `assert(m_node.chainman)` to trigger. This happens because the `Ipc::disconnectIncoming` method does not wait for asynchronous calls to complete after it disconnects IPC clients, so they may continue to run as the node is shutting down.\n\nThis PR changes `disconnectIncoming` to wait for asynchronous calls to complete to avoid this issue. It's a draft because it depends on libmultiprocess changes, but should otherwise be ready to review.\n\n**This is based on https://github.com/bitcoin-core/libmultiprocess/pull/335.**",
 "commits": [
  {
   "sha": "4a04be58d0ba036ecf27e8214b29f411f544d369",
   "date": "2026-07-31T19:19:30Z",
   "message": "ipc: add Connection::disconnect() separating teardown from destruction\n\nSplit connection teardown out of ~Connection into an idempotent disconnect()\nmethod, with the destructor delegating to it. This is a behavior-neutral\nrefactor: the same steps run in the same order on destruction.\n\nHaving a separate disconnect() method allows severing a connection while\nkeeping the Connection object alive, which the next commits use to let\nshutdown code wait for in-flight server call bodies to finish after a\ndisconnect (bitcoin/bitcoin#35845). Two details are new:\n\n- disconnect() cancels the m_on_disconnect handlers before severing the\n  connection. Previously they were implicitly canceled when the TaskSet\n  member was destroyed. When disconnect() is called separately from\n  destruction, this is required for correctness: severing the stream\n  completes m_network.onDisconnect(), and the registered handlers (_Serve,\n  ConnectStream) destroy the Connection object out from under the caller.\n\n- disconnect() explicitly releases m_thread_pool and m_thread_map so worker\n  thread teardown happens at disconnect time whether or not the object is\n  destroyed right away. Previously this happened implicitly during member\n  destruction.\n\nCo-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
  },
  {
   "sha": "cf7261b35448aa4d871c40c9d6a40043259c11ea",
   "date": "2026-07-31T19:21:48Z",
   "message": "ipc: add Connection::waitDrained() to wait for in-flight server calls\n\nAdd a per-connection ServerObjectTracker counting live ProxyServer objects,\nincremented in the ProxyServerBase constructor and decremented in its\ndestructor, with Connection::waitDrained() blocking until the count reaches\nzero and Connection::pendingServerObjects() exposing it for logging.\n\nDisconnecting a connection cancels the KJ promise of an in-flight call, but a\nC++ server method body already dispatched to a worker thread runs to\ncompletion. Counting live server objects turns Cap'n Proto's object lifetime\nrules into a usable quiescence signal: a ProxyServer object is not destroyed\nuntil its outstanding calls finish (the target capability is kept alive for\nthe duration of a call and pinned by post()/PassField via thisCap()), so\nafter disconnect() the count drains to zero exactly when no server call body\nis still executing. Waiting for that lets shutdown code avoid freeing\napplication state that a still-running call body dereferences\n(bitcoin/bitcoin#35845).\n\nThe tracker is held via shared_ptr by the Connection and by every\nProxyServer object because objects kept alive by in-flight calls can outlive\nthe Connection on some teardown paths (see ~ProxyServerBase), and their\ndestructors must decrement state that is still valid. It must be declared\nbefore m_rpc_system, whose construction creates the bootstrap server object\nthat registers itself with the tracker.\n\nCo-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
  },
  {
   "sha": "a28447e75f33ce1899b20c65c0fe8d097061ccd1",
   "date": "2026-07-31T19:40:26Z",
   "message": "test: cover draining in-flight server call after disconnect\n\nAdd a deterministic mptest regression test for bitcoin/bitcoin#35845: hold a\nserver method body in flight on a worker thread, call\nConnection::disconnect(), and assert that Connection::waitDrained() blocks\nuntil the body finishes and its server object is destroyed. Also covers\ndestroying an already-disconnected connection (~Connection noticing\ndisconnect() has run).\n\nCo-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
  },
  {
   "sha": "a882e0d63f38876a82bc8f187ae84ccfa85bcfbc",
   "date": "2026-08-10T19:37:25Z",
   "message": "Add EventLoop::incoming_connections() that returns std::views::all of the\nm_incoming_connections list. Currently the list holds Connection by value\nso the view yields Connection&. When keepconn+notrack later changes the\nlist to list<shared_ptr<Connection>>, the accessor will be updated to\nreturn a transform view, so Bitcoin Core code that iterates via this\naccessor compiles unchanged across that type change.\n\nCo-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>"
  },
  {
   "sha": "451f5455d6a139f7df2bbcd3f27b5143e9fbc65d",
   "date": "2026-08-10T19:37:25Z",
   "message": "ipc: drain in-flight server calls before shutdown frees node state\n\nFix bitcoin/bitcoin#35845, an assertion failure in MinerImpl::chainman()\nduring shutdown of an IPC-mining node.\n\nShutdown() calls disconnectIncoming() before node.chainman.reset().\nDisconnecting cancels the KJ promise of an in-flight IPC server call, but a\nC++ server method body already dispatched to a libmultiprocess worker thread\nis not interrupted and runs to completion. A still-running body (an in-flight\nMining.checkBlock) could then dereference m_node.chainman after\nchainman.reset() nulled it, aborting on Assert(m_node.chainman).\n\nMake disconnectIncoming() disconnect the non-parent incoming connections,\nwait off the event loop thread for their in-flight server call bodies to\nfinish (Connection::waitDrained), and only then destroy them and return, so\nShutdown() frees node state only once no server code is running. Log when\nthe wait actually blocks so a shutdown hang here is diagnosable.\n\nNo wait is needed for calls parked in waitTipChanged()/waitNext():\nInterrupt() runs before Shutdown() and notifies m_tip_block_cv after setting\nthe shutdown signal, so those return before disconnectIncoming() runs.\n\nIntentional limitations, to keep the fix narrow: m_impl destructors\nscheduled on the async cleanup thread are not waited for, the kept-open\nparent connection is not drained, and new incoming connections can still be\naccepted during shutdown (preventing that needs a listener API, proposed in\nbitcoin-core/libmultiprocess#269).\n\nCo-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
  }
 ],
 "timeline": [
  {
   "t": "2026-08-12T20:17:52Z",
   "kind": "force_push",
   "who": "ryanofsky",
   "commit": "451f5455d6a139f7df2bbcd3f27b5143e9fbc65d"
  },
  {
   "t": "2026-08-12T20:19:43Z",
   "kind": "comment",
   "who": "ryanofsky",
   "assoc": "MEMBER",
   "text": "Updated b0c970e305fde426f4aacc8f8d9ea55ea6e8e210 -> 451f5455d6a139f7df2bbcd3f27b5143e9fbc65d ([`pr/diswait.4`](https://github.com/ryanofsky/bitcoin/commits/pr/diswait.4) -> [`pr/diswait.5`](https://github.com/ryanofsky/bitcoin/commits/pr/diswait.5), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/diswait.4..pr/diswait.5)) using `incoming_connections()` accessor for compatibility with https://github.com/bitcoin-core/libmultiprocess/pull/336"
  }
 ],
 "labels_log": [
  {
   "t": "2026-08-07T14:13:42Z",
   "action": "labeled",
   "label": "IPC",
   "who": "DrahtBot"
  },
  {
   "t": "2026-08-07T16:14:53Z",
   "action": "labeled",
   "label": "CI failed",
   "who": "DrahtBot"
  }
 ],
 "state_log": [],
 "text_chars": 6627,
 "text_tokens_estimate": 1656,
 "changed_paths": [
  "src/ipc/capnp/protocol.cpp",
  "src/ipc/libmultiprocess/include/mp/proxy-io.h",
  "src/ipc/libmultiprocess/include/mp/proxy.h",
  "src/ipc/libmultiprocess/src/mp/proxy.cpp",
  "src/ipc/libmultiprocess/test/mp/test/test.cpp"
 ],
 "files": [
  {
   "path": "src/ipc/capnp/protocol.cpp",
   "add": 50,
   "del": 2
  },
  {
   "path": "src/ipc/libmultiprocess/include/mp/proxy-io.h",
   "add": 151,
   "del": 14
  },
  {
   "path": "src/ipc/libmultiprocess/include/mp/proxy.h",
   "add": 7,
   "del": 0
  },
  {
   "path": "src/ipc/libmultiprocess/src/mp/proxy.cpp",
   "add": 60,
   "del": 5
  },
  {
   "path": "src/ipc/libmultiprocess/test/mp/test/test.cpp",
   "add": 71,
   "del": 0
  }
 ],
 "test_lines": 0,
 "git": {
  "head": "451f5455d6a139f7df2bbcd3f27b5143e9fbc65d",
  "head_matches_backup": true,
  "base": "67efced1fc83a0b7215cc1513e7c4754fee0f12f",
  "commits": [
   {
    "sha": "4a04be58d0",
    "subject": "ipc: add Connection::disconnect() separating teardown from destruction",
    "files": 2,
    "add": 85,
    "del": 18
   },
   {
    "sha": "cf7261b354",
    "subject": "ipc: add Connection::waitDrained() to wait for in-flight server calls",
    "files": 3,
    "add": 129,
    "del": 1
   },
   {
    "sha": "a28447e75f",
    "subject": "test: cover draining in-flight server call after disconnect",
    "files": 1,
    "add": 71,
    "del": 0
   },
   {
    "sha": "a882e0d63f",
    "subject": "Add EventLoop::incoming_connections() that returns std::views::all of the m_incoming_connections list. Currently the list holds Connection by value so the view yields Connection&. When keepconn+notrack later changes the list to list<shared_ptr<Connection>>, the accessor will be updated to return a transform view, so Bitcoin Core code that iterates via this accessor compiles unchanged across that type change.",
    "files": 1,
    "add": 4,
    "del": 0
   },
   {
    "sha": "451f5455d6",
    "subject": "ipc: drain in-flight server calls before shutdown frees node state",
    "files": 1,
    "add": 50,
    "del": 2
   }
  ],
  "patch_truncated": false
 },
 "input_hash": "8b7c8ededcccb1ae",
 "extracted_at": "2026-09-17T16:15:31+00:00"
}