← Back to Pull requests

[ZEPPELIN-6540] Make ConnectionManager.userSocketMap thread-safe #5306

Closed
unknown closed this pull request without merging on Jul 21, 2026 13:48 UTC
View on GitHub
unknown unknown commented on Jul 15, 2026 13:37 UTC
Author

What is this PR for?


ConnectionManager.userSocketMap (user -> Queue<NotebookSocket>) is a plain HashMap, but every access to it is unsynchronized: addUserConnection/removeUserConnection (writes) and multicastToUser/unicastParagraph/forAllUsers/broadcastNoteListExcept (reads and keySet() iteration). The sibling field noteSocketMap is guarded by synchronized (noteSocketMap) at every access — only userSocketMap was left unprotected.

These paths run on different Jetty WebSocket threads (login onMessage, disconnect onClose, note-list broadcast broadcastNoteListUpdate). Under concurrent connect/disconnect — e.g. a burst of client reconnects after a server restart — this leads to:

  • ConcurrentModificationException while iterating keySet(), aborting the note-list broadcast so some users stop receiving updates

  • possible CPU spin / lost entries during HashMap resize

  • NullPointerException from the containsKey + get TOCTOU in multicastToUser/unicastParagraph
  • The map value is already a ConcurrentLinkedQueue, so only the map itself was unprotected.

    Fix: switch userSocketMap to ConcurrentHashMap and make the compound operations atomic:

  • addUserConnection: compute(...) so the queue create-or-reuse and the add happen in one atomic map operation (closing the add-after-remove window that a bare computeIfAbsent(...).add(...) would leave open)

  • removeUserConnection: computeIfPresent(...), removing the key when the queue becomes empty

  • multicastToUser / unicastParagraph: a single get() + null check, removing the TOCTOU/NPE

  • forAllUsers / broadcastNoteListExcept: unchanged — keySet() iteration is safe under ConcurrentHashMap's weakly-consistent iterator
  • noteSocketMap intentionally keeps synchronized: it needs multi-entry atomic operations (removeConnectionFromAllNote, checkCollaborativeStatus) that a per-key ConcurrentHashMap guarantee 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 via forAllUsers. On the old HashMap this reproduces ConcurrentModificationException / 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 publish compute() guarantees.
  • Ran the full ConnectionManagerTest 5× 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 under ConcurrentHashMap); 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.
  • Does this needs documentation? No.
  • Diff: 436 lines (+327 / -109) across 5 files Reviewer: ParkGyeongTae Review duration: 8651m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.2% (MEDIUM) · Higher = larger blast radius
    45.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.9% · Safe to merge
    17.9%