[ZEPPELIN-6540] Make ConnectionManager.userSocketMap thread-safe#5306
[ZEPPELIN-6540] Make ConnectionManager.userSocketMap thread-safe#5306HwangRock wants to merge 2 commits into
Conversation
userSocketMap was a plain HashMap accessed without synchronization from multiple Jetty WebSocket threads (login onMessage, disconnect onClose, note-list broadcast). Concurrent access could throw ConcurrentModificationException while iterating keySet(), spin during HashMap resize, or NPE via the containsKey/get TOCTOU in multicastToUser. The sibling noteSocketMap is guarded by synchronized everywhere; only userSocketMap was left unprotected. Switch userSocketMap to ConcurrentHashMap and make the compound operations atomic: compute() for add, computeIfPresent() for remove, and a single get()+null check for the read paths. Iteration over keySet() is safe under the weakly-consistent iterator. noteSocketMap keeps synchronized because it needs multi-entry atomic operations that a per-key guarantee cannot cover.
jongyoul
left a comment
There was a problem hiding this comment.
ConcurrentHashMap rejects null keys, but the WebSocket close path can pass one here. NotebookServer.onOpen() only adds the socket to connectedSockets; if the connection closes before its first message, NotebookSocket.getUser() is still null and onClose() calls removeUserConnection(null, socket). The old HashMap.containsKey(null) path only logged an absent connection, while the new computeIfPresent(null, ...) throws NullPointerException during close handling. Please add a null-user guard and a regression test for close-before-user-assignment.
|
@jongyoul Thanks for the catch — agreed the close path should never be able to reach One note from tracing the path: |
jongyoul
left a comment
There was a problem hiding this comment.
Re-reviewed at exact head. The null-user guard and regression tests address the prior concern, and the concurrent map/queue operations preserve the required invariants. The sole Selenium failure is unrelated and also recurs on master.
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
What is the Jira issue?
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: