Skip to content

Fix Race Condition in TestListenerCustomConnIDs#782

Merged
adrianosela merged 1 commit intopion:masterfrom
adrianosela:fix-test-listener-conn-ids
Jan 21, 2026
Merged

Fix Race Condition in TestListenerCustomConnIDs#782
adrianosela merged 1 commit intopion:masterfrom
adrianosela:fix-test-listener-conn-ids

Conversation

@adrianosela
Copy link
Copy Markdown
Contributor

@adrianosela adrianosela commented Jan 21, 2026

Fix Race Condition in TestListenerCustomConnIDs

Sometimes this test would fail in CI (intermittently):

  === RUN   TestListenerCustomConnIDs
  packet_conn_test.go:513:
  Error Trace:    /home/runner/work/dtls/dtls/internal/net/udp/packet_conn_test.go:513
  /opt/hostedtoolcache/go/1.24.11/x64/src/runtime/asm_amd64.s:1700
  Error:          Should be false
  Test:           TestListenerCustomConnIDs
  Messages:       Multiple messages from single client 127.0.0.1:56395
  --- FAIL: TestListenerCustomConnIDs (0.40s)

This test had a race condition caused by UDP port re-use. When clients rapidly open and close connections, the OS would sometimes assign the same port number to different clients. Since the test used the *net.UDPConn's LocalAddr as the unique identifier for clients, this would result in the same unique identifier being assigned to multiple clients (and thus not being unique!).

To fix this, we use an identifier based off the client index in the test's loop e.g. "client-%d"

How to Reproduce

Run the test 50 times

go test -run TestListenerCustomConnIDs ./internal/net/udp -count=50 -v

Always 1 or 2 (out of 50) failures on my MacBook.

How to Verify Fix

Ran it 1000 times and got no failures.

go test -run TestListenerCustomConnIDs ./internal/net/udp -count=1000 -v

This test had a race condition caused by UDP port re-use.
When clients rapidly open and close connections, the OS
would sometimes assign the same port number to different
clients. Since the test used the *net.UDPConn's LocalAddr
as the unique identifier for clients, this would result
in the same unique identifier being assigned to multiple
clients (and thus not being unique!).

To fix this, we use an identifier based off the client
index in the test's loop e.g. "client-%d"
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.61%. Comparing base (8ea3afc) to head (6a29b15).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #782      +/-   ##
==========================================
- Coverage   81.65%   81.61%   -0.04%     
==========================================
  Files         105      105              
  Lines        5837     5837              
==========================================
- Hits         4766     4764       -2     
- Misses        687      689       +2     
  Partials      384      384              
Flag Coverage Δ
go 81.61% <ø> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adrianosela adrianosela merged commit 6b2fbf1 into pion:master Jan 21, 2026
19 checks passed
@adrianosela adrianosela deleted the fix-test-listener-conn-ids branch January 21, 2026 13:56
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.

2 participants