Skip to content

Commit

Permalink
Fix jsonrpc result no pending[id]
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Sep 17, 2019
1 parent b8c96b9 commit 6cb3b4f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ func (c *Conn) Run(runCtx context.Context) error {
Error: msg.Error,
ID: msg.ID,
}
rchan <- response
close(rchan)
if rchan != nil {
rchan <- response
close(rchan)
}
default:
for _, h := range c.handlers {
h.Error(runCtx, fmt.Errorf("message not a call, notify or response, ignoring"))
Expand Down
26 changes: 26 additions & 0 deletions jsonrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ func TestHeaderCall(t *testing.T) {
}
}

func TestResultsNoPendingID(t *testing.T) {
ctx := context.Background()
result := `{"jsonrpc":"2.0","result":["test"],"id":1}`

aR, bW := io.Pipe()
bR, aW := io.Pipe()

stream := jsonrpc2.NewStream(aR, aW)
run(ctx, t, false, bR, bW)

ch := make(chan int)

go func() {
stream.Write(ctx, []byte(result))
stream.Write(ctx, []byte(result))

ch <- 0
}()

select {
case <-ch:
case <-time.After(time.Second * 1):
t.Fatalf("NoPendingID Blocking")
}
}

func prepare(ctx context.Context, t *testing.T, withHeaders bool) (*jsonrpc2.Conn, *jsonrpc2.Conn) {
aR, bW := io.Pipe()
bR, aW := io.Pipe()
Expand Down

0 comments on commit 6cb3b4f

Please sign in to comment.