Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[y-partyserver] provider.destroy() leaks reconnect timer, hanging Node processes and delaying GC

未关闭 适合新手
#402 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
65/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清

调研方向

从 y-partyserver/provider 入口点开始,跟踪 WebsocketProvider.destroy() 经过 WebSocket close 事件和重连调度的流程。运行 issue 中的 Node.js 复现脚本,然后验证销毁 provider 后不会留下使事件循环保持运行的重连计时器,并且进程会在没有延迟的情况下退出。

由索引模型根据 Issue 内容生成。

描述

I maintain YAOS a local-first sync engine running inside Obsidian, built on DOs and y-partyserver. While debugging a CI pipeline that inexplicably took 140 seconds instead of 36 seconds, we discovered that y-partyserver is leaking timers directly into the event loop every time a connection is torn down.

The Bug
When provider.destroy() is called on a WebsocketProvider in y-partyserver, the WebSocket's onclose event listener unconditionally queues a reconnect timer via setTimeout(setupWS, backoff, provider).

While the setupWS callback correctly checks this.shouldConnect and aborts (meaning it doesn't actually reconnect), the setTimeout handle itself is never stored or cleared during the destroy() lifecycle.

The Real-World Impact

  1. Delayed Garbage Collection (Electron/SPA): In long-lived environments like ours, the setTimeout closure retains the entire provider instance and its massive associated Y.Doc in memory until the timer naturally fires (up to 2.5s). In environments with flapping networks where connections are repeatedly destroyed and recreated, this creates a massive GC backlog and stutters the UI thread.

  2. Node.js / CI Hangs: The Node event loop refuses to exit until the backoff timer fires. In test suites that instantiate and destroy multiple providers, this compounds into massive CI delays, forcing developers to use process.exit(0) hacks just to allow scripts to terminate cleanly.

This issue is inherited DNA from y-websocket. The exact same architectural flaw was reported upstream 6 months ago (see yjs/y-websocket#198), but it has not merged. Since y-partyserver ships its own adapted provider implementation, it needs to be patched here.

Steps to Reproduce
Running this Node.js script proves the event loop is held hostage by the leaked timer, even after the provider is explicitly destroyed:

import YSyncProvider from "y-partyserver/provider";
import WebSocket from "ws";

async function run() {
  const ydoc = new Y.Doc();
  const provider = new YSyncProvider("ws://127.0.0.1:8787", "room", ydoc, { 
    WebSocketPolyfill: WebSocket 
  });

  // Wait for initial connection logic
  await new Promise(r => setTimeout(r, 100));

  console.log("Destroying provider...");
  provider.destroy();

  // Wait a tick for the ws 'close' event to fire and schedule the leak
  await new Promise(r => setTimeout(r, 100));

  const handles = process._getActiveHandles().filter(
    h => h.constructor.name === 'Timeout' || h.constructor.name === 'TimerWrap'
  );
  
  console.log(`Active timers preventing exit: ${handles.length}`);
  // Process hangs here for up to 2.5 seconds instead of exiting immediately
}

run();
主要语言
TypeScript
星标
1.3k
派生
84
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudflare/partykit 的其他 Issue

查看 cloudflare/partykit 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。