forked from tulir/whatsmeow
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Fix data race on the socket disconnect handler #46
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
Open
jlucaso1
wants to merge
4
commits into
main
Choose a base branch
from
fix/socket-disconnect-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2ebbc2
Merge pull request #42 from polymorfa/main
purpshell 7668f7a
socket: synchronize disconnect handler updates
jlucaso1 a682fb7
ci: pin goimports to x/tools v0.49.0 for go 1.25
jlucaso1 9c9a065
test: acknowledge CloseNow error in socket test server
jlucaso1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package socket | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/coder/websocket" | ||
|
|
||
| waLog "github.com/polymorfa/hypermeow/util/log" | ||
| ) | ||
|
|
||
| func newTestFrameSocket(t *testing.T) *FrameSocket { | ||
| t.Helper() | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{InsecureSkipVerify: true}) | ||
| if err != nil { | ||
| t.Errorf("Failed to accept websocket: %v", err) | ||
| return | ||
| } | ||
| defer func() { _ = conn.CloseNow() }() | ||
| <-conn.CloseRead(r.Context()).Done() | ||
| })) | ||
| t.Cleanup(server.Close) | ||
| fs := NewFrameSocket(waLog.Noop, server.Client()) | ||
| fs.URL = server.URL | ||
| if err := fs.Connect(t.Context()); err != nil { | ||
| t.Fatalf("Failed to connect websocket: %v", err) | ||
| } | ||
| t.Cleanup(func() { fs.Close(0) }) | ||
| return fs | ||
| } | ||
|
|
||
| func TestNoiseSocketStopConcurrentClose(t *testing.T) { | ||
| for range 100 { | ||
| fs := newTestFrameSocket(t) | ||
| ns, err := newNoiseSocket(t.Context(), fs, nil, nil, nil, func(context.Context, *NoiseSocket, bool) {}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var wg sync.WaitGroup | ||
| start := make(chan struct{}) | ||
| wg.Go(func() { | ||
| <-start | ||
| fs.Close(0) | ||
| }) | ||
| wg.Go(func() { | ||
| <-start | ||
| ns.Stop(true, false) | ||
| }) | ||
| close(start) | ||
| wg.Wait() | ||
| } | ||
| } | ||
|
|
||
| func TestNewNoiseSocketConcurrentClose(t *testing.T) { | ||
| for range 100 { | ||
| fs := newTestFrameSocket(t) | ||
| var wg sync.WaitGroup | ||
| start := make(chan struct{}) | ||
| wg.Go(func() { | ||
| <-start | ||
| fs.Close(0) | ||
| }) | ||
| close(start) | ||
| ns, err := newNoiseSocket(t.Context(), fs, nil, nil, nil, func(context.Context, *NoiseSocket, bool) {}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| wg.Wait() | ||
| ns.Stop(false, false) | ||
| } | ||
| } | ||
|
|
||
| func TestNoiseSocketStop(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| disconnect bool | ||
| allowOnDisconnect bool | ||
| }{ | ||
| {name: "local suppressed", disconnect: true}, | ||
| {name: "local allowed", disconnect: true, allowOnDisconnect: true}, | ||
| {name: "remote suppressed"}, | ||
| {name: "remote allowed", allowOnDisconnect: true}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| fs := newTestFrameSocket(t) | ||
| called := make(chan bool, 2) | ||
| ns, err := newNoiseSocket(t.Context(), fs, nil, nil, nil, func(ctx context.Context, socket *NoiseSocket, remote bool) { | ||
| if ctx != t.Context() { | ||
| t.Error("Unexpected disconnect context") | ||
| } | ||
| socket.Stop(false, false) | ||
| fs.Close(0) | ||
| called <- remote | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| ns.Stop(tc.disconnect, tc.allowOnDisconnect) | ||
| if fs.IsConnected() == tc.disconnect { | ||
| t.Fatalf("Unexpected connection state after Stop: %t", fs.IsConnected()) | ||
| } | ||
| select { | ||
| case <-ns.stopConsumer: | ||
| default: | ||
| t.Fatal("Frame consumer was not stopped") | ||
| } | ||
| fs.lock.Lock() | ||
| hasHandler := fs.OnDisconnect != nil | ||
| fs.lock.Unlock() | ||
| if hasHandler != tc.allowOnDisconnect { | ||
| t.Fatalf("Unexpected disconnect handler after Stop: %t", hasHandler) | ||
| } | ||
| fs.Close(0) | ||
| fs.Close(0) | ||
| if tc.allowOnDisconnect { | ||
| select { | ||
| case remote := <-called: | ||
| if remote == tc.disconnect { | ||
| t.Errorf("Unexpected remote flag: %t", remote) | ||
| } | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("Disconnect handler did not return") | ||
| } | ||
| } | ||
| select { | ||
| case <-called: | ||
| t.Fatal("Unexpected disconnect callback") | ||
| default: | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNoiseSocketRemoteDisconnect(t *testing.T) { | ||
| fs := newTestFrameSocket(t) | ||
| called := make(chan bool, 1) | ||
| ns, err := newNoiseSocket(t.Context(), fs, nil, nil, nil, func(ctx context.Context, socket *NoiseSocket, remote bool) { | ||
| socket.Stop(false, false) | ||
| fs.Close(0) | ||
| called <- remote | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| fs.Close(0) | ||
| select { | ||
| case remote := <-called: | ||
| if !remote { | ||
| t.Error("Expected remote disconnect") | ||
| } | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("Disconnect handler did not return") | ||
| } | ||
| if !ns.destroyed.Load() { | ||
| t.Fatal("Disconnect handler did not stop the noise socket") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.