Fix thread-safe auth token storage - #245
Conversation
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 48 minutes and 16 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR refactors token management across three interceptors by introducing a centralized, thread-safe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@app/src/main/java/org/piramalswasthya/cho/network/interceptors/AuthTokenManager.kt`:
- Around line 12-28: tmcToken and tmcJwt must be stored and read as one atomic
snapshot to avoid interleaved reads/writes; replace the two
AtomicReference<String> fields (tmcToken, tmcJwt) and the separate
setters/getters (setTmcToken, setTmcJwt, getTmcToken, getTmcJwt) with a single
AtomicReference holding an immutable TmcCredentials data class and add
setTmcCredentials(token, jwt) and getTmcCredentials() methods; update the TMC
interceptor to call getTmcCredentials() once and migrate all callers that
currently call setTmcJwt()/setTmcToken() separately to use
setTmcCredentials(token, jwt) so token and jwt are always consistent atomically.
In
`@app/src/test/java/org/piramalswasthya/cho/network/interceptors/AuthTokenManagerTest.kt`:
- Around line 40-63: Update the test concurrentWritesRemainReadable to (1)
ensure TMC token and JWT are read atomically from an API on AuthTokenManager
(add or use a method like getTmcTokenJwtSnapshot()/getTmcCredentialsSnapshot()
that returns both values together) and assert that the numeric suffix of the
returned token ("tmc-N") matches the numeric suffix of the returned jwt
("jwt-N"), and (2) wrap executor shutdown/awaitTermination in a finally block so
the executor is always shut down even on timeouts/failures; keep the existing
per-field prefix assertions if you want, but replace cross-field consistency
checks with the atomic-snapshot based equality check using
AuthTokenManager.getTmcToken/getTmcJwt only if they are returned together
atomically via the new snapshot API.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: aa308064-d7f9-41f9-a94e-a8cb1acbc5f6
📒 Files selected for processing (5)
app/src/main/java/org/piramalswasthya/cho/network/interceptors/AuthTokenManager.ktapp/src/main/java/org/piramalswasthya/cho/network/interceptors/TokenESanjeevaniInterceptor.ktapp/src/main/java/org/piramalswasthya/cho/network/interceptors/TokenInsertAbhaInterceptor.ktapp/src/main/java/org/piramalswasthya/cho/network/interceptors/TokenInsertTmcInterceptor.ktapp/src/test/java/org/piramalswasthya/cho/network/interceptors/AuthTokenManagerTest.kt
|
|
@sushant-bizbrolly please review |



Description
Make the auth token storage used by the network interceptors thread-safe so concurrent logins, sync jobs, and logout flows do not overwrite each other through shared mutable companion-object state.
Related Issue
Fixes #127
PR Category
Changes Made
AuthTokenManagerbacked byAtomicReferencevalues.Testing Performed
./gradlew app:testNiramayDebugUnitTest --tests org.piramalswasthya.cho.network.interceptors.AuthTokenManagerTest --no-daemonChecklist
Additional Notes for Reviewers
The change is intentionally small and keeps the existing interceptor API shape so the rest of the app does not need a larger refactor.
Summary by CodeRabbit
Refactor
Tests