aerc/worker/imap/flags.go
Tim Culverhouse f4d6ade429 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>
2022-09-20 00:03:36 +02:00

96 lines
2.7 KiB
Go

package imap
import (
"fmt"
"github.com/emersion/go-imap"
"git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/worker/types"
)
func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) {
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.DeletedFlag}
uids := toSeqSet(msg.Uids)
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
return
}
if err := imapw.client.Expunge(nil); err != nil {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
} else {
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
}
}
func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) {
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.AnsweredFlag}
if !msg.Answered {
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
flags = []interface{}{imap.AnsweredFlag}
}
uids := toSeqSet(msg.Uids)
emitErr := func(err error) {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
}
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
emitErr(err)
return
}
// 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) {
case *types.Error:
err := fmt.Errorf("handleAnsweredMessages: %w", m.Error)
logging.Errorf("could not fetch headers: %v", err)
emitErr(err)
case *types.Done:
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
}
})
}
func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) {
flags := []interface{}{flagToImap[msg.Flag]}
item := imap.FormatFlagsOp(imap.AddFlags, true)
if !msg.Enable {
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
}
uids := toSeqSet(msg.Uids)
emitErr := func(err error) {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
}
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
emitErr(err)
return
}
// 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) {
case *types.Error:
err := fmt.Errorf("handleFlagMessages: %w", m.Error)
logging.Errorf("could not fetch headers: %v", err)
emitErr(err)
case *types.Done:
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
}
})
}