Repository navigation
OpenBot Mobile: stop the push token loop, register one at a time, and wait out a 429 - #2881
Merged
Merged
Conversation
…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
marked this pull request as ready for review
December 3, 2026 17:47
davidmckayv
requested review from
MikeRyanDev,
dustywusty,
guidovizoso,
mxmzb,
samjulien and
tylerslaton
as code owners
December 3, 2026 17:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
keepPushRegisteredsubscribedNotifications.addPushTokenListener(renew), andrenewranregisterPush, which calledgetExpoPushTokenAsync({ projectId })with no device token. In expo-notifications 58.1.10 (the versionmobile/package.jsonpins, read from its source innode_modules):getExpoPushTokenAsyncdoesoptions.devicePushToken || (await getDevicePushTokenAsync()).getDevicePushTokenAsynccalls the nativePushTokenManager.getDevicePushTokenAsync().onDevicePushTokenon every fetch, whether the token changed or not. On iOS,PushTokenModule.didRegisterresolves the promise and then callssendEvent. On Android, the fetch's completion listener resolves and then callsonNewToken(token), which callssendEvent.addPushTokenListenerhands that event to every listener. Expo's ownTokenEmitter.tswarns 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):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.Retry-After. If there is noRetry-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.ApiErrornow carriesretryAfterMs, read from the header.Sign-in under a rate limit (
mobile/src/auth.ts,mobile/src/oauth.ts):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.
keepPushRegistered's closure. None of it lives on the server.PUTs, just far fewer.keepPushRegistered.Boundary and audit
API and database
api/and nothing in the database. The phone sends the same request with the same body, only fewer of them.Changelog
OpenBot Mobile.
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.tsnow behaves like 58.1.10 does on a phone:getExpoPushTokenAsyncwith nodevicePushTokencallsgetDevicePushTokenAsync, and that tells everyaddPushTokenListenersubscriber 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):
With the fix:
push-everywhere.check.ts8 pass,rate-limited-sign-in.check.ts2 pass,oauth.test.ts11 pass. Also run on the fix commit:bun test mobile/testsfrom the root (360 pass, 0 fail),npm run test:packages(5 files passed),npm run typecheck, andbiome checkon 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:PUT .../people/me/devices/<id>per organization, then nothing more. Leave it for a minute and check the count stays flat.PUTper organization, then nothing more.PUTper organization, then nothing more.PUTs.DELETEper organization at sign-out, then onePUTper organization after signing in.PUTs once the block lifts.