Skip to content

Commit

Permalink
fix(GODT-2649): Clean up cache files if connector create message fails
Browse files Browse the repository at this point in the history
Cleanup cache files we generate when creating messages from the
connector if the db transaction was not committed successfully.
  • Loading branch information
LBeernaertProton committed May 24, 2023
1 parent 75fd429 commit 395f1bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"os"
"runtime"
"strings"

Expand Down Expand Up @@ -214,7 +215,7 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
messageForMBox := make(map[imap.InternalMailboxID][]imap.InternalMessageID)
mboxInternalIDMap := make(map[imap.MailboxID]imap.InternalMailboxID)

return user.db.Write(ctx, func(ctx context.Context, tx *ent.Tx) error {
err := user.db.Write(ctx, func(ctx context.Context, tx *ent.Tx) error {
client := tx.Client()

for _, message := range update.Messages {
Expand Down Expand Up @@ -336,6 +337,19 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message

return nil
})

// Clean up cache messages that were created if the transaction failed.
if err != nil {
for _, message := range messagesToCreate {
if err := user.store.DeleteUnchecked(message.InternalID); err != nil {
if !os.IsNotExist(err) {
logrus.WithError(err).Errorf("Failed to delete cache message %v after failed transaction", message.InternalID)
}
}
}
}

return err
}

// applyMessageMailboxesUpdated applies a MessageMailboxesUpdated update.
Expand Down
6 changes: 6 additions & 0 deletions store/write_controlled_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func (w *WriteControlledStore) SetUnchecked(messageID imap.InternalMessageID, re
return w.impl.Set(messageID, reader)
}

// DeleteUnchecked allows the user to bypass lock access. This will only work if you can guarantee that the data being
// delete is not being used anywhere.
func (w *WriteControlledStore) DeleteUnchecked(messageIDs ...imap.InternalMessageID) error {
return w.impl.Delete(messageIDs...)
}

func (w *WriteControlledStore) Delete(messageID ...imap.InternalMessageID) error {
for _, id := range messageID {
if err := func() error {
Expand Down

0 comments on commit 395f1bc

Please sign in to comment.