Fix panic in manager when attachment-cache-dir is not set, fixes #617

This commit is contained in:
binwiederhier 2023-02-17 15:56:48 -05:00
parent 7fb6f794e5
commit 38e7801b41
5 changed files with 68 additions and 30 deletions

View file

@ -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) 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). 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 ## ntfy server v2.0.0
Released February 16, 2023 Released February 16, 2023
@ -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)) * 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) * `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 ## ntfy server v1.31.0
Released February 14, 2023 Released February 14, 2023
@ -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/)) * 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 ## ntfy server v1.30.1
Released December 23, 2022 🎅 Released December 23, 2022 🎅

View file

@ -536,7 +536,7 @@ func (c *messageCache) ExpireMessages(topics ...string) error {
} }
defer tx.Rollback() defer tx.Rollback()
for _, t := range topics { 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 return err
} }
} }

View file

@ -506,6 +506,7 @@ func (s *Server) maybeRemoveMessagesAndExcessReservations(r *http.Request, v *vi
if err := s.messageCache.ExpireMessages(topics...); err != nil { if err := s.messageCache.ExpireMessages(topics...); err != nil {
return err return err
} }
go s.pruneMessages()
return nil return nil
} }

View file

@ -116,7 +116,9 @@ func (s *Server) pruneTokens() {
} }
func (s *Server) pruneAttachments() { func (s *Server) pruneAttachments() {
if s.fileCache != nil { if s.fileCache == nil {
return
}
log. log.
Tag(tagManager). Tag(tagManager).
Timing(func() { Timing(func() {
@ -138,7 +140,6 @@ func (s *Server) pruneAttachments() {
} }
}). }).
Debug("Deleted expired attachments") Debug("Deleted expired attachments")
}
} }
func (s *Server) pruneMessages() { func (s *Server) pruneMessages() {
@ -149,9 +150,11 @@ func (s *Server) pruneMessages() {
if err != nil { if err != nil {
log.Tag(tagManager).Err(err).Warn("Error retrieving expired messages") log.Tag(tagManager).Err(err).Warn("Error retrieving expired messages")
} else if len(expiredMessageIDs) > 0 { } else if len(expiredMessageIDs) > 0 {
if s.fileCache != nil {
if err := s.fileCache.Remove(expiredMessageIDs...); err != nil { if err := s.fileCache.Remove(expiredMessageIDs...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error deleting attachments for expired messages") log.Tag(tagManager).Err(err).Warn("Error deleting attachments for expired messages")
} }
}
if err := s.messageCache.DeleteMessages(expiredMessageIDs...); err != nil { if err := s.messageCache.DeleteMessages(expiredMessageIDs...); err != nil {
log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted") log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
} }

View file

@ -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)
}