-
-
Notifications
You must be signed in to change notification settings - Fork 119
Draft: Fix race condition during CSM message exchange #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
41c230f
8111d36
cf9d485
c58872d
b64cd82
d24f3de
158fa8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||
"github.com/plgd-dev/go-coap/v3/options/config" | ||||||||||||||||||||||||||||||||||||||||||||||
"github.com/plgd-dev/go-coap/v3/pkg/runner/periodic" | ||||||||||||||||||||||||||||||||||||||||||||||
"github.com/plgd-dev/go-coap/v3/tcp/client" | ||||||||||||||||||||||||||||||||||||||||||||||
"github.com/plgd-dev/go-coap/v3/tcp/server" | ||||||||||||||||||||||||||||||||||||||||||||||
"github.com/stretchr/testify/assert" | ||||||||||||||||||||||||||||||||||||||||||||||
"github.com/stretchr/testify/require" | ||||||||||||||||||||||||||||||||||||||||||||||
"go.uber.org/atomic" | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -839,3 +840,77 @@ func TestConnRequestMonitorDropRequest(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||
require.Error(t, err) | ||||||||||||||||||||||||||||||||||||||||||||||
require.ErrorIs(t, err, context.DeadlineExceeded) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
func TestConnWithCSMExchangeTimeout(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||
type args struct { | ||||||||||||||||||||||||||||||||||||||||||||||
clientOptions []Option | ||||||||||||||||||||||||||||||||||||||||||||||
serverOptions []server.Option | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||||||||||||||||||
args args | ||||||||||||||||||||||||||||||||||||||||||||||
wantErr bool | ||||||||||||||||||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
name: "client-server-no-csm", | ||||||||||||||||||||||||||||||||||||||||||||||
args: args{ | ||||||||||||||||||||||||||||||||||||||||||||||
clientOptions: []Option{}, | ||||||||||||||||||||||||||||||||||||||||||||||
serverOptions: []server.Option{}, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
wantErr: false, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
name: "client-server-csm-success", | ||||||||||||||||||||||||||||||||||||||||||||||
args: args{ | ||||||||||||||||||||||||||||||||||||||||||||||
clientOptions: []Option{ | ||||||||||||||||||||||||||||||||||||||||||||||
options.WithCSMExchangeTimeout(time.Second * 3), | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
wantErr: false, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
name: "client-server-csm-timeout", | ||||||||||||||||||||||||||||||||||||||||||||||
args: args{ | ||||||||||||||||||||||||||||||||||||||||||||||
clientOptions: []Option{ | ||||||||||||||||||||||||||||||||||||||||||||||
options.WithCSMExchangeTimeout(time.Second * 3), | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
serverOptions: []server.Option{ | ||||||||||||||||||||||||||||||||||||||||||||||
options.WithDisableTCPSignalMessageCSM(), | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
wantErr: true, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
for _, tt := range tests { | ||||||||||||||||||||||||||||||||||||||||||||||
t.Run(tt.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||
l, err := coapNet.NewTCPListener("tcp", "") | ||||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||||
defer func() { | ||||||||||||||||||||||||||||||||||||||||||||||
errC := l.Close() | ||||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, errC) | ||||||||||||||||||||||||||||||||||||||||||||||
}() | ||||||||||||||||||||||||||||||||||||||||||||||
var wg sync.WaitGroup | ||||||||||||||||||||||||||||||||||||||||||||||
defer wg.Wait() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
s := NewServer(tt.args.serverOptions...) | ||||||||||||||||||||||||||||||||||||||||||||||
defer s.Stop() | ||||||||||||||||||||||||||||||||||||||||||||||
wg.Add(1) | ||||||||||||||||||||||||||||||||||||||||||||||
go func() { | ||||||||||||||||||||||||||||||||||||||||||||||
defer wg.Done() | ||||||||||||||||||||||||||||||||||||||||||||||
errS := s.Serve(l) | ||||||||||||||||||||||||||||||||||||||||||||||
assert.NoError(t, errS) | ||||||||||||||||||||||||||||||||||||||||||||||
}() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
client, err := Dial(l.Addr().String(), | ||||||||||||||||||||||||||||||||||||||||||||||
tt.args.clientOptions...) | ||||||||||||||||||||||||||||||||||||||||||||||
if tt.wantErr { | ||||||||||||||||||||||||||||||||||||||||||||||
require.Nil(t, client) | ||||||||||||||||||||||||||||||||||||||||||||||
require.Error(t, err) | ||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||
require.NotNil(t, client) | ||||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+905
to
+914
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent resource leaks and avoid package shadowing
- client, err := Dial(l.Addr().String(),
- tt.args.clientOptions...)
- if tt.wantErr {
- require.Nil(t, client)
- require.Error(t, err)
- } else {
- require.NotNil(t, client)
- require.NoError(t, err)
- }
+ cc, err := Dial(l.Addr().String(), tt.args.clientOptions...)
+ if tt.wantErr {
+ require.Nil(t, cc)
+ require.Error(t, err)
+ return
+ }
+ require.NoError(t, err)
+ require.NotNil(t, cc)
+ defer func() {
+ _ = cc.Close()
+ <-cc.Done()
+ }() 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix potential panic on close, ensure handler cleanup on all paths, and avoid timer leaks
Issues:
Proposed fix:
And add the import:
import ( "crypto/tls" "fmt" "net" "time" + "sync"
📝 Committable suggestion