test: fix waitTipChanged timeout on slow hosts and use monotonic clock in interface_ipc_mining #36791

pull ViniciusCestarii wants to merge 2 commits into bitcoin:master from ViniciusCestarii:fix-flaky-interface-ipc-mining changing 1 files +6 −6
  1. ViniciusCestarii commented at 3:25 PM on November 6, 2026: contributor

    Follow up suggested in #36731 (comment).

    The first commit fix the timeout on a slow host and the second one use time.monotonic() for durations.

    Note (added after merge): the first commit's failure is not a wall clock problem. The faketime command only reproduces it by making the test slow. See #36791 (review)

    Command to reproduce the errors with a simulated slow wall clock:

    faketime --exclude-monotonic -f "+0 x0.1" build/test/functional/interface_ipc_mining.py
    
  2. test: fix waitTipChanged timeout on slow hosts in interface_ipc_mining 9b6dadb39c
  3. test: use time.monotonic() for durations in interface_ipc_mining 46ee95db49
  4. DrahtBot added the label Tests on Nov 6, 2026
  5. DrahtBot commented at 3:25 PM on November 6, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    Type Reviewers
    ACK nervana21, hebasto, ryanofsky

    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-->

  6. maflcko commented at 3:40 PM on November 6, 2026: member

    Command to reproduce slow host:

    I think this is a "fast host", as the test takes a shorter "wall" time?

  7. nervana21 commented at 7:10 PM on November 6, 2026: contributor

    Concept ACK

  8. ViniciusCestarii commented at 10:52 PM on November 6, 2026: contributor

    I think this is a "fast host", as the test takes a shorter "wall" time?

    You are right. It can be confusing lol. The first commit fixes a wait that takes too long (this is what confused me) and the second fixes durations measured with a wall clock that doesn't match real time. Renamed it to "slow wall clock".

  9. nervana21 commented at 3:03 AM on November 8, 2026: contributor

    tACK 46ee95db49c9fa7f4b80a1e59816660f5ee0c4f4

  10. hebasto commented at 11:30 AM on November 12, 2026: member

    Concept ACK.

    Here is a real-life example of such a failure:

    472/505 - interface_ipc_mining.py failed (exit code 1), Duration: 11 s
    
    stdout:
    2026-11-12T08:24:54.078139Z TestFramework (INFO): PRNG seed is: 2459815776369179361
    2026-11-12T08:24:54.132354Z TestFramework (INFO): Initializing test directory /root/test_runner_₿_🏃_20261112_081320/interface_ipc_mining_36
    2026-11-12T08:24:55.674316Z TestFramework (INFO): Running Mining interface test
    2026-11-12T08:25:01.032939Z TestFramework (INFO): Running Mining interface early startup test
    2026-11-12T08:25:01.957129Z TestFramework (INFO): Running BlockTemplate interface test
    2026-11-12T08:24:48.047514Z TestFramework (ERROR): Unexpected exception:
    Traceback (most recent call last):
      File "/root/bitcoin/test/functional/test_framework/test_framework.py", line 146, in main
        self.run_test()
        ~~~~~~~~~~~~~^^
      File "/root/build/test/functional/interface_ipc_mining.py", line 890, in run_test
        self.run_block_template_test()
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
      File "/root/build/test/functional/interface_ipc_mining.py", line 343, in run_block_template_test
        asyncio.run(capnp.run(async_routine()))
        ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/pkg/lib/python3.13/asyncio/runners.py", line 196, in run
        return runner.run(main)
               ~~~~~~~~~~^^^^^^
      File "/usr/pkg/lib/python3.13/asyncio/runners.py", line 119, in run
        return self._loop.run_until_complete(task)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
      File "/usr/pkg/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
        return future.result()
               ~~~~~~~~~~~~~^^
      File "capnp/lib/capnp.pyx", line 2085, in run
      File "capnp/lib/capnp.pyx", line 2086, in capnp.lib.capnp.run
        return await coro
      File "/root/build/test/functional/interface_ipc_mining.py", line 225, in async_routine
        assert_greater_than_or_equal(time.time() - start, 0.9)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/bitcoin/test/functional/test_framework/util.py", line 109, in assert_greater_than_or_equal
        raise AssertionError("%s < %s" % (str(thing1), str(thing2)))
    AssertionError: -14.01054048538208 < 0.9
    2026-11-12T08:24:48.126115Z TestFramework (INFO): Not stopping nodes as test failed. The dangling processes will be cleaned up later.
    2026-11-12T08:24:48.126338Z TestFramework (WARNING): Not cleaning up dir /root/test_runner_₿_🏃_20261112_081320/interface_ipc_mining_36
    2026-11-12T08:24:48.126451Z TestFramework (ERROR): Test failed. Test logging available at /root/test_runner_₿_🏃_20261112_081320/interface_ipc_mining_36/test_framework.log
    2026-11-12T08:24:48.126749Z TestFramework (ERROR): 
    2026-11-12T08:24:48.127092Z TestFramework (ERROR): Hint: Call /root/bitcoin/test/functional/combine_logs.py '/root/test_runner_₿_🏃_20261112_081320/interface_ipc_mining_36' to consolidate all logs
    2026-11-12T08:24:48.127182Z TestFramework (ERROR): 
    2026-11-12T08:24:48.127254Z TestFramework (ERROR): If this failure happened unexpectedly or intermittently, please file a bug and provide a link or upload of the combined log.
    2026-11-12T08:24:48.127408Z TestFramework (ERROR): https://github.com/bitcoin/bitcoin/issues
    2026-11-12T08:24:48.127479Z TestFramework (ERROR): 
    
    
    stderr:
    [node 2] Cleaning up leftover process
    [node 1] Cleaning up leftover process
    [node 0] Cleaning up leftover process
    
    
  11. hebasto approved
  12. hebasto commented at 11:37 AM on November 12, 2026: member

    ACK 46ee95db49c9fa7f4b80a1e59816660f5ee0c4f4, I have reviewed the code and it looks OK.

  13. DrahtBot requested review from hebasto on Nov 12, 2026
  14. in test/functional/interface_ipc_mining.py:153 in 46ee95db49
     146 | @@ -147,9 +147,10 @@ async def async_routine():
     147 |              current_block_height = self.nodes[0].getchaintips()[0]["height"]
     148 |              assert_equal(blockref.result.height, current_block_height)
     149 |  
     150 | +            long_timeout = max(self.default_ipc_timeout, 60000.0)  # at least 1 minute
     151 |              self.log.debug("Mine a block")
     152 |              newblockref = (await wait_and_do(
     153 | -                mining.waitTipChanged(ctx, blockref.result.hash, self.default_ipc_timeout),
     154 | +                mining.waitTipChanged(ctx, blockref.result.hash, long_timeout),
    


    ryanofsky commented at 4:14 PM on November 13, 2026:

    In commit "test: fix waitTipChanged timeout on slow hosts in interface_ipc_mining" (9b6dadb39c22bfb6d8b8040519316768919ff906)

    Nit: could the commit message explain what goes wrong? It only has a subject line now, and the PR description's faketime repro makes it look like a wall clock problem, which isn't what this commit fixes. Maybe something like:

    Use a 1 minute timeout for the waitTipChanged call that waits for a newly mined block, instead of default_ipc_timeout, which is only 1 second when the test runs with the default --timeout-factor (CI uses 40).

    The test sends waitTipChanged, waits 0.1 seconds, and then calls generate. Everything between the node receiving the request and the new block becoming its tip (the rest of that sleep, the generate RPC, and creating, validating and connecting the block) had to fit in that 1 second, which a busy machine may not manage. When it didn't, waitTipChanged returned the old tip and the height check failed. The longer timeout costs nothing when the test passes, because the call returns as soon as the tip changes.

    As for why the faketime command reproduces this: it triggers the same failure, but not through the wall clock. The node measures the waitTipChanged deadline with the steady clock, which --exclude-monotonic leaves alone (it tells faketime not to fake CLOCK_MONOTONIC). But libfaketime also stretches epoll_wait timeouts, so the asyncio.sleep(0.1) in wait_and_do takes a full second of real time and uses up the timeout before the block is mined. On master, shrinking that sleep makes "Mine a block" pass under faketime, and raising it to 1 second makes it fail the same way without faketime. So for this commit, faketime is just a way to make the test slow. It also means the repro misses the same race in the waitNext fee-increase wait, which this PR doesn't fix: waitNext measures its deadline with the wall clock, which faketime slows down along with the sleep (see #36791 (review)).


    ViniciusCestarii commented at 6:03 PM on November 13, 2026:

    Thanks for the review and explanation! I can't change the commit description since this was already merged but I updated the PR description to point to your explanation. I will take a look on this other case!


    ryanofsky commented at 8:06 PM on November 13, 2026:

    re: #36791 (review)

    I updated the PR description to point to your explanation

    Thanks, the note in the description works well.

  15. in test/functional/interface_ipc_mining.py:323 in 46ee95db49


    ryanofsky commented at 4:15 PM on November 13, 2026:

    In commit "test: fix waitTipChanged timeout on slow hosts in interface_ipc_mining" (9b6dadb39c22bfb6d8b8040519316768919ff906)

    This PR fixes only one of two places with the same race. This waitNext call waits for an event the test triggers after wait_and_do's 0.1s sleep, just like the "Mine a block" waitTipChanged call the commit fixes, and its timeout is also default_ipc_timeout (1 second with the default --timeout-factor). If the sleep, the RPC and the mempool update together take more than a second, waitNext times out and assert template6 is not None fails. I checked this by passing sleep_time=1.2 to this wait_and_do call, which makes the test fail at that assert.

    The faketime repro only catches the waitTipChanged case because the two methods use different clocks: waitTipChanged measures its deadline with the steady clock, which faketime leaves running at real speed, while waitNext uses the wall clock, which faketime slows down along with the sleep, so the deadline stretches too and the test still passes.

    Could use the long timeout here as well. It might be simplest to set it once in run_test next to default_ipc_timeout:

    # Timeout for waits that should return early because of an event the test
    # triggers, long enough not to expire on a slow host.
    self.long_ipc_timeout = max(self.default_ipc_timeout, 60000.0)  # at least 1 minute
    

    and use it for both waitTipChanged calls, this wait, and the existing max(self.default_ipc_timeout, 60000.0) in wait_for_block(). The template2 wait (2000.0 * timeout_factor with a 1.1 * timeout_factor sleep) has a similar but smaller window and could use it too, since it ignores fees and only returns for the tip update.


    ViniciusCestarii commented at 6:45 PM on November 13, 2026:

    Good catch. I opened #36862 addressing this.


    ryanofsky commented at 8:06 PM on November 13, 2026:

    re: #36791 (review)

    I opened #36862 addressing this.

    Thanks for the quick follow-up! #36862 covers all the waits I mentioned.

    Another possible follow-up, if you're interested: waitNext measures its timeout with the wall clock while waitTipChanged uses the steady clock, and I don't think that's intentional. It's also why the faketime command only caught one of the two races. Only the testnet 20-minute rule needs NodeClock, since it compares node time with the tip's timestamp. The deadline and the 1-second fee tick seem to use it only because they share the same now variable.

    One effect is that waitNext never times out while mocktime is frozen: in a quick unit test, a waitNext call with a 500ms timeout under FakeNodeClock was still waiting after 5 seconds. Using the steady clock for the deadline and tick would fix that and make the two methods behave the same. With this change, that unit test returns after 500ms and the existing mining tests pass:

    -auto now{NodeClock::now()};
    +auto now{std::chrono::steady_clock::now()};
     const auto deadline = now + wait_options.timeout;
     ...
    -        if (now > tip_time + 20min) {
    +        if (NodeClock::now() > tip_time + 20min) {
     ...
    -    now = NodeClock::now();
    +    now = std::chrono::steady_clock::now();
     } while (now < deadline);
    

    ViniciusCestarii commented at 12:08 PM on November 14, 2026:

    Thanks! I did think about switching waitNext to the steady clock, but figured it would be considered churn. Glad you think it's worth doing. I'll open a PR


    ViniciusCestarii commented at 1:05 PM on November 14, 2026:
  16. ryanofsky approved
  17. ryanofsky commented at 4:57 PM on November 13, 2026: contributor

    Code review ACK 46ee95db49c9fa7f4b80a1e59816660f5ee0c4f4. This makes interface_ipc_mining.py more robust than on master: duration checks no longer fail when the system clock steps (like hebasto's NetBSD run, where the measured duration came out negative), and the waitTipChanged call that waits for a mined block no longer has only 1 second (with the default timeout factor) for the test to mine it.

    I also ran the faketime command from the description: master fails at "Mine a block" and this PR passes. This is only a partial fix, though: the waitNext fee-increase wait has the same race and isn't changed, because the faketime repro can't trigger it (https://github.com/bitcoin/bitcoin/pull/36791#discussion_r4565761925). It would be good to fix this case as well.

    Update: #36862 fixes this case. There is also a related follow-up about waitNext's clock: #36791 (review)

  18. ryanofsky assigned ryanofsky on Nov 13, 2026
  19. ryanofsky merged this on Nov 13, 2026
  20. ryanofsky closed this on Nov 13, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-01 02:51 UTC

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