420f236d31
There are concurrent threads that are accessing and modifying IMAPWorker.seqMap (the mapping of sequence numbers to message UIDs). This can lead to crashes when trying to add and remove a message ID. panic: runtime error: index out of range [391] with length 390 goroutine 1834 [running]: git.sr.ht/~rjarry/aerc/logging.PanicHandler() logging/panic-logger.go:47 +0x6de panic({0xa41760, 0xc0019b3290}) /usr/lib/golang/src/runtime/panic.go:838 +0x207 git.sr.ht/~rjarry/aerc/worker/imap.(*IMAPWorker).handleFetchMessages.func1() worker/imap/fetch.go:214 +0x185 created by git.sr.ht/~rjarry/aerc/worker/imap.(*IMAPWorker).handleFetchMessages worker/imap/fetch.go:209 +0x12b Use a map which makes more sense than a simple array for random access operations. Also, it allows better typing for the key values. Protect the map with a mutex. Add internal API to access the map. Add basic unit tests to ensure that concurrent access works. Fixes: https://todo.sr.ht/~rjarry/aerc/49 Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Moritz Poldrack <moritz@poldrack.dev>
114 lines
3 KiB
Go
114 lines
3 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
|
|
}
|
|
var deleted []uint32
|
|
ch := make(chan uint32)
|
|
done := make(chan interface{})
|
|
go func() {
|
|
defer logging.PanicHandler()
|
|
|
|
for seqNum := range ch {
|
|
if uid, found := imapw.seqMap.Pop(seqNum); !found {
|
|
imapw.worker.Logger.Printf("handleDeleteMessages unknown seqnum: %v", seqNum)
|
|
} else {
|
|
deleted = append(deleted, uid)
|
|
}
|
|
}
|
|
done <- nil
|
|
}()
|
|
if err := imapw.client.Expunge(ch); err != nil {
|
|
imapw.worker.PostMessage(&types.Error{
|
|
Message: types.RespondTo(msg),
|
|
Error: err,
|
|
}, nil)
|
|
} else {
|
|
<-done
|
|
imapw.worker.PostMessage(&types.MessagesDeleted{
|
|
Message: types.RespondTo(msg),
|
|
Uids: deleted,
|
|
}, nil)
|
|
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
|
|
}
|
|
imapw.worker.PostAction(&types.FetchMessageHeaders{
|
|
Uids: msg.Uids,
|
|
}, func(_msg types.WorkerMessage) {
|
|
switch m := _msg.(type) {
|
|
case *types.Error:
|
|
err := fmt.Errorf("handleAnsweredMessages: %v", m.Error)
|
|
imapw.worker.Logger.Printf("could not fetch headers: %s", 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
|
|
}
|
|
imapw.worker.PostAction(&types.FetchMessageHeaders{
|
|
Uids: msg.Uids,
|
|
}, func(_msg types.WorkerMessage) {
|
|
switch m := _msg.(type) {
|
|
case *types.Error:
|
|
err := fmt.Errorf("handleFlagMessages: %v", m.Error)
|
|
imapw.worker.Logger.Printf("could not fetch headers: %s", err)
|
|
emitErr(err)
|
|
case *types.Done:
|
|
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
|
}
|
|
})
|
|
}
|