worker/types: fix Worker.Callbacks race condition
Worker.Process* functions were called in different goroutines than Worker.Post*. Protect the map with a mutex. Also make the map unexported to prevent external unprotected accesses. Worker.Process* functions used to delete items from the map. However they didn't delete the element they retrieved: callbacks[msg.InResponseTo()] was read while callbacks[msg] was deleted. I'm not sure I understand why. I tried to delete the element that was accessed - but this broke everything (UI froze at "Connecting..."). I don't believe any elements were actually removed from the map, so the new code just doesn't remove anything.
This commit is contained in:
parent
2159eb876e
commit
9ef2a57b51
2 changed files with 42 additions and 21 deletions
|
@ -2,6 +2,7 @@ package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Backend interface {
|
type Backend interface {
|
||||||
|
@ -9,11 +10,42 @@ type Backend interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Worker struct {
|
type Worker struct {
|
||||||
Actions chan WorkerMessage
|
Backend Backend
|
||||||
Backend Backend
|
Actions chan WorkerMessage
|
||||||
Callbacks map[WorkerMessage]func(msg WorkerMessage)
|
Messages chan WorkerMessage
|
||||||
Messages chan WorkerMessage
|
Logger *log.Logger
|
||||||
Logger *log.Logger
|
|
||||||
|
callbacks map[WorkerMessage]func(msg WorkerMessage) // protected by mutex
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWorker(logger *log.Logger) *Worker {
|
||||||
|
return &Worker{
|
||||||
|
Actions: make(chan WorkerMessage, 50),
|
||||||
|
Messages: make(chan WorkerMessage, 50),
|
||||||
|
Logger: logger,
|
||||||
|
callbacks: make(map[WorkerMessage]func(msg WorkerMessage)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (worker *Worker) setCallback(msg WorkerMessage,
|
||||||
|
cb func(msg WorkerMessage)) {
|
||||||
|
|
||||||
|
if cb != nil {
|
||||||
|
worker.mutex.Lock()
|
||||||
|
worker.callbacks[msg] = cb
|
||||||
|
worker.mutex.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (worker *Worker) getCallback(msg WorkerMessage) (func(msg WorkerMessage),
|
||||||
|
bool) {
|
||||||
|
|
||||||
|
worker.mutex.Lock()
|
||||||
|
cb, ok := worker.callbacks[msg]
|
||||||
|
worker.mutex.Unlock()
|
||||||
|
|
||||||
|
return cb, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (worker *Worker) PostAction(msg WorkerMessage,
|
func (worker *Worker) PostAction(msg WorkerMessage,
|
||||||
|
@ -26,9 +58,7 @@ func (worker *Worker) PostAction(msg WorkerMessage,
|
||||||
}
|
}
|
||||||
worker.Actions <- msg
|
worker.Actions <- msg
|
||||||
|
|
||||||
if cb != nil {
|
worker.setCallback(msg, cb)
|
||||||
worker.Callbacks[msg] = cb
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (worker *Worker) PostMessage(msg WorkerMessage,
|
func (worker *Worker) PostMessage(msg WorkerMessage,
|
||||||
|
@ -41,9 +71,7 @@ func (worker *Worker) PostMessage(msg WorkerMessage,
|
||||||
}
|
}
|
||||||
worker.Messages <- msg
|
worker.Messages <- msg
|
||||||
|
|
||||||
if cb != nil {
|
worker.setCallback(msg, cb)
|
||||||
worker.Callbacks[msg] = cb
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
|
func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
|
||||||
|
@ -52,9 +80,8 @@ func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
|
||||||
} else {
|
} else {
|
||||||
worker.Logger.Printf("(ui)<= %T\n", msg)
|
worker.Logger.Printf("(ui)<= %T\n", msg)
|
||||||
}
|
}
|
||||||
if cb, ok := worker.Callbacks[msg.InResponseTo()]; ok {
|
if cb, ok := worker.getCallback(msg.InResponseTo()); ok {
|
||||||
cb(msg)
|
cb(msg)
|
||||||
delete(worker.Callbacks, msg)
|
|
||||||
}
|
}
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
@ -65,9 +92,8 @@ func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage {
|
||||||
} else {
|
} else {
|
||||||
worker.Logger.Printf("<-(ui) %T\n", msg)
|
worker.Logger.Printf("<-(ui) %T\n", msg)
|
||||||
}
|
}
|
||||||
if cb, ok := worker.Callbacks[msg.InResponseTo()]; ok {
|
if cb, ok := worker.getCallback(msg.InResponseTo()); ok {
|
||||||
cb(msg)
|
cb(msg)
|
||||||
delete(worker.Callbacks, msg)
|
|
||||||
}
|
}
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,12 +15,7 @@ func NewWorker(source string, logger *log.Logger) (*types.Worker, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
worker := &types.Worker{
|
worker := types.NewWorker(logger)
|
||||||
Actions: make(chan types.WorkerMessage, 50),
|
|
||||||
Callbacks: make(map[types.WorkerMessage]func(msg types.WorkerMessage)),
|
|
||||||
Messages: make(chan types.WorkerMessage, 50),
|
|
||||||
Logger: logger,
|
|
||||||
}
|
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "imap":
|
case "imap":
|
||||||
fallthrough
|
fallthrough
|
||||||
|
|
Loading…
Reference in a new issue