Skip to content

test: fix race in Tor control reconnect check - #36937

Open
ANAMASGARD wants to merge 1 commit into
bitcoin:masterfrom
ANAMASGARD:test/fix-torcontrol-reconnect-race
Open

ANAMASGARD wants to merge 1 commit into
bitcoin:masterfrom
ANAMASGARD:test/fix-torcontrol-reconnect-race

Conversation

@ANAMASGARD

Copy link
Copy Markdown

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 DrahtBot added the Tests label Nov 21, 2026
@DrahtBot

DrahtBot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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.

@StephenChi-hi

Copy link
Copy Markdown

cACK

@Waridi318

Waridi318 commented Nov 25, 2026 •

Copy link
Copy Markdown
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.

from contextlib import contextmanager
import socket
import threading
from time import monotonic

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that this is a bit nicer but not a blocker

@fjahr fjahr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

EDIT: and Approach ACK

from contextlib import contextmanager
import socket
import threading
from time import monotonic

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that this is a bit nicer but not a blocker

Comment on lines -154 to -155
# Expect to receive a PROTOCOLINFO 1 on reconnect, bumping the received
# commands length.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

# The reconnect may already have happened while the test was sending data.
self.wait_until(lambda: len(mock_tor.received_commands) == initial_len + 1)
assert_equal(mock_tor.received_commands[initial_len], "PROTOCOLINFO 1")
# Allow a half-second margin on the initial one-second backoff.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment could be a bit clearer:

Suggested change
# Allow a half-second margin on the initial one-second backoff.
# 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.

self.wait_until(lambda: len(mock_tor.received_commands) == initial_len + 1)
assert_equal(mock_tor.received_commands[initial_len], "PROTOCOLINFO 1")
# Allow a half-second margin on the initial one-second backoff.
assert_greater_than_or_equal(mock_tor.last_accept_time - start, 0.5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would have chosen 0.9s here

ensure_for(duration=0.5, f=lambda: len(mock_tor.received_commands) == initial_len)
# Expect to receive a PROTOCOLINFO 1 on reconnect, bumping the received
# commands length.
# The reconnect may already have happened while the test was sending data.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@winterrdog winterrdog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@DrahtBot
DrahtBot requested a review from fjahr November 28, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

qa: Intermittent failure in feature_torcontrol.py

6 participants