imap: fix data race on seqMap array
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>
This commit is contained in:
parent
389c0f82a7
commit
420f236d31
6 changed files with 146 additions and 28 deletions
worker/imap
|
@ -2,6 +2,7 @@ package imap
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/logging"
|
||||
|
@ -26,9 +27,11 @@ func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) {
|
|||
defer logging.PanicHandler()
|
||||
|
||||
for seqNum := range ch {
|
||||
i := seqNum - 1
|
||||
deleted = append(deleted, imapw.seqMap[i])
|
||||
imapw.seqMap = append(imapw.seqMap[:i], imapw.seqMap[i+1:]...)
|
||||
if uid, found := imapw.seqMap.Pop(seqNum); !found {
|
||||
imapw.worker.Logger.Printf("handleDeleteMessages unknown seqnum: %v", seqNum)
|
||||
} else {
|
||||
deleted = append(deleted, uid)
|
||||
}
|
||||
}
|
||||
done <- nil
|
||||
}()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue