fix(trakt): add single-flight guard to refreshAfterAuthFailure() (B4)#334
Merged
Conversation
When multiple concurrent scrobble calls each receive a 401, they now all await the same token refresh POST rather than each POSTing their own. Uses a two-level mutex (static flag + flock) to prevent Trakt's rotating refresh tokens from invalidating each other. Also adds retry logic in onPlaybackStarted/onPlaybackStopped after token refresh succeeds.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| Documentation | 3 minor |
| ErrorProne | 1 medium |
| CodeStyle | 3 minor |
| Complexity | 6 medium |
| Comprehensibility | 1 minor |
🟢 Metrics 49 complexity · 0 duplication
Metric Results Complexity 49 Duplication 0
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
…_get_temp_dir for lock file - Replace blocking usleep(5000) with Co\sleep(0.005) that yields to the Workerman event loop instead of blocking the worker process - Fall back to usleep(5000) in non-Swoole environments (unit tests) - Use sys_get_temp_dir() instead of hardcoded /tmp/ for lock file path - Token-key the in-flight refresh cache by md5(token) to prevent cross-token cache pollution between test runs - Add proper cleanup of cache entries on error paths
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #334 +/- ##
==========================================
Coverage 59.93% 59.93%
- Complexity 12927 12961 +34
==========================================
Files 480 480
Lines 42190 42297 +107
==========================================
+ Hits 25286 25352 +66
- Misses 16904 16945 +41 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
When multiple concurrent scrobble calls each receive a 401, they now all await the same token refresh POST rather than each POSTing their own. This prevents Trakt's rotating refresh tokens from invalidating each other.
Changes
TraktApi::refreshAfterAuthFailure(): New method with two-level single-flight mutex:$inFlightRefreshflag for same-process concurrency (Workerman coroutines)flock()on/tmp/trakt_refresh.lockfor cross-process safety (multiple workers)TraktPlugin::ensureFreshToken(): New method that callsrefreshAfterAuthFailure()and updates settings with new tokens. Uses its own single-flight mutex.onPlaybackStarted/onPlaybackStopped: Added retry logic - onTraktAuthenticationException, callsensureFreshToken()then retries the scrobble once.Unit test:
testRefreshAfterAuthFailureIsSingleFlightedverifies that 3 sequential calls result in exactly 1 POST to/oauth/token.Success conditions verified
./vendor/bin/phpunitgreen (3625 tests, 16 skipped)./vendor/bin/phpstan analyze src/ --level=9 --no-progressclean./vendor/bin/phpcs --standard=PSR12 -n src/cleanPR checklist