1
0
Fork 0
mirror of https://github.com/sourcegraph/jsonrpc2.git synced 2026-06-19 05:30:03 +02:00

Fix underlying connection not being closed on protocol error

Before this commit, the underlying connection of `Conn` was not being
closed when a protocol error was encountered. This behavior contradicted
with `Conn.DisconnectNotify()` because it reported that the underlying
connection was being closed. Additionally, the underlying connection was
now orphaned because `Conn` was no longer processing any of the
subsequent requests.

With this commit, the underlying connection is now being closed when a
protocol error is encountered, matching what `Conn.DisconnectNotify()`
has already been reporting.
This commit is contained in:
Sam Herrmann 2023-02-06 16:42:46 -05:00
parent 78a3d790f3
commit 21db451b57
2 changed files with 49 additions and 33 deletions

View file

@ -390,6 +390,23 @@ func TestConn_Close_waitingForResponse(t *testing.T) {
<-done
}
func TestConn_DisconnectNotify_protocol_error(t *testing.T) {
connA, connB := net.Pipe()
c := jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(connB, jsonrpc2.VarintObjectCodec{}), nil)
connA.Write([]byte("invalid json"))
select {
case <-c.DisconnectNotify():
case <-time.After(200 * time.Millisecond):
t.Fatal("no disconnect notification")
}
// Assert that the underlying connection is closed by trying to write to it.
_, got := connB.Write(nil)
want := io.ErrClosedPipe
if got != want {
t.Fatalf("got %q, want %q", got, want)
}
}
func serve(ctx context.Context, lis net.Listener, h jsonrpc2.Handler, streamMaker streamMaker, opts ...jsonrpc2.ConnOpt) error {
for {
conn, err := lis.Accept()