Fix data race in queuepacketconn_test.go

Use mutex when checking the length of a TranscriptPacketConn.
This commit is contained in:
Cecylia Bocovich 2025-08-26 13:15:36 -04:00
parent 24c42dff13
commit b9e7865c50
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90

View file

@ -150,6 +150,13 @@ func (c *TranscriptPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) {
return c.PacketConn.WriteTo(p, addr)
}
func (c *TranscriptPacketConn) Length() int {
c.lock.Lock()
defer c.lock.Unlock()
return len(c.Transcript)
}
// Tests that QueuePacketConn.WriteTo is compatible with the way kcp-go uses
// PacketConn, allocating source buffers in a sync.Pool.
//
@ -216,7 +223,7 @@ func TestQueuePacketConnWriteToKCP(t *testing.T) {
// A sleep after the Write makes buffer reuse more likely.
time.Sleep(100 * time.Millisecond)
if len(transcript.Transcript) == 0 {
if transcript.Length() == 0 {
panic("empty transcript")
}