Synchronous Busy-Wait Loop + Multiple Missing await Bugs in EmbeddedChatApi.ts
Maintainer thường phản hồi trong vòng 1 ngày
@Utkarsh2294 đang làm issue này rồi.
Từ ngày 21/2/2026.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 55/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- javascript, typescript
Hướng nghiên cứu
Bắt đầu trong packages/api/src/EmbeddedChatApi.ts, xem xét handleTypingEvent, sendAttachment, getUserStatus, userInfo, userData và pattern getCommandsList hiện có. Kiểm tra hành vi upload trong AttachmentPreview.js và chạy script proof.js đã đề cập; được xem là hoàn tất khi busy-wait và các await bị thiếu được xử lý mà không thay đổi các kết quả thành công, đồng thời các lỗi fetch hoặc JSON đi đến đúng đường dẫn xử lý dự kiến.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug: Synchronous busy-wait loop and missing await calls in EmbeddedChatApi.ts
Description
packages/api/src/EmbeddedChatApi.ts contains a synchronous busy-wait anti-pattern in the typing event handler and several missing await keywords that bypass error handling in async API methods.
1. Synchronous busy-wait loop in handleTypingEvent (L354–L381)
handleTypingEvent({ typingUser, isTyping }) {
// don't wait for more than 2 seconds.
setTimeout(() => {
typingHandlerLock = 0;
}, 2000);
// eslint-disable-next-line no-empty
while (typingHandlerLock) {} // <-- busy-wait blocks the event loop
typingHandlerLock = 1;
// move user to front if typing else remove it.
const idx = this.typingUsers.indexOf(typingUser);
if (idx !== -1) {
this.typingUsers.splice(idx, 1);
}
if (isTyping) {
this.typingUsers.unshift(typingUser);
}
typingHandlerLock = 0;
const newTypingStatus = cloneArray(this.typingUsers);
this.onTypingStatusCallbacks.forEach((callback) =>
callback(newTypingStatus)
);
}
This attempts to implement a mutex using a synchronous while loop. This is fundamentally wrong in JavaScript:
- The lock is unnecessary. JavaScript is single-threaded. WebSocket callbacks already execute atomically — there is no concurrent access to
typingUsers, so no lock is needed. - If the lock is ever stuck at
1(e.g. an exception thrown between L367–L376 before the reset on L376), thewhileloop becomes an infinite loop that freezes the browser tab. - The
setTimeoutsafety net on L362 cannot rescue it. While thewhileloop is spinning synchronously, the event loop is blocked, so thesetTimeoutcallback sitting in the task queue will never execute. - The
// eslint-disable-next-line no-emptycomment shows the linter flagged this and it was suppressed rather than fixed.
2. Missing await on fetch() in sendAttachment (L1068)
async sendAttachment(file, fileName, fileDescription = "", threadId = undefined) {
try {
// ...
const response = fetch(`${this.host}/api/v1/rooms.upload/${this.rid}`, {
method: "POST",
body: form,
headers: { "X-Auth-Token": authToken, "X-User-Id": userId },
}).then((r) => r.json());
return response;
} catch (err) {
console.log(err); // <-- this never catches fetch errors
}
}
Because fetch() is not awaited, the Promise is returned before it settles. This means the try/catch block is completely bypassed for any fetch error — network failures, auth errors, server errors all become unhandled Promise rejections instead of being caught. The console.log(err) on L1078 is dead code for fetch-related errors.
Caller impact (AttachmentPreview.js L55–68):
const submit = async () => {
setIsPending(true);
await RCInstance.sendAttachment(data, fileName, ...);
toggle(); // runs even if upload failed
setData(null); // clears data even if upload failed
};
The caller has no try/catch either. When a network error occurs, the unhandled rejection crashes silently — no error message is shown to the user, but the attachment preview closes as if the upload succeeded.
Proof
A minimal reproduction script (proof.js) demonstrates this using real fetch() against an unreachable host:
// Mirrors EmbeddedChatApi.ts L1068 — fetch() without await
async function sendAttachment_current(host) {
try {
const response = fetch(`${host}/api/v1/rooms.upload/test`, {
method: "POST",
}).then((r) => r.json());
return response;
} catch (err) {
console.log(" [INTERNAL try/catch] Caught:", err.message);
return "error was handled internally";
}
}
// Same code with await added
async function sendAttachment_fixed(host) {
try {
const response = await fetch(`${host}/api/v1/rooms.upload/test`, {
method: "POST",
}).then((r) => r.json());
return response;
} catch (err) {
console.log(" [INTERNAL try/catch] Caught:", err.message);
return "error was handled internally";
}
}
Output (node proof.js):
=== CURRENT CODE (no await — from EmbeddedChatApi.ts L1068) ===
ERROR ESCAPED past try/catch to caller: fetch failed
The try/catch inside sendAttachment NEVER ran.
In the real app (AttachmentPreview.js L55-68), the caller
has no try/catch either, so this becomes an unhandled rejection.
=== FIXED CODE (with await added) ===
[INTERNAL try/catch] Caught: fetch failed
Result: error was handled internally
The current code's try/catch is completely bypassed. Adding await makes it work as intended.
3. Missing await on response.json() in three methods (L1246, L1263, L1280)
// getUserStatus (L1246)
const data = response.json(); // missing await
return data;
// userInfo (L1263)
const data = response.json(); // missing await
return data;
// userData (L1280)
const data = response.json(); // missing await
return data;
Note: Because these are async functions and callers use await, JavaScript's Promise auto-flattening means the data resolves correctly in the happy path. However, the missing await means any .json() parse error (e.g. a non-JSON response from the server) would not be catchable within these methods — the error propagates as an unhandled rejection instead of being handled locally.
This is also inconsistent with the rest of the file — e.g. getCommandsList on L1229 correctly uses const data = await response.json().
Impact summary
| Bug | Affected feature | Impact |
|---|---|---|
| Busy-wait loop | Typing indicators | Unnecessary lock in single-threaded JS; potential infinite loop/tab freeze if lock gets stuck |
Missing await in sendAttachment |
File uploads | try/catch bypassed — fetch errors become unhandled rejections; UI closes as if upload succeeded |
Missing await in getUserStatus |
User presence | Inconsistent error handling; .json() parse errors become unhandled rejections |
Missing await in userInfo |
User profiles | Same as above |
Missing await in userData |
User lookups | Same as above |
Steps to reproduce
sendAttachment error-handling bypass (proof script)
No server needed. Run the included proof script:
node proof.js
This uses real fetch() against an unreachable host (localhost:1) and shows that the current code's try/catch never fires, while the fixed version catches the error correctly.
sendAttachment in the live app
- Go to https://rocketchat.github.io/EmbeddedChat/
- Log in with username
test_acc, passwordtest_acc - Open browser DevTools (F12) → Console tab
- Open DevTools → Network tab → set throttling to Offline
- Click the attachment/file-upload button in the chat input
- Select any file and confirm the upload
- Observe: the attachment preview closes as if the upload succeeded, but the console shows an unhandled Promise rejection (not a caught error)
Busy-wait loop (code inspection)
Read packages/api/src/EmbeddedChatApi.ts line 366 — the while (typingHandlerLock) {} with a suppressed ESLint warning on the line above. The lock is unnecessary in single-threaded JavaScript.
Suggested fix
- Remove the busy-wait entirely. The lock is unnecessary in single-threaded JS — array operations on
typingUsersare already atomic. RemovetypingHandlerLock, thewhileloop, and thesetTimeout. - Add
awaitbeforefetch()insendAttachmentso thetry/catchactually catches network/server errors. - Add
awaitbeforeresponse.json()ingetUserStatus,userInfo, anduserDatafor consistency and proper error handling — matching the pattern used elsewhere in the file (e.g.getCommandsListon L1229).
- Ngôn ngữ chính
- JavaScript
- Star
- 171
- Fork
- 421
- Merge trung bình
- 3 ngày 6 giờ
- Pull request đã merge (30 ngày)
- 4
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RocketChat/EmbeddedChat
-
Bug: username fallback in updateUserUsername never runs since the ddp-client migrationCó thể đã có người làm @MuhammadFaizan-prog đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
RocketChat/EmbeddedChat#1489 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: Low contrast on color swatch borders in Theme Lab (Layout Editor)Có thể đã có người làm @yashsarvaiya45 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
RocketChat/EmbeddedChat#1486 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug(api): _doConnect swallows connection/auth errors and marks failed sessions as authenticatedCó thể đã có người làm @ashwanisingh011 đã nhận 1 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
RocketChat/EmbeddedChat#1484 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug: White screen crash (`Cannot read properties of undefined (reading 'members')`) when rendering UserMention outside MarkupInteractionContextCó thể đã có người làm @swarnendusahu10-dotcom đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
RocketChat/EmbeddedChat#1482 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug: UIKit PreviewBlock cover image tiles repeatedly and stretches across viewport when zoomed outCó thể đã có người làm @swarnendusahu10-dotcom đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
RocketChat/EmbeddedChat#1480 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của RocketChat/EmbeddedChat
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Life-Framework/Life-Framework#7017 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
e/DevOps enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Accenture/sfmc-devtools#2795 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
agent/guide documentation help wanted stage/010-actionable
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
kubestellar/console-kb#4003 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
FilOzone/synapse-sdk#1201 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày