TryBugHunt

AI Attempt: Disconnected user still receives chat broadcasts

Mediumjavascript

Chat Room Router Leaks Messages Due to Event Listener Identity Mismatch

0:00
Step 1: Test Traffic & Expose Flaw
/**
 * Socket Client Representation
 */
class SocketClient {
  constructor(socketId) {
    this.id = socketId;
    this.isClosed = false;
    this.inbox = [];
  }

  deliver(message) {
    this.inbox.push(message);
  }

  close() {
    this.isClosed = true;
  }
}

module.exports = { SocketClient };
Step 1: Test Traffic & Expose FlawClient-side WASM

Run a scenario below to trace socket subscriptions. Prove that after a user leaves a room, their socket still receives broadcast messages.

Choose Traffic Scenario to TestClick to simulate
Architectural Guide & Interview Notes

Deep Dive: AI Attempt: Disconnected user still receives chat broadcasts

An engineering analysis of why this AI-generated Pull Request passed isolated unit tests, how multi-tenant traffic exposes the architectural flaw, and how interviewers score your response.

The Root Cause Anti-Pattern

In JavaScript event emitters and pub/sub buses, unsubscribe(topic, handler) relies on function reference equality (fn !== handler). Passing an anonymous arrow function wrapper (like (msg) => socket.deliver(msg)) during unsubscribe creates a brand new function reference, failing the equality check and silently leaving dead sockets subscribed to private broadcast channels.

The Interviewer's Scoring Rubric

Candidates are assessed on whether they jump to trial-and-error editing or follow a hypothesis-driven approach. A "Strong Hire" candidate restates the data flow between modules, explains why single-client unit tests passed, crafts an adversarial probe showing cross-tenant leakage, and implements a surgical 1-line patch without breaking existing contracts.

Candidate Strategy for the Code Comprehension Round

  1. 1Map Module ContractsTrace how the gateway invokes the underlying service. Note what data is passed by reference vs. by value.
  2. 2Break It With An Adversarial ProbeSimulate concurrent or sequential traffic across multiple identities to force shared state mutation.
  3. 3Deliver a Surgical PatchMake the minimal required diff to ensure proper per-client state isolation without refactoring unnecessary files.