partysocket: a URL provider that rejects with undefined gives an unhandled rejection and stops the reconnection

Đang mở Phù hợp với người mới
#434 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
networking

Hướng nghiên cứu

Lỗi nằm ở .catch() của _connect() trong packages/partysocket/src/ws.ts (khoảng dòng 585-588), đoạn này đọc err.message từ một provider URL hoặc protocols bị reject. Chạy repro trong issue với partysocket 1.3.0 để xác nhận TypeError và trạng thái CONNECTING bị treo. Hoàn thành khi một provider reject với undefined hoặc null phát ra sự kiện error và lên lịch lần thử tiếp theo, không có unhandled rejection, và bộ test của partysocket chạy đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

The .catch() of _connect() reads err.message. When the URL provider (or the protocols provider) rejects with undefined or null, for example with reject() and no argument, that line throws a TypeError inside the .catch(). The result is an unhandled rejection, no error event, and no next attempt: the socket stays in CONNECTING.

https://github.com/cloudflare/partykit/blob/partysocket%401.3.0/packages/partysocket/src/ws.ts#L585-L588

Repro

The packages: [email protected]. Install them with npm i [email protected].

// npm install, then: node repro.ts (Node.js 22.18 or later runs TypeScript directly)
import WebSocket from "partysocket/ws";

process.on("unhandledRejection", (reason) => {
  console.log("unhandledRejection:", String(reason));
});

let calls = 0;
const ws = new WebSocket(
  () => {
    calls++;
    // for example, reject() in a token fetcher
    return Promise.reject(undefined);
  },
  undefined,
  { minReconnectionDelay: 50, maxReconnectionDelay: 50 },
);
let errors = 0;
ws.addEventListener("error", () => errors++);

setTimeout(() => {
  console.log("provider calls in 500 ms:", calls);
  console.log("error events:", errors);
  console.log("readyState:", ws.readyState);
  ws.close();
}, 500);

node repro.ts (Node.js 22.18 or later runs TypeScript directly)

Actual

unhandledRejection: TypeError: Cannot read properties of undefined (reading 'message')
provider calls in 500 ms: 1
error events: 0
readyState: 0

Expected

An error event, and a new attempt after the reconnection delay, as for a provider that rejects with an Error. No unhandled rejection.

A fix

A fix with a new test is on the branch fix/partysocket-provider-rejects-nullish of my fork, at 54a3d36. On a GitHub Actions runner, the new test fails before the change and passes after it, and the whole partysocket suite passes. This repository accepts pull requests only from collaborators, so I did not open a pull request. I can open one if you allow it, or you can take the commit.

I found this with AI assistance (Claude) during a review of WebSocket client libraries. The repro above was run with partysocket 1.3.0 on Node.js 24.22.2 on Linux (a GitHub Actions runner), and the output above is from that run. The fix was also prepared with AI assistance (Claude).

Ngôn ngữ chính
TypeScript
Star
1.3k
Fork
86
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloudflare/partykit

Tất cả issue của cloudflare/partykit

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.