good morning!!!!

Skip to content
Snippets Groups Projects
Commit 5347bf6b authored by Garet Halliday's avatar Garet Halliday
Browse files

don't hang in idreply if conn closes

parent 548b2022
No related branches found
Tags v0.2.23
1 merge request!37return error to all clients waiting on idreply when connection closes
Pipeline #33423 canceled with stage
in 4 minutes and 39 seconds
......@@ -66,7 +66,9 @@ func (c *Client) Mount(h jsonrpc.Middleware) {
func (c *Client) listen() error {
var msg json.RawMessage
defer c.cn()
defer func() {
_ = c.Close()
}()
dec := json.NewDecoder(bufio.NewReader(c.rd))
for {
err := dec.Decode(&msg)
......@@ -157,7 +159,7 @@ func (c *Client) SetHeader(key string, value string) {
func (c *Client) Close() error {
c.cn()
return nil
return c.p.Close()
}
func (c *Client) writeContext(ctx context.Context, xs []byte) error {
......
......@@ -3,6 +3,7 @@ package clientutil
import (
"context"
"io"
"net"
"sync"
"sync/atomic"
......@@ -12,6 +13,8 @@ import (
type IdReply struct {
id atomic.Int64
closed chan struct{}
chs map[string]chan msgOrError
mu sync.Mutex
}
......@@ -23,7 +26,8 @@ type msgOrError struct {
func NewIdReply() *IdReply {
return &IdReply{
chs: make(map[string]chan msgOrError, 1),
closed: make(chan struct{}),
chs: make(map[string]chan msgOrError, 1),
}
}
......@@ -94,5 +98,16 @@ func (i *IdReply) Ask(ctx context.Context, id []byte) (io.ReadCloser, error) {
case <-ctx.Done():
i.remove(id)
return nil, ctx.Err()
case <-i.closed:
return nil, net.ErrClosed
}
}
func (i *IdReply) Closed() <-chan struct{} {
return i.closed
}
func (i *IdReply) Close() error {
close(i.closed)
return nil
}
......@@ -3,7 +3,9 @@ package jrpctest
import (
"context"
"embed"
"errors"
"math/rand"
"net"
"reflect"
"sync"
"testing"
......@@ -188,6 +190,16 @@ func RunBasicTestSuite(t *testing.T, args BasicTestSuiteArgs) {
wg.Wait()
})
makeTest("close", func(t *testing.T, server *server.Server, client jsonrpc.Conn) {
go func() {
_ = client.Close()
}()
err := jsonrpc.CallInto(context.Background(), client, nil, "test_block")
if !errors.Is(err, net.ErrClosed) {
t.Errorf("expected close error but got %v", err)
}
})
makeTest("", func(t *testing.T, server *server.Server, client jsonrpc.Conn) {
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment