[y-partyserver] provider.destroy() leaks reconnect timer, hanging Node processes and delaying GC
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 65/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- node.js, typescript
- 领域
- backend, networking
调研方向
从 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
-
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.
-
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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudflare/partykit 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
cloudflare/partykit#425 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
cloudflare/partykit#424 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
cloudflare/partykit#423 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
cloudflare/partykit#419 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
cloudflare/partykit#411 ·
查看 cloudflare/partykit 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
gnosis/gnosis_vpn#661 ·
维护者通常 1 天内回复
-
content-freshness: staleOwnedContent doc comments still say 'complete receipt' (follow-up to #7912)未关闭triage:bug-verified
难度 1/5 1 小时以内 新手友好度 92/100
维护者通常 1 天内回复
-
fix: reject sourceStorage/packagedStorage bucket that differs from the stack default (fail loud)未关闭
难度 2/5 1-3 小时 新手友好度 82/100
Eyevinn/open-videocore#1300 ·
维护者通常 1 天内回复
-
nextjs-tracking
难度 2/5 1-3 小时 新手友好度 67/100
cloudflare/vinext#4390 ·
维护者通常 1 天内回复
-
kind/bug
难度 2/5 1-3 小时 新手友好度 82/100
kubernetes-sigs/headlamp#8175 ·
维护者通常 2 天内回复