[ZEPPELIN-6540] Make ConnectionManager.userSocketMap thread-safe #5306
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 436 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.364
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #5306"
general
weight: 0.35
ParkGyeongTae
What is this PR for?
ConnectionManager.userSocketMap(user -> Queue<NotebookSocket>) is a plainHashMap, but every access to it is unsynchronized:addUserConnection/removeUserConnection(writes) andmulticastToUser/unicastParagraph/forAllUsers/broadcastNoteListExcept(reads andkeySet()iteration). The sibling fieldnoteSocketMapis guarded bysynchronized (noteSocketMap)at every access — onlyuserSocketMapwas left unprotected.These paths run on different Jetty WebSocket threads (login
onMessage, disconnectonClose, note-list broadcastbroadcastNoteListUpdate). Under concurrent connect/disconnect — e.g. a burst of client reconnects after a server restart — this leads to:ConcurrentModificationExceptionwhile iteratingkeySet(), aborting the note-list broadcast so some users stop receiving updatesHashMapresizeNullPointerExceptionfrom thecontainsKey+getTOCTOU inmulticastToUser/unicastParagraphThe map value is already a
ConcurrentLinkedQueue, so only the map itself was unprotected.Fix: switch
userSocketMaptoConcurrentHashMapand make the compound operations atomic:addUserConnection:compute(...)so the queue create-or-reuse and theaddhappen in one atomic map operation (closing the add-after-remove window that a barecomputeIfAbsent(...).add(...)would leave open)removeUserConnection:computeIfPresent(...), removing the key when the queue becomes emptymulticastToUser/unicastParagraph: a singleget()+ null check, removing the TOCTOU/NPEforAllUsers/broadcastNoteListExcept: unchanged —keySet()iteration is safe underConcurrentHashMap's weakly-consistent iteratornoteSocketMapintentionally keepssynchronized: it needs multi-entry atomic operations (removeConnectionFromAllNote,checkCollaborativeStatus) that a per-keyConcurrentHashMapguarantee does not cover, so a single-strategy migration would not be correct there.What type of PR is it?
Bug Fix
Todos
[ ] - none
What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-6540
How should this be tested?
Added two unit tests in
ConnectionManagerTest:userSocketMapConcurrentAccessTest: 8 writer threads add/remove connections while 2 reader threads iterate viaforAllUsers. On the oldHashMapthis reproducesConcurrentModificationException/NullPointerException; with the fix it passes with no thrown exception.userSocketMapConcurrentAddPreservesAllConnectionsTest: 16 threads concurrently add unique sockets for the same user, then assert every socket is present and the final queue size matches — validates the atomic publishcompute()guarantees.Ran the full
ConnectionManagerTest5× consecutively — stable, no intermittent failures.Note on the add-after-remove window: it is extremely narrow and did not reproduce as a deterministic failing test even under heavy contention (16 churn threads, 16×5000 iterations). The
compute()fix closes it by construction (atomic per-key create-and-add underConcurrentHashMap); the correctness rests on that plus the concurrent-add invariant test rather than a RED→GREEN of the exact interleaving.Screenshots (if appropriate)
Questions:
Does the license files need to update? No.
Is there breaking changes for older versions? No — public method signatures are unchanged.