From 89fbdd72074a1062b6d9960b5447f4b2e7f8bbf1 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:28:04 -0600 Subject: [PATCH] serialize push subscription cleanup and test malformed tokens --- frigate/comms/webpush.py | 46 +++++++++++++++------------- frigate/test/test_webpush_cleanup.py | 15 ++++++++- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/frigate/comms/webpush.py b/frigate/comms/webpush.py index 92a1413ba3..3788912317 100644 --- a/frigate/comms/webpush.py +++ b/frigate/comms/webpush.py @@ -181,6 +181,7 @@ class WebPushClient(Communicator): self.web_pushers: dict[str, list[WebPusher]] = {} self.expired_subs: dict[str, list[str]] = {} self.expired_subs_lock = threading.Lock() + self.cleanup_lock = threading.Lock() self.suspended_cameras: dict[str, int] = { c.name: 0 # type: ignore[misc] for c in self.config.cameras.values() @@ -261,36 +262,37 @@ class WebPushClient(Communicator): self.claim_headers[endpoint] = self.vapid.sign(claim) def cleanup_registrations(self) -> None: - # delete any expired subs - with self.expired_subs_lock: - expired_subs, self.expired_subs = self.expired_subs, {} + # concurrent cleanups would each restore what the other removed + with self.cleanup_lock: + with self.expired_subs_lock: + expired_subs, self.expired_subs = self.expired_subs, {} - for user, expired in expired_subs.items(): - user_subs = [] + for user, expired in expired_subs.items(): + user_subs = [] - # get all subscriptions, removing ones that are expired - stored_user: User | None = User.get_or_none(User.username == user) + # get all subscriptions, removing ones that are expired + stored_user: User | None = User.get_or_none(User.username == user) - # the user was deleted after the endpoint expired - if stored_user is None: - continue - - for token in stored_user.notification_tokens: - if isinstance(token, dict) and token.get("endpoint") in expired: + # the user was deleted after the endpoint expired + if stored_user is None: continue - user_subs.append(token) + for token in stored_user.notification_tokens: + if isinstance(token, dict) and token.get("endpoint") in expired: + continue - # overwrite the database and reset web pushers - User.update(notification_tokens=user_subs).where( - User.username == user - ).execute() + user_subs.append(token) - self.web_pushers[user] = _build_web_pushers(user, user_subs) + # overwrite the database and reset web pushers + User.update(notification_tokens=user_subs).where( + User.username == user + ).execute() - logger.info( - f"Cleaned up {len(expired)} notification subscriptions for {user}" - ) + self.web_pushers[user] = _build_web_pushers(user, user_subs) + + logger.info( + f"Cleaned up {len(expired)} notification subscriptions for {user}" + ) def suspend_notifications(self, camera: str, minutes: int) -> None: """Suspend notifications for a specific camera.""" diff --git a/frigate/test/test_webpush_cleanup.py b/frigate/test/test_webpush_cleanup.py index 2985fbae4b..d2f7ebc401 100644 --- a/frigate/test/test_webpush_cleanup.py +++ b/frigate/test/test_webpush_cleanup.py @@ -16,7 +16,7 @@ DEAD = {"endpoint": "https://push.example.com/dead", "keys": {}} class TestCleanupRegistrations(unittest.TestCase): def setUp(self): self.db = SqliteDatabase(":memory:") - self.db.bind([User]) + self.enterContext(self.db.bind_ctx([User])) self.db.create_tables([User]) User.create( @@ -27,6 +27,7 @@ class TestCleanupRegistrations(unittest.TestCase): self.client.web_pushers = {} self.client.expired_subs = {} self.client.expired_subs_lock = threading.Lock() + self.client.cleanup_lock = threading.Lock() def tearDown(self): self.db.close() @@ -43,3 +44,15 @@ class TestCleanupRegistrations(unittest.TestCase): self.assertEqual(User.get_by_id("alice").notification_tokens, [LIVE]) self.assertEqual(self.client.expired_subs, {}) _build.assert_called_once_with("alice", [LIVE]) + + @patch("frigate.comms.webpush._build_web_pushers", return_value=[]) + def test_keeps_malformed_tokens_and_prunes_expired(self, _build): + malformed = [None, {"keys": {}}] + User.update(notification_tokens=[LIVE, *malformed, DEAD]).execute() + self.client.expired_subs = {"alice": [DEAD["endpoint"]]} + + self.client.cleanup_registrations() + + self.assertEqual( + User.get_by_id("alice").notification_tokens, [LIVE, *malformed] + )