Skip to content

OpenBot Mobile: stop the push token loop, register one at a time, and wait out a 429 - #2881

Merged
davidmckayv merged 2 commits into
mainfrom
fix/mobile-push-loop
Dec 3, 2026
Merged

davidmckayv merged 2 commits into
mainfrom
fix/mobile-push-loop

Conversation

@davidmckayv

Copy link
Copy Markdown
Contributor

What this changes

OpenBot Mobile build 7 (app 1.2.7), signed in with notifications on, sends about 9,000 to 14,000 PUT /api/v2/organizations/<org>/people/me/devices/<id> a minute, across the person's organizations. The deployment's per-address rate limit then blocks the phone. The next sign-in metadata read gets a 429, and the app says "You can't sign in to this OpenBot from OpenBot Mobile yet. Ask an admin to update OpenBot."

The cause. keepPushRegistered subscribed Notifications.addPushTokenListener(renew), and renew ran registerPush, which called getExpoPushTokenAsync({ projectId }) with no device token. In expo-notifications 58.1.10 (the version mobile/package.json pins, read from its source in node_modules):

  • getExpoPushTokenAsync does options.devicePushToken || (await getDevicePushTokenAsync()).
  • getDevicePushTokenAsync calls the native PushTokenManager.getDevicePushTokenAsync().
  • The native module emits onDevicePushToken on every fetch, whether the token changed or not. On iOS, PushTokenModule.didRegister resolves the promise and then calls sendEvent. On Android, the fetch's completion listener resolves and then calls onNewToken(token), which calls sendEvent.
  • addPushTokenListener hands that event to every listener. Expo's own TokenEmitter.ts warns that calling the token getter inside the listener "may lead to an infinite loop".

So every registration told the listener again, and the listener registered again: one PUT per organization each pass, with nothing to stop it and no back-off.

The fix (mobile/src/notifications.ts):

  1. The listener registers with the token it is handed (getExpoPushTokenAsync({ projectId, devicePushToken })) and never asks the phone for one, so nothing on the listener path can trigger another token event. When a token arrives that the phone has already registered, nothing happens. A move to another organization reuses the token already known. Only a start asks the phone for its token.
  2. One registration at a time. Anything that asks while a registration is running (a move, a token) causes one more run after it, with the newest token, however many asked. The triggers are the same as before: a start once signed in, a move to another organization, and a real token change.
  3. Back-off on 429. When the server refuses with 429, the registration stops. It doesn't try the other organizations, because the limit is on the phone's address. It waits out Retry-After. If there is no Retry-After, it waits 30 seconds, doubling after each 429 in a row up to 30 minutes. Then it tries once more, with whatever asked in the meantime. ApiError now carries retryAfterMs, read from the header.

Sign-in under a rate limit (mobile/src/auth.ts, mobile/src/oauth.ts):

  • A 429 on the metadata read now shows the existing "Couldn't reach OpenBot. Try again in a moment." It no longer tells the person to ask an admin to update OpenBot.
  • signInEnded() counted every 4xx as the sign-in being over, so a refresh refused with 429 deleted the refresh token and signed the person out. A 429 is now treated like a 5xx: the sign-in is kept, and the app tries again later.

Where it runs

On the phone only. This changes no server code.

  • New state that outlives a request? On the phone: the last device token it registered (module state, for as long as the app runs), plus the queue and back-off state inside keepPushRegistered's closure. None of it lives on the server.
  • What happens on the second replica? No change. The server gets the same idempotent PUTs, just far fewer.
  • Anything serialised? Only on the phone: one registration at a time, inside keepPushRegistered.
  • Anything fanned out to a browser? None.
  • New listener, port, or schedule? None. One timer on the phone, there only while it waits out a 429.

Boundary and audit

  • No gateway, refusal or audit changes.

API and database

  • This changes nothing under api/ and nothing in the database. The phone sends the same request with the same body, only fewer of them.

Changelog

OpenBot Mobile.

  • Fixed: a phone with notifications on no longer registers itself thousands of times a minute. Build 7 sent the device registration over and over until the deployment's rate limit blocked the phone, which then could not sign in. Now the phone registers once per organization when it starts, when the person moves to another organization, and when its push token changes. It waits when the server says it is getting too many requests.
  • Fixed: a rate limit no longer reads as "Ask an admin to update OpenBot." Signing in while the deployment refuses requests as too many now says "Couldn't reach OpenBot. Try again in a moment.", and the phone stays signed in.

Proof

Red, then green. The commits are split so the tests land first: 7253f9e has the tests only, and the commit after it has the fix.

The stand-in for expo-notifications in mobile/tests/isolated/push-everywhere.check.ts now behaves like 58.1.10 does on a phone: getExpoPushTokenAsync with no devicePushToken calls getDevicePushTokenAsync, and that tells every addPushTokenListener subscriber the token, changed or not, on a later turn of the event loop. The old stand-in returned a token and told nobody, which is how the loop got past the tests.

Against the code before the fix (tests-only commit):

(fail) signed in, the phone registers once in each organization, though Expo tells the app the token it just asked for
       1,498 registrations in 300 ms, where there should be 2
(fail) signed in, the phone registers now, again when Expo changes its token, and again on moving organization
       678 registrations in 100 ms, where there should be 2
(fail) one registration runs at a time: whatever comes during it makes one more, with the newest token
       256 registrations in flight at once, where there should be 1
(fail) refused as too many, the phone waits out Retry-After before it registers again
       11,192 attempts in 200 ms after the 429, where there should be 1
(fail) refused as too many with no Retry-After, the phone still waits before it registers again
       6,086 attempts in 1 s after the 429, where there should be 1
(fail) a phone the person didn't turn notifications on for is left alone, and never asked
       10 registrations, left running by the earlier tests' loops
(fail) refused as too many, signing in says it couldn't reach OpenBot, never that OpenBot needs updating
       "You can't sign in to this OpenBot from OpenBot Mobile yet. Ask an admin to update OpenBot."
(fail) a refresh refused as too many keeps the sign-in
       the refresh resolved as signed out, and the refresh token was deleted
(fail) a refused refresh ends the sign-in; a server that is down, or a rate limit, does not
       signInEnded(429) was true

With the fix: push-everywhere.check.ts 8 pass, rate-limited-sign-in.check.ts 2 pass, oauth.test.ts 11 pass. Also run on the fix commit: bun test mobile/tests from the root (360 pass, 0 fail), npm run test:packages (5 files passed), npm run typecheck, and biome check on the changed files.

Not run on a phone. There is no iOS or Android device here, so all of the above runs against stand-ins for Expo's push library and a stand-in server. To confirm on a phone (Tyler), with build N+1 and an account in at least two organizations, while watching the devices PUTs for that account in the server or Cloudflare logs:

  1. Install build N+1 and sign in, then turn notifications on (the Notifications screen, Allow notifications) if they are not on already. Expect one PUT .../people/me/devices/<id> per organization, then nothing more. Leave it for a minute and check the count stays flat.
  2. Kill the app and relaunch it, still signed in. Expect one more PUT per organization, then nothing more.
  3. Switch organization from the app. Expect one PUT per organization, then nothing more.
  4. Background the app for a few minutes, then bring it back. Expect no new PUTs.
  5. Sign out, then sign back in. Expect one DELETE per organization at sign-out, then one PUT per organization after signing in.
  6. Send yourself something that pushes (an approval, say). Expect the notification on the phone.
  7. If you can rate-limit the phone's address, sign out and try to sign in while it is blocked. Expect "Couldn't reach OpenBot. Try again in a moment.", not "Ask an admin to update OpenBot", and no tight loop of PUTs once the block lifts.

…ne at a time, and waiting out a 429

The stand-in for expo-notifications now does what 58.1 does on a phone: asking for the Expo push token without a device token asks the phone for one, and the phone tells every push token listener the token it gave, changed or not. Against the code before the fix these fail: hundreds of registrations in 300ms where there should be two.

Also: signing in refused as too many says it couldn't reach OpenBot, and a refresh refused as too many keeps the refresh token.
…tration at a time, and wait out a 429

Asking expo-notifications for the Expo push token without a device token asks the phone for one, and the phone then tells every push token listener. The listener asked again, so it told itself again: build 7 sent about 10,000 device registrations a minute until the rate limit blocked the phone.

The listener now registers with the token it is handed and never asks the phone for one. A token already registered registers nothing. One registration runs at a time, and whatever asks during it makes one more after it, with the newest token. Refused as too many, the phone waits out Retry-After, or a wait that doubles from 30 seconds, before it tries again.

Sign-in refused as too many now says it couldn't reach OpenBot, not that OpenBot needs updating, and a refresh refused as too many no longer ends the sign-in.
@davidmckayv
davidmckayv marked this pull request as ready for review December 3, 2026 17:47
@davidmckayv
davidmckayv added this pull request to the merge queue Dec 3, 2026
Merged via the queue into main with commit 0384d8c Dec 3, 2026
24 checks passed
@davidmckayv
davidmckayv deleted the fix/mobile-push-loop branch December 3, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant