Skip to content

Commit

Permalink
protect map write with mutex
Browse files Browse the repository at this point in the history
* in order to make the r.receivedResponsesById map goroutine-safe, a
  mutex was added. This is not particular nice, but rewriting the
  jsonrpc connection handling would be quite a lot of work and has a
  big regression potential. So, if it is not absolutely necessary, I'd
  like to avoid that.

see #555
  • Loading branch information
bbernhard committed Jul 9, 2024
1 parent cd996e1 commit dc1efc1
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/client/jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type JsonRpc2Client struct {
signalCliApiConfig *utils.SignalCliApiConfig
number string
receivedMessagesMutex sync.Mutex
receivedResponsesMutex sync.Mutex
}

func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
Expand Down Expand Up @@ -137,11 +138,16 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{
}

responseChan := make(chan JsonRpc2MessageResponse)
r.receivedResponsesMutex.Lock()
r.receivedResponsesById[u.String()] = responseChan
r.receivedResponsesMutex.Unlock()

var resp JsonRpc2MessageResponse
resp = <-responseChan

r.receivedResponsesMutex.Lock()
delete(r.receivedResponsesById, u.String())
r.receivedResponsesMutex.Unlock()

log.Debug("json-rpc command response message: ", string(resp.Result))
log.Debug("json-rpc response error: ", string(resp.Err.Message))
Expand Down

0 comments on commit dc1efc1

Please sign in to comment.