Skip to content

Commit

Permalink
Merge pull request #98 from filecoin-project/fix/rev-client-close
Browse files Browse the repository at this point in the history
Fix reverse client hang on closed connections
  • Loading branch information
magik6k authored Mar 15, 2023
2 parents 6fff219 + 6be5fef commit 1fbbed8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
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

0 comments on commit 1fbbed8

Please sign in to comment.