fix(cmaf): serialize ensure_cmaf_session to kill concurrent-start race
The CMAF session cache is an Arc<RwLock<Option<CmafSession>>>. The old
ensure_cmaf_session did a read-check then dropped the guard then did
its POST unlocked, which meant two concurrent callers would each see
"no session", each POST /session/start, and each receive a different
session_id + infos. The second to finish stored its session as the new
cache, clobbering the first.
That was a problem because file/url wraps the per-track AES key against
whichever session_id the X-Session-Id header carried. If task A got its
wrapped key under session X but the cache got overwritten to Y before
decrypt time, the HKDF over Y-infos produced the wrong session_key and
the AES-CBC unwrap failed with "Unpad Error" — which is exactly the
warning that showed up in prefetch runs during parallel downloads and
caused the >3s gap between offline-cached tracks when the next track's
prefetch had silently fallen back to legacy.
The symptom in logs:
[CMAF] Starting new session ← task A
[CMAF] Starting new session ← task B (same ms)
Session started: id=bHoSYBFx...
Session started: id=bJ-w2wgm... ← cache now holds B
[PREFETCH] CMAF failed for track N: Content key unwrap failed:
AES-CBC unwrap failed: Unpad Error
Fix: double-checked lock pattern. Fast path stays on the read guard
for existing-valid sessions. Slow path takes the write guard, re-checks
under exclusive ownership, and only the first caller actually hits
session/start. Everyone else wakes up and returns the freshly-cached
value.
Compile check only — full behaviour validation in the running dev
session after this commit.
cc
This commit is contained in:
1 file changed
+28
-3
@@ -2290,10 +2290,24 @@ impl QobuzClient {
|
||||
|
||||
/// Ensure we have a valid CMAF session, renewing if expired.
|
||||
/// Returns `(session_id, infos)` for use with file/url and key derivation.
|
||||
///
|
||||
/// Concurrency note: this method serializes concurrent session
|
||||
/// renewals. Without that, two overlapping callers could both see
|
||||
/// "no session" on the read side, each POST /session/start, each get
|
||||
/// DIFFERENT `infos`, and the second one to finish would overwrite
|
||||
/// the first in the cache. Any `get_file_url` response whose wrapped
|
||||
/// key was tied to the first session then unwrapped with the second
|
||||
/// session's key and blew up with AES-CBC "Unpad Error" — which
|
||||
/// manifested as prefetch CMAF failures + downloaded-but-gappy
|
||||
/// transitions between offline tracks.
|
||||
///
|
||||
/// Fix: a double-checked lock pattern on the write guard. Fast path
|
||||
/// uses a read guard; slow path acquires the write guard, re-checks
|
||||
/// under exclusive ownership, and only one caller hits the network.
|
||||
pub async fn ensure_cmaf_session(&self) -> Result<(String, String)> {
|
||||
let now = get_timestamp();
|
||||
|
||||
// Check existing session (with 60s safety margin)
|
||||
// Fast path: existing session with > 60s left.
|
||||
{
|
||||
let guard = self.cmaf_session.read().await;
|
||||
if let Some(ref cs) = *guard {
|
||||
@@ -2303,7 +2317,18 @@ impl QobuzClient {
|
||||
}
|
||||
}
|
||||
|
||||
// Need a new session
|
||||
// Slow path: take the write lock and re-check. Concurrent callers
|
||||
// end up here one at a time; after the first finishes POST
|
||||
// session/start, the rest find the freshly-populated cache and
|
||||
// return without hitting the network.
|
||||
let mut guard = self.cmaf_session.write().await;
|
||||
if let Some(ref cs) = *guard {
|
||||
if cs.expires_at > now + 60 {
|
||||
return Ok((cs.session_id.clone(), cs.infos.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
// We're the one task that actually starts a session.
|
||||
log::info!("[CMAF] Starting new session");
|
||||
let timestamp = get_timestamp();
|
||||
let sig = sign_session_start(timestamp);
|
||||
@@ -2340,7 +2365,7 @@ impl QobuzClient {
|
||||
let session_id = resp.session_id.clone();
|
||||
let infos_clone = infos.clone();
|
||||
|
||||
*self.cmaf_session.write().await = Some(CmafSession {
|
||||
*guard = Some(CmafSession {
|
||||
session_id: resp.session_id,
|
||||
infos,
|
||||
expires_at: resp.expires_at,
|
||||
|
||||
Reference in new issue
Block a user