Skip to content
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

Callback from a notification handler can deadlock #78

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions jrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,50 @@ func TestServer_Callback(t *testing.T) {
}
}

func TestServer_CallbackFromNotification(t *testing.T) {
defer leaktest.Check(t)()

pCtx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFunc()

loc := server.NewLocal(handler.Map{
"NotifyMe": handler.New(func(ctx context.Context) error {
if _, err := jrpc2.ServerFromContext(ctx).Callback(pCtx, "succeed", nil); err != nil {
t.Errorf("Callback failed: %v", err)
}

t.Log("succeed request sent")
return nil
}),
}, &server.LocalOptions{
Server: &jrpc2.ServerOptions{AllowPush: true},
Client: &jrpc2.ClientOptions{
OnCallback: func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
t.Logf("OnCallback invoked for method %q", req.Method())
switch req.Method() {
case "succeed":
return true, nil
}
panic("broken test: you should not see this")
},
},
})
defer loc.Close()
ctx := context.Background()

// Call the notification method that posts a callback.
if err := loc.Client.Notify(ctx, "NotifyMe", nil); err != nil {
t.Fatalf("Notify NotifyMe: unexpected error: %v", err)
}
t.Log("first notification sent")
if err := loc.Client.Notify(ctx, "NotifyMe", nil); err != nil {
t.Fatalf("Notify NotifyMe: unexpected error: %v", err)
}
t.Log("second notification sent")

<-pCtx.Done()
}

// Verify that a server push after the client closes does not trigger a panic.
func TestServer_pushAfterClose(t *testing.T) {
defer leaktest.Check(t)()
Expand Down