From 62c0d27d1e52d259968a97399e5fc816f3b4d21a Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 23 Mar 2018 10:23:23 -0700 Subject: [PATCH] push_notifications: Handle errors with token reuse. If an Android token has been used to connect a given device with multiple Zulip servers, and then is expired, we would 500 in trying to remove the Zulip-side registration for it, because the code assumed there was only one such registration. If a token is no longer valid, it's invalid for all servers, so the correct fix is to just remove them all. --- zerver/lib/push_notifications.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index cd96fc728a..d82845ca25 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -214,9 +214,9 @@ def send_android_push_notification(devices: List[DeviceToken], data: Dict[str, A if error in ['NotRegistered', 'InvalidRegistration']: for reg_id in reg_ids: logging.info("GCM: Removing %s" % (reg_id,)) - - device = DeviceTokenClass.objects.get(token=reg_id, kind=DeviceTokenClass.GCM) - device.delete() + # We remove all entries for this token (There + # could be multiple for different Zulip servers). + DeviceTokenClass.objects.filter(token=reg_id, kind=DeviceTokenClass.GCM).delete() else: for reg_id in reg_ids: logging.warning("GCM: Delivery to %s failed: %s" % (reg_id, error))