worker/types/worker: set ID before sending message

The previous code set the message ID after sending it, which could result in
the receiver reading the ID before it's set.
This commit is contained in:
Simon Ser 2019-05-19 09:50:17 +00:00 committed by Drew DeVault
parent 7c6325977b
commit 34dd6bc635
1 changed files with 9 additions and 3 deletions

View File

@ -30,11 +30,13 @@ func NewWorker(logger *log.Logger) *Worker {
}
}
func (worker *Worker) setCallback(msg WorkerMessage,
cb func(msg WorkerMessage)) {
func (worker *Worker) setId(msg WorkerMessage) {
msg.setId(nextId)
nextId++
}
func (worker *Worker) setCallback(msg WorkerMessage,
cb func(msg WorkerMessage)) {
if cb != nil {
worker.mutex.Lock()
@ -59,6 +61,8 @@ func (worker *Worker) getCallback(msg WorkerMessage) (func(msg WorkerMessage),
func (worker *Worker) PostAction(msg WorkerMessage,
cb func(msg WorkerMessage)) {
worker.setId(msg)
if resp := msg.InResponseTo(); resp != nil {
worker.Logger.Printf("(ui)=> %T:%T\n", msg, resp)
} else {
@ -72,6 +76,8 @@ func (worker *Worker) PostAction(msg WorkerMessage,
func (worker *Worker) PostMessage(msg WorkerMessage,
cb func(msg WorkerMessage)) {
worker.setId(msg)
if resp := msg.InResponseTo(); resp != nil {
worker.Logger.Printf("->(ui) %T:%T\n", msg, resp)
} else {