imap: prevent deadlock from posting actions to self

The IMAP worker has a few methods that post a new Action to itself. This
can create a deadlock when the worker.Actions channel is full: The
worker can't accept a new Action because it's trying to post an action.
This is most noticeable when cached headers are enabled and the message
list is scrolled fast.

Use a goroutine to post actions to the worker when posting from within
the worker.

Fixes: https://todo.sr.ht/~rjarry/aerc/45
Fixes: 7aa71d334b ("imap: add option to cache headers")
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Tim Culverhouse 2022-09-16 14:41:05 -05:00 committed by Robin Jarry
parent e808b96d63
commit f4d6ade429
3 changed files with 10 additions and 3 deletions

View file

@ -126,7 +126,8 @@ func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 {
}, nil)
}
if len(found) > 0 {
w.worker.PostAction(&types.FetchMessageFlags{
// Post in a separate goroutine to prevent deadlocking
go w.worker.PostAction(&types.FetchMessageFlags{
Uids: found,
}, nil)
}

View file

@ -48,7 +48,8 @@ func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) {
emitErr(err)
return
}
imapw.worker.PostAction(&types.FetchMessageHeaders{
// Post in a separate goroutine to prevent deadlocking
go imapw.worker.PostAction(&types.FetchMessageHeaders{
Uids: msg.Uids,
}, func(_msg types.WorkerMessage) {
switch m := _msg.(type) {
@ -79,7 +80,8 @@ func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) {
emitErr(err)
return
}
imapw.worker.PostAction(&types.FetchMessageHeaders{
// Post in a separate goroutine to prevent deadlocking
go imapw.worker.PostAction(&types.FetchMessageHeaders{
Uids: msg.Uids,
}, func(_msg types.WorkerMessage) {
switch m := _msg.(type) {

View file

@ -35,6 +35,8 @@ func (worker *Worker) setId(msg WorkerMessage) {
msg.setId(id)
}
// PostAction posts an action to the worker. This method should not be called
// from the same goroutine that the worker runs in or deadlocks may occur
func (worker *Worker) PostAction(msg WorkerMessage, cb func(msg WorkerMessage)) {
worker.setId(msg)
@ -50,6 +52,8 @@ func (worker *Worker) PostAction(msg WorkerMessage, cb func(msg WorkerMessage))
}
}
// PostMessage posts an message to the UI. This method should not be called
// from the same goroutine that the UI runs in or deadlocks may occur
func (worker *Worker) PostMessage(msg WorkerMessage,
cb func(msg WorkerMessage),
) {