Files
aerc-fork-mirror/commands/msg/delete.go
Robin Jarry 0211c9bf23 reload: fix crash when reloading via IPC
When reloading the configuration with :reload, global variables in the
config package are reset to their startup values and then, the config is
parsed from disk. While the parsing is done, these variables are
temporarily in an inconsistent and possibly invalid state.

When commands are executed interactively from aerc, they are handled by
the main goroutine which also deals with UI rendering. No UI render will
be done while :reload is in progress.

However, the IPC socket handler runs in an independent goroutine. This
has the unfortunate side effect to let the UI goroutine to run while
config parsing is in progress and causes crashes:

[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6bb142]

goroutine 1 [running]:
git.sr.ht/~rjarry/aerc/lib/log.PanicHandler()
	lib/log/panic-logger.go:51 +0x6cf
panic({0xc1d960?, 0x134a6e0?})
	/usr/lib/go/src/runtime/panic.go:783 +0x132
git.sr.ht/~rjarry/aerc/config.(*StyleConf).getStyle(0xc00038b908?, 0x4206b7?)
	config/style.go:386 +0x42
git.sr.ht/~rjarry/aerc/config.StyleSet.Get({0x0, 0x0, 0x0, {0x0, 0x0, 0x0}}, 0x421a65?, 0x0)
	config/style.go:408 +0x8b
git.sr.ht/~rjarry/aerc/config.(*UIConfig).GetStyle(...)
	config/ui.go:379
git.sr.ht/~rjarry/aerc/lib/ui.(*TabStrip).Draw(0xc000314700, 0xc000192230)
	lib/ui/tab.go:378 +0x15b
git.sr.ht/~rjarry/aerc/lib/ui.(*Grid).Draw(0xc000186fc0, 0xc0002c25f0)
	lib/ui/grid.go:126 +0x28e
git.sr.ht/~rjarry/aerc/app.(*Aerc).Draw(0x14b9f00, 0xc0002c25f0)
	app/aerc.go:192 +0x1fe
git.sr.ht/~rjarry/aerc/lib/ui.Render()
	lib/ui/ui.go:155 +0x16b
main.main()
	main.go:310 +0x997

Make the reload operation safe by changing how config objects are
exposed and updated. Change all objects to be atomic pointers. Expose
public functions to access their value atomically. Only update their
value after a complete and successful config parse. This way the UI
thread will always have access to a valid configuration.

NB: The account configuration is not included in this change since it
cannot be reloaded.

Fixes: https://todo.sr.ht/~rjarry/aerc/319
Reported-by: Anachron <gith@cron.world>
Signed-off-by: Robin Jarry <robin@jarry.cc>
2025-09-08 12:19:51 +02:00

181 lines
4.4 KiB
Go

package msg
import (
"errors"
"fmt"
"slices"
"time"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)
type Delete struct {
MultiFileStrategy *types.MultiFileStrategy `opt:"-m" action:"ParseMFS" complete:"CompleteMFS" desc:"Multi-file strategy."`
}
func init() {
commands.Register(Delete{})
}
func (Delete) Description() string {
return "Delete the selected message(s)."
}
func (Delete) Context() commands.CommandContext {
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
}
func (Delete) Aliases() []string {
return []string{"delete", "delete-message"}
}
func (d *Delete) ParseMFS(arg string) error {
if arg != "" {
mfs, ok := types.StrToStrategy[arg]
if !ok {
return fmt.Errorf("invalid multi-file strategy %s", arg)
}
d.MultiFileStrategy = &mfs
}
return nil
}
func (Delete) CompleteMFS(arg string) []string {
return commands.FilterList(types.StrategyStrs(), arg, nil)
}
// :delete is allowed if either:
// - The `restrict-delete` setting is false, or
// - The current directory is either the Trash or the Junk folder.
func mayDeleteOnSelectedDirectory() bool {
selAccount := app.SelectedAccount()
if selAccount == nil {
log.Debugf("No account selected; defaulting to allowing deletes")
} else if selAccount.AccountConfig().RestrictedDeletes {
if dirs := selAccount.Directories(); dirs != nil {
currentDir := dirs.Directory(selAccount.SelectedDirectory())
if currentDir == nil {
return false
}
return currentDir.Role == models.TrashRole || currentDir.Role == models.JunkRole
}
}
return true
}
func (d Delete) Execute(args []string) error {
if !mayDeleteOnSelectedDirectory() {
return errors.New("Forbidden by `restrict-delete` setting")
}
h := newHelper()
store, err := h.store()
if err != nil {
return err
}
uids, err := h.markedOrSelectedUids()
if err != nil {
return err
}
acct, err := h.account()
if err != nil {
return err
}
sel := store.Selected()
marker := store.Marker()
marker.ClearVisualMark()
// caution, can be nil
next := findNextNonDeleted(uids, store)
store.Delete(uids, d.MultiFileStrategy, func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
var s string
if len(uids) > 1 {
s = "%d messages deleted"
} else {
s = "%d message deleted"
}
app.PushStatus(fmt.Sprintf(s, len(uids)), 10*time.Second)
mv, isMsgView := h.msgProvider.(*app.MessageViewer)
if isMsgView {
if !config.Ui().NextMessageOnDelete {
app.RemoveTab(h.msgProvider, true)
} else {
// no more messages in the list
if next == nil {
app.RemoveTab(h.msgProvider, true)
acct.Messages().Select(-1)
ui.Invalidate()
return
}
lib.NewMessageStoreView(next, mv.MessageView().SeenFlagSet(),
store, app.CryptoProvider(), app.DecryptKeys,
func(view lib.MessageView, err error) {
if err != nil {
app.PushError(err.Error())
return
}
nextMv, err := app.NewMessageViewer(acct, view)
if err != nil {
app.PushError(err.Error())
return
}
app.ReplaceTab(mv, nextMv, next.Envelope.Subject, true)
})
}
} else {
if next == nil {
// We deleted the last message, select the new last message
// instead of the first message
acct.Messages().Select(-1)
}
}
case *types.Error:
marker.Remark()
store.Select(sel.Uid)
app.PushError(msg.Error.Error())
case *types.Unsupported:
marker.Remark()
store.Select(sel.Uid)
// notmuch doesn't support it, we want the user to know
app.PushError(" error, unsupported for this worker")
}
})
return nil
}
func findNextNonDeleted(deleted []models.UID, store *lib.MessageStore) *models.MessageInfo {
var next, previous *models.MessageInfo
stepper := []func(){store.Next, store.Prev}
for _, stepFn := range stepper {
previous = nil
for {
next = store.Selected()
if next != nil && !slices.Contains(deleted, next.Uid) {
if _, deleted := store.Deleted[next.Uid]; !deleted {
return next
}
}
if next == nil || previous == next {
// If previous == next, this is the last
// message. Set next to nil either way
next = nil
break
}
stepFn()
previous = next
}
}
if next != nil {
store.Select(next.Uid)
}
return next
}