[engine.io-client] close() called in an upgrade listener does not close the socket

未关闭
#5,595 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@adityajai119 已经在做这个了。

开始于 2026年11月11日。

  • #5596 来自 @adityajai119 —— 未关闭

评估

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

调研方向

The issue points to packages/engine.io-client/lib/socket.ts and says a fix and test are already being submitted; linked pull request #5596 is open. Read that pull request and the upgrade handling around this.emitReserved("upgrade", transport) and this.upgrading = false to understand the change. The reported behavior is resolved when calling close() in an upgrade listener closes the socket immediately with reason forced close.

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

描述

Describe the bug

Calling socket.close() in a listener of the upgrade event does not close the socket. The socket stays in the closing state and the WebSocket connection stays open. Since the client no longer answers the pings in that state, the server closes the connection once pingTimeout expires, so the close event comes pingInterval + pingTimeout later (45 seconds with the default values), with the reason transport close instead of forced close.

The upgrade event is emitted before upgrading is set back to false, so close() waits for the upgrade to finish, that is, for an upgrade event that has already been emitted.

To Reproduce

Engine.IO server version: 6.6.11

Engine.IO client version: 6.6.8 (also reproduced on main)

Server and client

const { createServer } = require("node:http");
const { attach } = require("engine.io");
const { Socket } = require("engine.io-client");

const httpServer = createServer();
const engine = attach(httpServer, { pingInterval: 2000, pingTimeout: 1000 });

httpServer.listen(3000, () => {
  const socket = new Socket("http://localhost:3000");
  let closedAt;

  socket.on("upgrade", () => {
    closedAt = Date.now();
    socket.close();
    console.log("readyState right after close():", socket.readyState);
  });

  socket.on("close", (reason) => {
    console.log(`"close" event ${Date.now() - closedAt} ms after close(), reason: ${reason}`);
    engine.close();
    httpServer.close();
  });
});

Output:

readyState right after close(): closing
"close" event 3059 ms after close(), reason: transport close

Expected behavior

The socket is closed right away, as when close() is called at any other time:

"close" event 2 ms after close(), reason: forced close
readyState right after close(): closed

Platform:

  • Device: desktop
  • OS: Windows, Node.js 24.22.1

Additional context

In packages/engine.io-client/lib/socket.ts, this.upgrading = false comes after this.emitReserved("upgrade", transport). I'm opening a PR with a fix and a test.

AI-assisted: found and reproduced with an AI coding agent; the example above was run locally.

主要语言
TypeScript
星标
63.2k
派生
10.2k
平均合并
6 天 4 小时
30 天内合并 PR
6

环境准备

从这里开始

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

socketio/socket.io 的其他 Issue

查看 socketio/socket.io 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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