Skip to content

Commit 02831c0

Browse files
jaystenmarkSean-Der
authored andcommitted
Fix data race in WriteTo
There is no harm in shadowing `err` in the goroutine since we do not need it outside of the routine. I also discovered data races in tcp_conn_test.go while fixing this bug and have included them in this PR.
1 parent 98ed93b commit 02831c0

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

internal/client/tcp_conn_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ func TestTCPConn(t *testing.T) {
9292
client = &mockClient{
9393
performTransaction: func(msg *stun.Message, _ net.Addr, _ bool) (TransactionResult, error) {
9494
if msg.Type.Class == stun.ClassRequest && msg.Type.Method == stun.MethodConnect {
95-
msg, err = stun.Build(
95+
msg, buildErr := stun.Build(
9696
stun.TransactionID,
9797
stun.NewType(stun.MethodConnect, stun.ClassErrorResponse),
9898
stun.ErrorCodeAttribute{Code: stun.CodeBadRequest},
9999
)
100-
assert.NoError(t, err)
100+
assert.NoError(t, buildErr)
101101

102102
return TransactionResult{Msg: msg}, nil
103103
}
@@ -174,12 +174,12 @@ func TestTCPConn(t *testing.T) {
174174
typ = stun.NewType(stun.MethodCreatePermission, stun.ClassSuccessResponse)
175175
}
176176

177-
msg, err = stun.Build(
177+
msg, buildErr := stun.Build(
178178
stun.TransactionID,
179179
typ,
180180
cid,
181181
)
182-
assert.NoError(t, err)
182+
assert.NoError(t, buildErr)
183183

184184
return TransactionResult{Msg: msg}, nil
185185
},

internal/client/udp_conn.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ func (c *UDPConn) WriteTo(payload []byte, addr net.Addr) (int, error) { //nolint
240240
if bound.state() == bindingStateReady && time.Since(bound.refreshedAt()) > 5*time.Minute {
241241
bound.setState(bindingStateRefresh)
242242
go func() {
243-
err = c.bind(bound)
244-
if err != nil {
245-
c.log.Warnf("Failed to bind() for refresh: %s", err)
243+
if bindErr := c.bind(bound); bindErr != nil {
244+
c.log.Warnf("Failed to bind() for refresh: %s", bindErr)
246245
bound.setState(bindingStateFailed)
247246
// Keep going...
248247
} else {

lt_cred.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package turn
55

6-
import ( //nolint:gci
6+
import (
77
"crypto/hmac"
88
"crypto/sha1" //nolint:gosec,gci
99
"encoding/base64"

0 commit comments

Comments
 (0)