From 7a674312b6f006be7c1241b10cf9063841aa1d9c Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Fri, 14 Jul 2023 23:42:19 +0200 Subject: jmap: fix crash when opening multiple instances Fix the following error when opening another aerc instance with the same jmap account: panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x9a1ffd] git.sr.ht/~rjarry/aerc/worker/jmap/cache.(*JMAPCache).get(0x99d08e?, {0xbc3c1a?, 0xc00003a160?}) git.sr.ht/~rjarry/aerc/worker/jmap/cache/cache.go:47 +0x1d git.sr.ht/~rjarry/aerc/worker/jmap/cache.(*JMAPCache).GetSession(0xc00052a030?) git.sr.ht/~rjarry/aerc/worker/jmap/cache/session.go:8 +0x29 git.sr.ht/~rjarry/aerc/worker/jmap.(*JMAPWorker).handleConnect(0xc00055e180, 0x0?) git.sr.ht/~rjarry/aerc/worker/jmap/connect.go:29 +0xd3 git.sr.ht/~rjarry/aerc/worker/jmap.(*JMAPWorker).handleMessage(0xc000311500?, {0xcc8b00?, 0xc0001fcff0?}) git.sr.ht/~rjarry/aerc/worker/jmap/worker.go:114 +0x9f git.sr.ht/~rjarry/aerc/worker/jmap.(*JMAPWorker).Run(0xc00055e180) git.sr.ht/~rjarry/aerc/worker/jmap/worker.go:177 +0x10c git.sr.ht/~rjarry/aerc/widgets.NewAccountView.func3() git.sr.ht/~rjarry/aerc/widgets/account.go:110 +0x65 created by git.sr.ht/~rjarry/aerc/widgets.NewAccountView git.sr.ht/~rjarry/aerc/widgets/account.go:103 +0x518 Do not return an error if the leveldb cannot be opened, log a message and fallback on the in-memory cache. Fixes: be0bfc1ae28b ("worker: add jmap support") Reported-by: Tim Culverhouse Signed-off-by: Robin Jarry Tested-by: Tim Culverhouse --- worker/jmap/cache/cache.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'worker/jmap/cache') diff --git a/worker/jmap/cache/cache.go b/worker/jmap/cache/cache.go index ab264744..07d23493 100644 --- a/worker/jmap/cache/cache.go +++ b/worker/jmap/cache/cache.go @@ -5,6 +5,7 @@ import ( "os" "path" + "git.sr.ht/~rjarry/aerc/log" "github.com/mitchellh/go-homedir" "github.com/syndtr/goleveldb/leveldb" ) @@ -15,29 +16,31 @@ type JMAPCache struct { blobsDir string } -func NewJMAPCache(state, blobs bool, accountName string) (*JMAPCache, error) { +func NewJMAPCache(state, blobs bool, accountName string) *JMAPCache { c := new(JMAPCache) cacheDir, err := os.UserCacheDir() if err != nil { cacheDir, err = homedir.Expand("~/.cache") if err != nil { - return nil, err + log.Errorf("homedir.Expand: %s", err) + cacheDir = "" } } - if state { + if state && cacheDir != "" { dir := path.Join(cacheDir, "aerc", accountName, "state") _ = os.MkdirAll(dir, 0o700) c.file, err = leveldb.OpenFile(dir, nil) if err != nil { - return nil, err + log.Errorf("failed to open goleveldb: %s", err) + c.mem = make(map[string][]byte) } } else { c.mem = make(map[string][]byte) } - if blobs { + if blobs && cacheDir != "" { c.blobsDir = path.Join(cacheDir, "aerc", accountName, "blobs") } - return c, nil + return c } var notfound = errors.New("key not found") -- cgit