fix(apns): copy client per request to avoid shared host mutation - #882
Open
ridwanakf wants to merge 1 commit into
Open
fix(apns): copy client per request to avoid shared host mutation#882ridwanakf wants to merge 1 commit into
ridwanakf wants to merge 1 commit into
Conversation
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.
Fixes #881.
Opening this proactively with a fix. If mutating the shared client is intentional and I have misread the send path, happy to close.
I went looking at why some iOS pushes can land in the wrong APNs environment. It comes down to
getApnsClient: it picks the environment by callingProduction()/Development()on the shared globalApnsClient, and in apns2 those two methods mutate the receiver and return it rather than handing back a configured copy. So every call rewritesApnsClient.Hoston the single process-wide client and returns that same pointer.Once more than one push is in flight (and
worker_numdefaults toruntime.NumCPU(), so that is the normal case) two things go wrong:ApnsClient.Host. It happens even when both requests want the same host, because both still write the field.PushToIOSresolves the client once and then fans out into goroutines that each read the host when they build their request, so a later request can redirect pushes that were already in flight. Aproduction: truenotification can go to the sandbox, and adevelopment: trueone to production.It is easy to miss in practice because Apple just returns a generic
BadDeviceTokenfor a wrong-environment token, so it looks like ordinary token churn and the rate tracks your traffic mix. The full write-up, a deterministic reproduction, and the-raceoutput are in #881.The fix is small: copy the client per request and set the host on the copy, so the global is never touched.
apns2.Clientis a plain struct with no lock, and itsHTTPClientandTokenare safe to share, so the copy is cheap and keeps the same connection pool.Tests live in
notify/notification_apns_host_test.go:mastertoday),-race.I kept this to
getApnsClientalone. Running the whole package under-racealso turns up a separate, older race on theresp.Logs/newTokensappends inPushToIOS, but that one is already being handled in #874, so I left it out here.