mining: handle negative and NaN wait timeouts

bitcoin/bitcoin: PR #36940

2 suggestions

Optional improvements

Link

[suggestion] · Infinite-timeout tests can consume an interrupt sent before the wait

The new IPC tests establish that positive infinity is accepted, but an interrupt sent before the wait can bypass blocking and leave premature-timeout regressions undetected.

Location: test/functional/interface_ipc_mining.py:171 (head).

Details and suggested fix

Both mining.interrupt() and template6.interruptWait() create IPC requests while Python evaluates the arguments to wait_and_do, whereas the wait coroutine bodies have not started. The helper delays awaiting the interrupt promises, not creating the requests. If the interrupt is already latched, WaitTipChanged returns before its deadline-based wait, and WaitAndCreateNewBlock returns from its first predicate check without blocking. A regression that treated infinity as an expired timeout could therefore still pass these assertions. The existing interrupt tests use the same pattern. Start each infinity wait as a task, assert that it remains pending, and only then create and await the interrupt request. For waitNext, keep it pending beyond the one-second fee tick to exercise continuation past that tick. Retain the interrupted-result assertions and use a bounded watchdog.


Link

[suggestion] · Negative-timeout compatibility change lacks a release note

Both mining IPC waits now reject negative timeouts that previously acted as polls, with waitTipChanged limited to release builds; no current caller sending these inputs was identified, but the compatibility change lacks a release note.

Location: src/node/interfaces.cpp:929 (head).

Details and suggested fix

Previously, a slightly negative timeout made BlockTemplate.waitNext perform one immediate tip/fee check in every build type, returning no template if neither condition was met. Mining.waitTipChanged clamped negative values to zero in release builds, while debug builds aborted. A client computing a remaining interval as deadline - now can therefore receive an IPC exception where it previously received a poll result. The new behavior is documented in mining.h, but a short release note would also make this compatibility change visible to client authors. State that both methods now reject negative and NaN timeouts, following the existing release-note precedent for rejecting mining options that were previously clamped.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Ralph's inferences

Acceptance of positive infinity is consistent with the interfaces' existing 'wait forever' semantics, but the inspected evidence does not establish a separately negotiated non-finite-input contract. Both interfaces document an indefinite default; waitTipChanged expresses its default using MillisecondsDouble::max(), which is a large finite value rather than positive infinity.

The compatibility distinction is between preserving an existing way to request an effectively indefinite, interruptible wait and claiming that IEEE positive infinity was expressly promised. The pinned base wrappers forwarded timeouts without rejecting positive infinity, and both underlying implementations already accommodated it. This supports describing acceptance as preserved behavior, while leaving its historical contractual status unproven.

WaitTipChanged separates an effectively indefinite request from the representable deadline passed to the waiting machinery. It caps timeouts above 100 years before constructing the deadline; its code comment explicitly identifies avoiding undefined behavior in std::chrono as the purpose. Positive infinity therefore becomes a finite deadline rather than reaching wait_until unchanged.

This explains why accepting infinity need not mean performing an infinite-duration conversion in this interface. The cap preserves practical 'forever' behavior while bounding deadline arithmetic. The wait remains responsive to tip changes, shutdown and explicit interruption. Base and head retain the same mechanism.

WaitAndCreateNewBlock accommodates infinity differently: its floating-point overall deadline can remain infinite, while each condition-variable wait uses the earlier of that deadline and a one-second tick. With positive infinity, the actual wait target is the finite tick and the overall timeout comparison continues to permit waiting. The code explains the tick as limiting expensive fee checks to once per second.

The absence of WaitTipChanged's 100-year cap is therefore not, by itself, evidence of a different acceptance policy. The two implementations bound the deadline handed to the waiting machinery at different places: one caps the request, while the other repeatedly selects a finite per-iteration target. Tip changes, qualifying fee increases, shutdown and interruption can still end the latter wait; infinity disables the overall timeout rather than these other exit conditions.