partysocket: failed connections are reported as a clean close with code 1000, so `shouldReconnectOnClose` stops reconnecting

Đang mở Phù hợp với người mới
#428 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
82/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ệ
node.js, typescript
Lĩnh vực
backend, networking

Hướng nghiên cứu

Bắt đầu với packages/partysocket/src/ws.ts, đặc biệt là _handleError và _disconnect, mà issue xác định là nguồn có khả năng gây ra sự kiện close tổng hợp. So sánh sự kiện close được gửi đi với sự kiện close của WebSocket nền trong các trường hợp kết nối thất bại và kết nối bị ngắt. Thêm hoặc cập nhật kiểm thử cho các trường hợp đó; hoàn thành khi sự kiện báo mã lỗi của kết nối nền và shouldReconnectOnClose cho phép thử kết nối lại.

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

Mô tả

When a connection attempt fails or an open connection drops (server down, network lost), PartySocket dispatches a close event with code: 1000, wasClean: true and reason: "unknown reason", and dispatches it before the error event. The underlying WebSocket reports 1006 / wasClean: false. Because the same synthetic event is passed to the new shouldReconnectOnClose option, a predicate like e => e.code !== 1000 ("reconnect unless the server closed normally") turns reconnection off the first time the server is unreachable.

Reproduction (npm i [email protected], Node 26, nothing listening on port 9):

// repro.mjs
import WebSocket from "partysocket/ws";

// Nothing listens on this port, so every attempt fails.
const ws = new WebSocket("ws://127.0.0.1:9", undefined, {
  minReconnectionDelay: 100,
  // Reconnect unless the server closed normally.
  shouldReconnectOnClose: (e) => e.code !== 1000,
});
ws.addEventListener("close", (e) => console.log("close", e.code, e.wasClean, e.reason));
ws.addEventListener("error", () => console.log("error"));
setTimeout(() => {
  console.log("shouldReconnect", ws.shouldReconnect, "retryCount", ws.retryCount);
  process.exit(0);
}, 2000);

node repro.mjs

Actual:

close 1000 true unknown reason
error
shouldReconnect false retryCount 0

Expected: the close event carries the real code (close 1006 false, which is what new WebSocket("ws://127.0.0.1:9") itself reports), the predicate returns true, and the socket keeps retrying.

The same happens when an established connection is dropped without a close frame (server process killed): the user sees close 1000 true and no reconnect.

Tested with partysocket 1.3.0 and main (cloudflare/partykit f0a2e97d; packages/partysocket/src/ws.ts last changed in 6b977426), on Node 24.22.2, Node 26.13.0 and Bun 1.4.4.

Likely cause: _handleError calls this._disconnect(undefined, ...), so _disconnect(code = 1000, reason) synthesizes new CloseEvent(1000, ...) and removes the listeners, so the real close event from the socket is never seen.

Credit: https://ucsbplse.github.io/

Ngôn ngữ chính
TypeScript
Star
1.3k
Fork
84
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.