test: fix race in Tor control reconnect check #36937

Open
ANAMASGARD wants to merge 1 commit from test/fix-torcontrol-reconnect-race into master AGit
ANAMASGARD commented 2026-11-21 10:13:35 +00:00 (Migrated from github.com)

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.

`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.
DrahtBot commented 2026-11-21 10:13:44 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK Waridi318, winterrdog
Concept ACK StephenChi-hi
Approach ACK fjahr

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36170 (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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36937 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36937.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36937#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [Waridi318](https://github.com/bitcoin/bitcoin/pull/36937#issuecomment-7156332167), [winterrdog](https://github.com/bitcoin/bitcoin/pull/36937#pullrequestreview-6100289271) | | Concept ACK | [StephenChi-hi](https://github.com/bitcoin/bitcoin/pull/36937#issuecomment-7076993183) | | Approach ACK | [fjahr](https://github.com/bitcoin/bitcoin/pull/36937#pullrequestreview-6058130126) | 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](https://github.com/bitcoin/bitcoin/pull/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-->
First-time contributor

🔵 Suggestion · The reconnect timing check includes work before the disconnect trigger

expect_disconnect includes pre-trigger work in its 0.5-second check, so a slow test thread could mask a shortened reconnect backoff; this is a coverage improvement, not an observed false pass.

Location: test/functional/feature_torcontrol.py:153 (head).

Review coverage was incomplete.


Reviewed: ba7734b12092 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:f5a67cf4f3d03e7f5934832b7f066ae4aecf3417 --> <!-- ralph-head:ba7734b120923d7bb554f0259e21ad91fa9c500f --> <!-- ralph-case:4fb390fc9d3e6c76d339f144053f15e9e1b9e730bcd1f45d0408a6b6a9bb3271 --> **🔵 Suggestion · The reconnect timing check includes work before the disconnect trigger** `expect_disconnect` includes pre-trigger work in its 0.5-second check, so a slow test thread could mask a shortened reconnect backoff; this is a coverage improvement, not an observed false pass. Location: [`test/functional/feature_torcontrol.py:153`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/ba7734b120923d7bb554f0259e21ad91fa9c500f/test/functional/feature_torcontrol.py#L153>) (head). Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`ba7734b12092`](<https://git.fish.foo/bitcoin/bitcoin/commit/ba7734b120923d7bb554f0259e21ad91fa9c500f>) · [Review report](<https://review.fish.foo/report/36937-ba7734b120923d7bb554f0259e21ad91fa9c500f-2917-1.html>) · [Previous reports](<https://review.fish.foo/traces/36937/>) [permalink](<https://review.fish.foo/report/36937-ba7734b120923d7bb554f0259e21ad91fa9c500f-2917-1.html>)
StephenChi-hi commented 2026-11-22 09:11:43 +00:00 (Migrated from github.com)

cACK

cACK
Waridi318 commented 2026-11-25 07:51:35 +00:00 (Migrated from github.com)

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.

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.
Waridi318 (Migrated from github.com) reviewed 2026-11-25 07:59:09 +00:00
Waridi318 (Migrated from github.com) commented 2026-11-25 07:59:09 +00:00

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

nit: this is the only `from time import` under `test/functional`. The other files use `import time` with `time.monotonic()`
fjahr (Migrated from github.com) reviewed 2026-11-25 13:23:50 +00:00
fjahr (Migrated from github.com) left a comment

Concept ACK

EDIT: and Approach ACK

Concept ACK EDIT: and Approach ACK
fjahr (Migrated from github.com) commented 2026-11-25 13:06:51 +00:00

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

There is no need to remove this comment since the line it refers to is unchanged.
fjahr (Migrated from github.com) commented 2026-11-25 13:04:55 +00:00

Agree that this is a bit nicer but not a blocker

Agree that this is a bit nicer but not a blocker
fjahr (Migrated from github.com) commented 2026-11-25 13:23:39 +00:00

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.

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.
fjahr (Migrated from github.com) commented 2026-11-25 13:12:34 +00:00

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.
I think this comment could be a bit clearer: ```suggestion # 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. ```
fjahr (Migrated from github.com) commented 2026-11-25 13:13:28 +00:00

nit: I would have chosen 0.9s here

nit: I would have chosen 0.9s here
winterrdog (Migrated from github.com) reviewed 2026-11-28 22:08:49 +00:00
winterrdog (Migrated from github.com) left a comment

ACK ba7734b120. 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

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
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36937
No description provided.