Skip to content

test(pubsub): de-flake test_gossip_gate_filters_peers (#1401) - #1402

Merged
acul71 merged 5 commits into
libp2p:mainfrom
yashksaini-coder:test/deflake-gossipsub-score-gates
Aug 10, 2026
Merged

test(pubsub): de-flake test_gossip_gate_filters_peers (#1401)#1402
acul71 merged 5 commits into
libp2p:mainfrom
yashksaini-coder:test/deflake-gossipsub-score-gates

Conversation

@yashksaini-coder

Copy link
Copy Markdown
Contributor

Summary

Closes #1401.

test_gossip_gate_filters_peers fails intermittently on CI (seen on windows (3.12, core); green on all Linux runners and Windows 3.11/3.13):

tests/core/pubsub/test_gossipsub_v1_1_score_gates.py:177
    assert peer1_id in peers_to_send
AssertionError: assert <ID ...> in []

Root cause

The test subscribes all peers, then waits a fixed await trio.sleep(0.2) before calling gsub0._get_peers_to_send(...). That helper skips a topic until it appears in pubsub.peer_topics:

if topic not in self.pubsub.peer_topics:
    continue

peer_topics[topic] is only populated when the node receives a SUBSCRIBE RPC (handle_subscription). On a slow runner the subscription hasn't propagated to gsub0 within 0.2s → topic key absent → continuesend_to empty → peers_to_send == []. A fixed-sleep race, not a logic bug (all mesh/score state in this test is set manually).

Fix

Replace the fixed sleep with the existing event-based helper Pubsub.wait_for_subscription(peer_id, topic) — whose own docstring says "Use this instead of arbitrary trio.sleep() calls to avoid race conditions." Deterministic and non-hanging (5s timeout, returns instantly once the subscription is registered).

-await trio.sleep(0.2)
+await pubsubs[0].wait_for_subscription(hosts[1].get_id(), topic)
+await pubsubs[0].wait_for_subscription(hosts[2].get_id(), topic)

Scope

Intentionally limited to this one test. Sibling tests in the file share the fixed-sleep pattern, but at least one (test_publish_gate_blocks_low_scoring_peers) depends on heartbeat-driven mesh formation, where a subscription-only wait would under-wait and introduce a different flake. A broader de-flake needs per-test analysis and is out of scope here.

Test plan

  • test_gossip_gate_filters_peers run 5× locally → stable pass
  • Full file test_gossipsub_v1_1_score_gates.py → 11 passed, no regression
  • ruff check / ruff format --check clean
  • newsfragments/1401.internal.rst added

yashksaini-coder and others added 5 commits July 22, 2026 13:20
The test subscribed all peers to a topic then waited a fixed
trio.sleep(0.2) before calling _get_peers_to_send. That helper skips a
topic until it appears in pubsub.peer_topics, which is only populated on
receipt of a SUBSCRIBE RPC. On slow CI (seen on windows 3.12) 0.2s was
insufficient, so the topic key was absent, the loop continued, and
peers_to_send came back empty -> 'assert peer1_id in peers_to_send' failed.

Replace the fixed sleep with the event-based Pubsub.wait_for_subscription
helper (whose docstring recommends exactly this). All mesh/score state in
this test is set manually, so subscription propagation is the only timing
dependency; the fix is deterministic and non-hanging (5s timeout).

Scope limited to this test: sibling tests share the pattern but some
depend on heartbeat-driven mesh formation where a subscription-only wait
would under-wait.

Closes libp2p#1401.

@acul71 acul71 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM — approve.

Correct, well-scoped de-flake for #1401: _get_peers_to_send skips topics absent from peer_topics, and the fixed trio.sleep(0.2) after subscribe raced on slow CI. Switching to Pubsub.wait_for_subscription is the right fix (event-based, 5s timeout, early-return if already registered). Newsfragment and issue linkage look good; CI is green including windows (3.12, core).

Optional follow-up (out of scope, not blocking): a remaining await trio.sleep(0.2) after host connect() in this test (and similar fixed sleeps in sibling score-gate tests) could still flake under connect/mesh races later. Worth a separate audit with per-test readiness predicates when someone has bandwidth — not required for this PR.

@acul71
acul71 merged commit b81493e into libp2p:main Aug 10, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flaky: test_gossip_gate_filters_peers races on subscription propagation (fixed trio.sleep)

2 participants