mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:50:37 +00:00
mining: handle negative and NaN wait timeouts #36940
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36940
Loading…
Reference in a new issue
No description provided.
Delete branch "11-26-mining-timeout-nan"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
An ipc client can pass any timeout to
waitTipChanged, but the code assumes it is never negative:On debug builds, a negative or NaN timeout fails this check and aborts the node. I reproduced this with a timeout of -1.0.
On release builds the check is skipped. Negative values are clamped to zero, but NaN is not, because every comparison with NaN is false. It ends up as the deadline passed to
wait_until.waitNexthas no Assume, but it also doesn't check its timeout. A NaN timeout waits one extra fee tick (about one second) before returning.waitTipChangedandwaitNextnow throw an error for negative or NaN timeouts. The internalAssumeinWaitTipChangedstill checks internal callers. The interface documents the errors, and the functional test covers rejection of negative and NaN timeouts and acceptance of positive infinity in both methods.I'm not aware of any client that sends these values (sv2-tp, for example, requires an interval of at least one second), so this is hardening against bad input from ipc clients.
This is similar to #36520, which handles invalid arguments to the Chain interface for the same reason.
Links
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
No conflicts as of last run.
LLM Linter (✨ experimental)
Possible places where comparison-specific test macros should replace generic comparisons:
test/functional/interface_ipc_mining.py:assert template8 is None→ Preferassert_equal(template8, None).test/functional/test_framework/ipc_util.py:assert wait_done >= do_called→ Preferassert_greater_than_or_equal(wait_done, do_called).2026-11-30 15:01:37
🔵 Suggestion · The
waitNextinfinity test stops before the deadline loop checkThe
waitNextinfinity case interrupts after 0.1 seconds, so a regression that returns after the first one-second fee tick would still pass.Location:
test/functional/interface_ipc_mining.py:358(head).🔵 Suggestion · Callable interrupts leave their IPC errors unobserved
wait_and_dodiscards promises returned by callable interrupts, so a rejected interrupt can leave the new infinite waits pending instead of propagating its error; no hang was observed.Location:
test/functional/test_framework/ipc_util.py:55(head).Reviewed:
d293ef600dbe· Review report · Previous reportspermalink
🐙 This pull request conflicts with the target branch and needs rebase.
Concept ACK
Thanks.
Since both negative values and NaN indicate a broken client, I prefer returning an error message, rather than clamping to 0.
Might as well also cover
"-inf"(rejected) and"inf"(accepted).Done.
-infis now tested alongside -1.0 and NaN as rejected.Thanks, done.
waitTipChangedandwaitNextnow throw an error for negative or NaN timeouts instead of clamping them.🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/45207773992/job/137808054907LLM reason (✨ experimental): CI failed because IWYU added a missing
<compare>include tosrc/node/interfaces.cpp, leaving an uncommitted diff that the check rejects.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
Code review ACK
758ae2c916. This stops an IPC client from crashing debug builds of the node by passing a negative or NaN timeout towaitTipChanged, and from turning a NaN timeout into await_untildeadline in release builds. Rejecting these values innode/interfaces.cpp, as Sjors suggested, is the same way #36520 handles invalidChainarguments, and it lets the internalAssumestay.This issue was also reported privately in two security audits: [1] found the negative and NaN timeout problem and reproduced the abort, and [2] suggested rejecting non-finite values when libmultiprocess reads any duration from the wire, which could stop this kind of bug from coming back with new duration parameters.
In commit "mining: handle negative and NaN waitTipChanged timeouts" (
758ae2c916)The iwyu CI job is failing because the new
timeout >= 0mscomparisons need<compare>. Adding#include <compare>after#include <atomic>a few lines above should fix it. That one line is the whole diff the job prints.In commit "mining: handle negative and NaN waitTipChanged timeouts" (
758ae2c916)nit: could say why the comparison is negated, here and in
waitTipChanged, so it doesn't get simplified totimeout < 0ms, which would accept NaN:In commit "mining: handle negative and NaN waitTipChanged timeouts" (
758ae2c916)The commit message and PR description still describe the previous push: the commit says negative and NaN timeouts are treated as zero, and the PR description says the Assume is removed. Now that these calls throw and the Assume stays, both should be updated. The commit subject could also mention
waitNext, for example "mining: reject negative and NaN wait timeouts from IPC clients", and NaN is spelled "Nan" in two places.Thanks, added after
Thanks, added a comment at both checks explaining that the negated comparison also catches NaN.
Made the changes here, updated both. They now say that
waitTipChangedandwaitNextreject negative and NaN timeouts. The internal Assume staysThanks for the reviews and for sharing the audit context. made the changes for iwyu and a comment at both checks explaining why the negated comparison catches NaN.
also updated the commit message and pr description to reflect that both methods reject invalid timeouts and the internal
Assumestays.Thanks for the reviews, resolved the iwyu issues and updated the commit and pr descriptions to match the recent changes
tACK
2bfe24e3adCode review ACK
2bfe24e3ad. This stops IPC clients from crashing debug builds of the node with negative or NaN wait timeouts, and from turning a NaN timeout into await_untildeadline in release builds.Thanks for the quick updates, this addresses all my earlier comments. One test suggestion inline: the new infinite-timeout checks don't test what they're meant to, because of how the interrupt is sent.
In commit "mining: reject negative and NaN wait timeouts from IPC clients" (
2bfe24e3ad)These "Infinite timeout is accepted" tests, here and in the
waitTipChangedtest above, check that an infinite timeout isn't rejected, but not that the call actually waits. They would also pass if an infinite timeout madewaitNextorwaitTipChangedreturn immediately, for example because it was clamped to zero.template6.interruptWait()is evaluated as an argument towait_and_do, and pycapnp sends the request right then, so the interrupt reaches the node before the wait starts and the wait returns immediately. I measured both waits returning in under 4ms instead of afterwait_and_do's 0.1s sleep. The Ralph review pointed out thewaitNextcase.Passing the method without calling it (
template6.interruptWaitrather thantemplate6.interruptWait()) letswait_and_docall it after the sleep. To catch the problem,wait_and_docould take an optionalcheck_orderargument that asserts the wait finished afterdo_fnwas called, used here for the two new tests. I checked that this passes with the current code and fails withwait finished 0.097s before do_fn was calledifwaitNexttreats an infinite timeout as zero:The interrupt tests already on master have the same problem: the
interrupt()test forwaitTipChanged, thecreateNewBlockcooldown test and theinterruptWait()test all pass the IPC call itself, so the interrupt is sent before the wait starts. The cooldown test also has no cooldown to interrupt: the previous step submits the announced block, so the tip has caught up. With the interrupt sent at the right time,createNewBlockreturns a template and the test fails; announcing a header for a new block first fixes it.A follow-up could fix these three tests, make
check_orderthe default so everywait_and_docaller gets the check, and requiredo_fnto be a callable (awaiting what it returns) so an IPC call can't be passed directly again. I tried that version with all five call sites passing the method uncalled and the cooldown test fixed, and the test passes. That could be done here as well, or separately.Good point, I checked this locally by timing the wait in the infinite timeout tests. With the interrupt passed as a call the wait returned in about 1ms, and after passing it uncalled it took about 105ms, so it now actually waits for the interrupt.
Added your
check_orderchange in a separate commit and used it for both tests, also added you as the co author. ThanksThe existing interrupt tests have the same issue, so they could use
check_ordertoo. Making it the default and only accepting a callable would also stop this from coming back. I'd prefer to do that in a follow-up so this pr stays focused on the timeout handling.Thanks for the review @ryanofsky, split the test change into a separate commit that adds
check_ordertowait_and_do, and used it for both infinite timeout tests so they now check the wait actually lasts until the interrupt. Will open a follow-up for the existing interrupt testsCode review ACK
525d69e3f9. This stops IPC clients from crashing debug builds of the node with negative or NaN wait timeouts, and from turning a NaN timeout into await_untildeadline in release builds.Thanks for adding the
check_ordercommit! Left some possible followup suggestions but no changes need to be made here.In commit "mining: reject negative and NaN wait timeouts from IPC clients" (
525d69e3f9)Since IPC clients can see this change, it could use a release note, like the one in the 32.1 notes for #33966 ("The IPC mining interface now rejects out-of-range block template options instead of silently clamping them"). The Ralph review suggested this too. Maybe a
doc/release-notes-36940.mdlike:re: https://github.com/bitcoin/bitcoin/pull/36940#discussion_r4712425285
Makes sense, thanks for applying this and for checking the timing. Doing the existing tests separately keeps this PR focused, and the cooldown test needs its setup fixed as well, which is unrelated to timeouts.
The Ralph review points out two more things that would fit the follow-up:
wait_and_docallsdo_fnwithout awaiting what it returns, so an error from the interrupt call is dropped, and with an infinite timeout a broken interrupt would make the test hang instead of fail, which anasyncio.wait_fordeadline around those waits would prevent.Thanks, added it here with some slight changes. It documents the behaviour this pr changes, so it made more sense here than in the follow up.
Thanks for the review @ryanofsky, added a release note in a separate commit. The test suggestions will go in the follow up.
ACK
d293ef600dtACK
d293ef600dCode review ACK
d293ef600d. This stops IPC clients from crashing debug builds of the node with negative or NaN wait timeouts, and from turning a NaN timeout into await_untildeadline in release builds.Only change since my last review is the new release note, which looks good and describes the old behavior accurately. Thanks for adding it!
For the follow-up, the Ralph review has another idea: the infinite
waitNexttest could passsleep_time=fee_ticks_wait_sso the interrupt comes after the first fee tick, which would catch a wait that ends after one tick instead of running until interrupted.