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

Fix reverse client hang on closed connections #98

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions options_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func WithReverseClient[RP any](namespace string) ServerOption {
}

// todo test that everything is closing correctly
stop := make(chan struct{}) // todo better stop?
cl.exiting = stop
cl.exiting = conn.exiting

requests := cl.setupRequestChan()
conn.requests = requests
Expand Down
59 changes: 59 additions & 0 deletions rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,65 @@ func TestReverseCallAliased(t *testing.T) {
closer()
}

// RevCallDropTestServerHandler attempts to make a client call on a closed connection.
type RevCallDropTestServerHandler struct {
closeConn func()
res chan error
}

func (h *RevCallDropTestServerHandler) Call(ctx context.Context) error {
revClient, ok := ExtractReverseClient[RevCallTestClientProxy](ctx)
if !ok {
return fmt.Errorf("no reverse client")
}

h.closeConn()
time.Sleep(time.Second)

_, err := revClient.CallOnClient(7)
h.res <- err

return nil
}

func TestReverseCallDroppedConn(t *testing.T) {
// setup server

hnd := &RevCallDropTestServerHandler{
res: make(chan error),
}

rpcServer := NewServer(WithReverseClient[RevCallTestClientProxy]("Client"))
rpcServer.Register("Server", hnd)

// httptest stuff
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()

// setup client

var client struct {
Call func() error
}
closer, err := NewMergeClient(context.Background(), "ws://"+testServ.Listener.Addr().String(), "Server", []interface{}{
&client,
}, nil, WithClientHandler("Client", &RevCallTestClientHandler{}))
require.NoError(t, err)

hnd.closeConn = closer

// do the call!
e := client.Call()

require.Error(t, e)
require.Contains(t, e.Error(), "websocket connection closed")

res := <-hnd.res
require.Error(t, res)
require.Contains(t, res.Error(), "RPC client error: sendRequest failed: websocket routine exiting")
time.Sleep(100 * time.Millisecond)
}

type BigCallTestServerHandler struct {
}

Expand Down