Skip to content

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

Open
yashksaini-coder wants to merge 2 commits into
libp2p:mainfrom
yashksaini-coder:test/deflake-gossipsub-score-gates
Open

test(pubsub): de-flake test_gossip_gate_filters_peers (#1401)#1402
yashksaini-coder wants to merge 2 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

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

1 participant