rpc: fix issue with multiple subscriptions (#4406)

Using the WebSocket server, when the same client calls multiple time the subscribe method, only the last subscription receives all the events of the previous ones.

example:

    subscription1 = tm.event = 'NewBlock'
    subscription2 = tm.event = 'Tx'

In this case, subscription2 will receive the new blocks but subscription1 will not.

This came from the WebSocket handler that had the declaration of the rpcrequest moved and so overridden for every request and given in the JSONReq client context (so the id of the subscription was not the right one).

This fixes the issue by simply declaring the rpcrequest inside the loop so every request will create a new object without overwriting the previous one.
This commit is contained in:
Anthony
2020-02-17 14:00:56 +07:00
committed by GitHub
parent da8813071b
commit 5ea1ff94d1
2 changed files with 4 additions and 3 deletions

View File

@@ -20,3 +20,5 @@ program](https://hackerone.com/tendermint).
### IMPROVEMENTS:
### BUG FIXES:
- [rpc] [\#4406](https://github.com/tendermint/tendermint/pull/4406) Fix issue with multiple subscriptions on the websocket (@antho1404)

View File

@@ -299,8 +299,6 @@ func (wsc *wsConnection) Context() context.Context {
// Read from the socket and subscribe to or unsubscribe from events
func (wsc *wsConnection) readRoutine() {
var request types.RPCRequest
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
@@ -308,7 +306,7 @@ func (wsc *wsConnection) readRoutine() {
err = fmt.Errorf("WSJSONRPC: %v", r)
}
wsc.Logger.Error("Panic in WSJSONRPC handler", "err", err, "stack", string(debug.Stack()))
wsc.WriteRPCResponse(types.RPCInternalError(request.ID, err))
wsc.WriteRPCResponse(types.RPCInternalError(types.JSONRPCIntID(-1), err))
go wsc.readRoutine()
}
}()
@@ -339,6 +337,7 @@ func (wsc *wsConnection) readRoutine() {
return
}
var request types.RPCRequest
err = json.Unmarshal(in, &request)
if err != nil {
wsc.WriteRPCResponse(types.RPCParseError(errors.Wrap(err, "error unmarshaling request")))