test(pubsub): de-flake test_gossip_gate_filters_peers (#1401) - #1402
Conversation
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
left a comment
There was a problem hiding this comment.
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.
Summary
Closes #1401.
test_gossip_gate_filters_peersfails intermittently on CI (seen onwindows (3.12, core); green on all Linux runners and Windows 3.11/3.13):Root cause
The test subscribes all peers, then waits a fixed
await trio.sleep(0.2)before callinggsub0._get_peers_to_send(...). That helper skips a topic until it appears inpubsub.peer_topics:peer_topics[topic]is only populated when the node receives a SUBSCRIBE RPC (handle_subscription). On a slow runner the subscription hasn't propagated togsub0within 0.2s → topic key absent →continue→send_toempty →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).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_peersrun 5× locally → stable passtest_gossipsub_v1_1_score_gates.py→ 11 passed, no regressionruff check/ruff format --checkcleannewsfragments/1401.internal.rstadded