Skip to content

Commit

Permalink
Fix panic in manager when attachment-cache-dir is not set, fixes #617
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed Feb 17, 2023
1 parent 7fb6f79 commit 38e7801
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
16 changes: 11 additions & 5 deletions docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Binaries for all releases can be found on the GitHub releases pages for the [ntfy server](https://github.com/binwiederhier/ntfy/releases)
and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/releases).

## ntfy server v2.0.1 (UNRELEASED)

**Bug fixes + maintenance:**

* Avoid panic in manager when `attachment-cache-dir` is not set ([#617](https://github.com/binwiederhier/ntfy/issues/617), thanks to [@ksurl](https://github.com/ksurl))

## ntfy server v2.0.0
Released February 16, 2023

Expand Down Expand Up @@ -64,6 +70,11 @@ going. It'll only make ntfy better.
* User account signup, login, topic reservations, access tokens, tiers etc. ([#522](https://github.com/binwiederhier/ntfy/issues/522))
* `OPTIONS` method calls are not serviced when the UI is disabled ([#598](https://github.com/binwiederhier/ntfy/issues/598), thanks to [@enticedwanderer](https://github.com/enticedwanderer) for reporting)

**Special thanks:**

A big Thank-you goes to everyone who tested the user account and payments work. I very much appreciate all the feedback,
suggestions, and bug reports. Thank you, @nwithan8, @deadcade, @xenrox, @cmeis, and the others who I forgot.

## ntfy server v1.31.0
Released February 14, 2023

Expand Down Expand Up @@ -95,11 +106,6 @@ breaking-change upgrade, which required some work to get working again.

* Portuguese (thanks to [@ssantos](https://hosted.weblate.org/user/ssantos/))

**Special thanks:**

A big Thank-you goes to everyone who tested the user account and payments work. I very much appreciate all the feedback,
suggestions, and bug reports. Thank you, @nwithan8, @deadcade, and @xenrox.

## ntfy server v1.30.1
Released December 23, 2022 🎅

Expand Down
2 changes: 1 addition & 1 deletion server/message_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func (c *messageCache) ExpireMessages(topics ...string) error {
}
defer tx.Rollback()
for _, t := range topics {
if _, err := tx.Exec(updateMessagesForTopicExpiryQuery, time.Now().Unix(), t); err != nil {
if _, err := tx.Exec(updateMessagesForTopicExpiryQuery, time.Now().Unix()-1, t); err != nil {
return err
}
}
Expand Down
1 change: 1 addition & 0 deletions server/server_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ func (s *Server) maybeRemoveMessagesAndExcessReservations(r *http.Request, v *vi
if err := s.messageCache.ExpireMessages(topics...); err != nil {
return err
}
go s.pruneMessages()
return nil
}

Expand Down
51 changes: 27 additions & 24 deletions server/server_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,30 @@ func (s *Server) pruneTokens() {
}

func (s *Server) pruneAttachments() {
if s.fileCache != nil {
log.
Tag(tagManager).
Timing(func() {
ids, err := s.messageCache.AttachmentsExpired()
if err != nil {
log.Tag(tagManager).Err(err).Warn("Error retrieving expired attachments")
} else if len(ids) > 0 {
if log.Tag(tagManager).IsDebug() {
log.Tag(tagManager).Debug("Deleting attachments %s", strings.Join(ids, ", "))
}
if err := s.fileCache.Remove(ids...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error deleting attachments")
}
if err := s.messageCache.MarkAttachmentsDeleted(ids...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
}
} else {
log.Tag(tagManager).Debug("No expired attachments to delete")
}
}).
Debug("Deleted expired attachments")
if s.fileCache == nil {
return
}
log.
Tag(tagManager).
Timing(func() {
ids, err := s.messageCache.AttachmentsExpired()
if err != nil {
log.Tag(tagManager).Err(err).Warn("Error retrieving expired attachments")
} else if len(ids) > 0 {
if log.Tag(tagManager).IsDebug() {
log.Tag(tagManager).Debug("Deleting attachments %s", strings.Join(ids, ", "))
}
if err := s.fileCache.Remove(ids...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error deleting attachments")
}
if err := s.messageCache.MarkAttachmentsDeleted(ids...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
}
} else {
log.Tag(tagManager).Debug("No expired attachments to delete")
}
}).
Debug("Deleted expired attachments")
}

func (s *Server) pruneMessages() {
Expand All @@ -149,8 +150,10 @@ func (s *Server) pruneMessages() {
if err != nil {
log.Tag(tagManager).Err(err).Warn("Error retrieving expired messages")
} else if len(expiredMessageIDs) > 0 {
if err := s.fileCache.Remove(expiredMessageIDs...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error deleting attachments for expired messages")
if s.fileCache != nil {
if err := s.fileCache.Remove(expiredMessageIDs...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error deleting attachments for expired messages")
}
}
if err := s.messageCache.DeleteMessages(expiredMessageIDs...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
Expand Down
28 changes: 28 additions & 0 deletions server/server_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package server

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestServer_Manager_Prune_Messages_Without_Attachments_DoesNotPanic(t *testing.T) {
// Tests that the manager runs without attachment-cache-dir set, see #617
c := newTestConfig(t)
c.AttachmentCacheDir = ""
s := newTestServer(t, c)

// Publish a message
rr := request(t, s, "POST", "/mytopic", "hi", nil)
require.Equal(t, 200, rr.Code)
m := toMessage(t, rr.Body.String())

// Expire message
require.Nil(t, s.messageCache.ExpireMessages("mytopic"))

// Does not panic
s.pruneMessages()

// Actually deleted
_, err := s.messageCache.Message(m.ID)
require.Equal(t, errMessageNotFound, err)
}

0 comments on commit 38e7801

Please sign in to comment.