test: fix race in Tor control reconnect check #36937

pull ANAMASGARD wants to merge 1 commits into bitcoin:master from ANAMASGARD:test/fix-torcontrol-reconnect-race changing 1 files +8 −4
  1. ANAMASGARD commented at 10:13 AM on November 21, 2026: none

    feature_torcontrol.py can fail if the node reconnects before the test thread starts checking the backoff. This caused an intermittent CI failure on NetBSD even though the node observed the expected delay.

    Record when the mock Tor server accepts the new connection and compare that timestamp with the time just before triggering the disconnect. This preserves both checks: at least half a second must have elapsed, and the node must send PROTOCOLINFO 1 on reconnect.

    On Linux, the 1.5-second delay from the issue reproduced the original failure. The updated test passed both with and without that delay.

    Fixes #36934.

  2. test: fix race in Tor control reconnect check
    Signed-off-by: Gaurav Chaudhary <chaudharygaurav2004@gmail.com>
    ba7734b120
  3. DrahtBot added the label Tests on Nov 21, 2026
  4. DrahtBot commented at 10:13 AM on November 21, 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 Waridi318, winterrdog
    Concept ACK StephenChi-hi
    Approach ACK fjahr

    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.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36170 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36170.svg"></sub> (net: stop sharing P2P listeners with automatic Tor services by l0rinc)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. StephenChi-hi commented at 9:11 AM on November 22, 2026: none

    cACK

  6. Waridi318 commented at 7:51 AM on November 25, 2026: contributor

    tACK ba7734b

    Reproduced #36934. On master, ensure_for check fails immediately, and on this branch it passes.

    Also checked the new assertion still fails when the backoff is genuinely missing. Set RECONNECT_TIMEOUT_START to 0.0 and rebuilding:

    test_oversized_line: AssertionError: 0.013399518000369426 < 0.5
    test_overmany_lines: AssertionError: 0.013552839998737909 < 0.5 (run on its own)
    

    Clearly, almost all of the 0.5s threshold has to come from the node's actual wait.

    Ran feature_torcontrol.py 5 times on this branch with backoff restored, all passing.

  7. in test/functional/feature_torcontrol.py:9 in ba7734b120
       5 | @@ -6,9 +6,11 @@
       6 |  from contextlib import contextmanager
       7 |  import socket
       8 |  import threading
       9 | +from time import monotonic
    


    Waridi318 commented at 7:59 AM on November 25, 2026:

    nit: this is the only from time import under test/functional. The other files use import time with time.monotonic()


    fjahr commented at 1:04 PM on November 25, 2026:

    Agree that this is a bit nicer but not a blocker

  8. in test/functional/feature_torcontrol.py:155 in ba7734b120
     155 |  
     156 |          if expect:
     157 | -            # No reconnect before the initial reconnect timeout of 1s has passed
     158 | -            ensure_for(duration=0.5, f=lambda: len(mock_tor.received_commands) == initial_len)
     159 | -            # Expect to receive a PROTOCOLINFO 1 on reconnect, bumping the received
     160 | -            # commands length.
    


    fjahr commented at 1:06 PM on November 25, 2026:

    There is no need to remove this comment since the line it refers to is unchanged.

  9. in test/functional/feature_torcontrol.py:160 in ba7734b120
     159 | -            # Expect to receive a PROTOCOLINFO 1 on reconnect, bumping the received
     160 | -            # commands length.
     161 | +            # The reconnect may already have happened while the test was sending data.
     162 |              self.wait_until(lambda: len(mock_tor.received_commands) == initial_len + 1)
     163 |              assert_equal(mock_tor.received_commands[initial_len], "PROTOCOLINFO 1")
     164 | +            # Allow a half-second margin on the initial one-second backoff.
    


    fjahr commented at 1:12 PM on November 25, 2026:

    I think this comment could be a bit clearer:

                # No reconnect before the initial reconnect timeout of 1s has passed.
                # Measured from before the disconnect was triggered, so a slow test
                # can only increase the elapsed time.
    
  10. in test/functional/feature_torcontrol.py:161 in ba7734b120
     160 | -            # commands length.
     161 | +            # The reconnect may already have happened while the test was sending data.
     162 |              self.wait_until(lambda: len(mock_tor.received_commands) == initial_len + 1)
     163 |              assert_equal(mock_tor.received_commands[initial_len], "PROTOCOLINFO 1")
     164 | +            # Allow a half-second margin on the initial one-second backoff.
     165 | +            assert_greater_than_or_equal(mock_tor.last_accept_time - start, 0.5)
    


    fjahr commented at 1:13 PM on November 25, 2026:

    nit: I would have chosen 0.9s here

  11. in test/functional/feature_torcontrol.py:157 in ba7734b120
     156 |          if expect:
     157 | -            # No reconnect before the initial reconnect timeout of 1s has passed
     158 | -            ensure_for(duration=0.5, f=lambda: len(mock_tor.received_commands) == initial_len)
     159 | -            # Expect to receive a PROTOCOLINFO 1 on reconnect, bumping the received
     160 | -            # commands length.
     161 | +            # The reconnect may already have happened while the test was sending data.
    


    fjahr commented at 1:23 PM on November 25, 2026:

    This comment seems to explain why the test was changed from the old version. But reading it after the change has been made is confusing. I would suggest to remove it.

  12. fjahr commented at 1:23 PM on November 25, 2026: contributor

    Concept ACK

    EDIT: and Approach ACK

  13. winterrdog commented at 10:08 PM on November 28, 2026: contributor

    ACK ba7734b120923d7bb554f0259e21ad91fa9c500f. code reviewed and tested

    tested with the reproducer patch (with a 4s sleep): the merge base failed at ensure_for() as expected, while PR HEAD passed and correctly verified the reconnect behaviour and that at least 500ms had elapsed before the mock Tor server accepted the new connection

    all the above was tested with g++-12.5.0 on NetBSD 11.0, targeting x86_64


    once the outstanding suggestions are accurately addressed, i believe this will be ok to merge because the patchset's core approach already asserts both the reconnection behaviour and the elapsed-time threshold correctly

  14. DrahtBot requested review from fjahr on Nov 28, 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-11-29 05:51 UTC

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