Symptom

Kraken’s nightly architectural scan flagged PinProvider’s bearer-token cache as unsynchronized under concurrent access. refreshIfStale() checked and wrote two separate mutable fields (cachedBearerToken, lastReadMs) with no lock, so N threads could all observe a stale cache simultaneously and all call readTokenFile() — producing redundant file reads and interleaved writes.

Root cause

private var cachedBearerToken: String? and private var lastReadMs: Long were plain mutable fields with no @Volatile or lock guard. The read-check-write sequence in refreshIfStale() was not atomic: between one thread reading lastReadMs and writing the new values, another thread could make the same staleness decision and race to refresh.

Fix

Prevention