Skip to content

dgb: NodeRPC calls have a deadline (BTC's DeadlineStream) - #2380

Merged
frstrtr merged 8 commits into
masterfrom
dgb/rpc-timeouts
Nov 29, 2026
Merged

frstrtr merged 8 commits into
masterfrom
dgb/rpc-timeouts

Conversation

@frstrtr

@frstrtr frstrtr commented Nov 29, 2026

Copy link
Copy Markdown
Owner

DGB NodeRPC calls to digibyted have a deadline, as BTC's NodeRPC does. Stacked on #2369.

  • Every request (write and read) runs on a stream that bounds each read_some/write_some by one absolute deadline per call: the socket is in user non-blocking mode and the wait for readiness is poll() with the time left, failing with timed_out once it is spent. A kernel SO_RCVTIMEO does not bound asio's synchronous recv (asio waits in poll(fd, -1) on EAGAIN), so it is not used.
  • A request that times out closes the stream; the next call reconnects. A request past its deadline is not re-sent.
  • Send(), close_stream() and the socket setup take dgb: NodeRPC serves one request at a time on its one stream #2369's I/O mutex.

Tests: a peer that accepts and never answers (the call returns timed_out within the deadline, the stream is closed, the next call reconnects); a peer that answers slowly within the deadline (the call succeeds); concurrent calls with the I/O mutex. All DGB targets 875/875; oracle harness both directions PASS.

The digibyted RPC connection is one beast HTTP stream, used from the
stratum IO thread (getblocktemplate, submitblock) and from the HTTP thread
(getblockheader for the dashboard), with nothing ordering them: two
requests at once could interleave their writes and reads on the socket
and take each other's replies. A recursive mutex (m_io_mutex) now covers
every use of the stream, the request and the connected flag: Send (the
write and the read of one request, and its reconnect), connect and its
resolve/connect handlers, reconnect and sync_reconnect. Recursive, because
the connect check and a failed request's reconnect send from inside it.

Pinned: DgbNodeRpc.TwoThreadsGetTheirOwnRepliesOnOneConnection (rides
dgb_coin_node_seam_test): two threads, 200 requests each, on one
keep-alive connection to a local JSON-RPC server; every reply is the
sender's own. Clean under TSan.
The KAT's server looped back to a blocking accept after the client's
connection closed, and closing the acceptor from the test thread does not
interrupt it on Linux, so the join hung. It serves one connection and
returns when the client closes it. Test only.
…lock; the KAT fails fast

- The second thread on the NodeRPC stream is the sharechain compute
  thread, under the tracker lock, when a share is a block
  (won_block_dispatch submit_block_hex, then record_found_block's
  getblockheader). It runs against the IO thread (the stratum
  submitblock, the GBT tip, the block-verify getblockheader). The comment
  in rpc.hpp and the KAT header say so; 5531693 named an HTTP thread.
- The async_connect handler returns on operation_canceled before it
  takes m_io_mutex.
- KAT DgbNodeRpc.TwoThreadsGetTheirOwnRepliesOnOneConnection: the fake
  server caches its port and closes the acceptor right after its one
  accept, so a reconnect is refused at once; the test has a 60 s ctest
  TIMEOUT. The worker flag that selects getmininginfo is named
  mininginfo.
As DASH's NodeRPC (impl/dash/coin/rpc.cpp):
- apply_socket_timeouts: after every connect (the async connect before
  check()'s first request, and sync_reconnect), the socket back in
  blocking mode with SO_RCVTIMEO / SO_SNDTIMEO of RPC_IO_TIMEOUT_SECONDS
  (12). beast's synchronous read and write ignore tcp_stream's
  expires_after, and asio's non_blocking flag (left set by async_connect)
  makes the kernel timeouts do nothing. A digibyted that keeps the
  socket open and sends nothing no longer holds Send() -- and m_io_mutex,
  so the other thread too -- forever. An inactivity bound per operation:
  a large template arriving steadily does not trip it. POSIX only.
- close_stream after a request's last attempt failed: its reply may still
  come, and on the same connection the next request would read it (the
  id is the constant "curltest"; replies are not matched to requests).
  The next request reconnects.
- set_io_timeout_sec for a shorter deadline (tests).

Pinned: DgbNodeRpc.ASilentDaemonTimesOut (two attempts, each bounded,
one reconnect between), ALateReplyIsNotTheNextRequests (a reply past the
deadline is not the next request's).
The kernel SO_RCVTIMEO / SO_SNDTIMEO on a blocking socket does not bound
asio's synchronous I/O: its recv takes the EAGAIN the kernel timeout gives
for would_block and waits in poll(fd, -1), as long as the daemon does
(DgbNodeRpc.ASilentDaemonTimesOut hung). As BTC's NodeRPC (rpc.cpp
DeadlineStream):
- apply_socket_timeouts puts the socket in user non-blocking mode, after
  every connect;
- Send() reads and writes through DeadlineStream, which waits for
  readiness (poll) for at most the time left of ONE deadline for the call
  (RPC_IO_TIMEOUT_SECONDS, 30; set_io_timeout), its retry included, and
  fails with timed_out once it is spent;
- past the deadline the request is not re-sent (the daemon has it), and
  the stream is closed, so its late reply is never read as the next
  request's; any other write or read failure retries once after a
  reconnect, as before.

Pinned: DgbNodeRpc.ASilentDaemonTimesOut (returns at the deadline, one
connection), ALateReplyIsNotTheNextRequests (the next request reconnects
and reads its own reply).
#2369 refreshed onto master (the Windows smoke step, #2367, among it).
@frstrtr
frstrtr changed the base branch from dgb/rpc-send-mutex to master November 29, 2026 14:32
#2369 is on master (76714dc); its files are this branch's, with the
NodeRPC deadline on top.
@frstrtr
frstrtr merged commit c9921af into master Nov 29, 2026
54 checks passed
frstrtr added a commit that referenced this pull request Nov 29, 2026
master holds #2369, #2372, #2380 and #2376. main_dgb_wiring_test.cpp keeps
both sides' new tests (the header-chain rules and the single stratum
listener; the mint's block body and the GBT poller).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant