protect map write with mutex

* 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
This commit is contained in:
Bernhard B
2024-07-09 21:24:24 +02:00
parent cd996e1814
commit dc1efc1a14

View File

@@ -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 {
@@ -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))