Skip to content

fix(api): remove redundant typingHandlerLock and spinlock loop - #1359

Open
jhalak101205-cpu wants to merge 1 commit into
RocketChat:developfrom
jhalak101205-cpu:fix/remove-typing-handler-lock
Open

fix(api): remove redundant typingHandlerLock and spinlock loop#1359
jhalak101205-cpu wants to merge 1 commit into
RocketChat:developfrom
jhalak101205-cpu:fix/remove-typing-handler-lock

Conversation

@jhalak101205-cpu

Copy link
Copy Markdown

Hey @Aryan-Verma-999, here is the Pull Request for the issue. Please review it and let me know if you have any feedback!

Summary

Removes the global typingHandlerLock variable, fallback setTimeout, and synchronous while (typingHandlerLock) {} spinlock loop from packages/api/src/EmbeddedChatApi.ts.

Context & Issue

Fixes #1354.

Logic & Implementation Details

  1. Leveraging Event-Loop Task Queue Semantics:
    In single-threaded JavaScript, incoming WebSocket events are queued and processed sequentially off the Event Loop. Synchronous function execution is atomic and completes without thread context switching. Therefore, mutex spinlocks like while (lock) {} are redundant and pose a deadlock risk if the lock remains set.

  2. Atomic Array Management:
    handleTypingEvent now directly and safely performs atomic array updates:

    • Uses indexOf and splice to clear existing instances of the user from this.typingUsers.
    • Uses unshift to prepend the active typing user to the front of the list.
    • Operates in microsecond execution time with zero main-thread blocking.
  3. Clean Subscriber Broadcasting:
    Creates an immutable copy via cloneArray(this.typingUsers) and broadcasts the updated status array to all onTypingStatusCallbacks listeners.

Verification

  • Tested rapid and concurrent typing event execution locally (http://localhost:5173/ & http://localhost:6006/).
  • Verified zero main-thread deadlocks or CPU spikes.
  • Workspace builds cleanly via yarn build.

@CLAassistant

CLAassistant commented Aug 12, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Potential main-thread deadlock in EmbeddedChatApi.ts when typingHandlerLock remains set

2 participants