nodejs / nodejs/node

EALREADY when reconnecting socket after destroying immediately after connecting

未关闭
#55,519 11 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

net
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v22.10.0

Platform
Darwin xxx.local 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103 arm64
Subsystem

No response

What steps will reproduce the bug?
import { Socket } from 'node:net';

const socket = new Socket();
socket.on('error', (err) => {
  console.log(err);
});
socket.on('connect', () => {
  console.log('connected');
});
socket.connect({ host: 'google.com', port: 80 });
socket.once('close', () => {
  console.log('closed');
  socket.connect({ host: 'google.com', port: 80 });
});
socket.destroy();
How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

Seeing

closed
connected

in the console.

What do you see instead?
closed
Error: connect EALREADY 142.250.179.174:2404 - Local (192.168.178.178:61323)
    at internalConnect (node:net:1097:16)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
    at GetAddrInfoReqWrap.emitLookup [as callback] (node:net:1496:9)
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:132:8) {
  errno: -37,
  code: 'EALREADY',
  syscall: 'connect',
  address: '142.250.179.174',
  port: 2404
}
Additional information

Calling socket.destroySoon(); or socket.end(); does work as intended, but IMO socket.destroy(); should also allow a reconnect immediately afterwards. Note that calling socket.destroy(); on a socket that has been connected does allow for immediate reconnect afterwards as expected.

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先在报告的平台上复现提供的 node:net Socket 示例,然后跟踪 Socket 的 connect/destroy 生命周期以及堆栈跟踪中指定的 internalConnect 路径。当 destroy() 后立即重新连接能够在不出现 EALREADY 的情况下达到 connected,同时已连接时的 destroy-and-reconnect 行为保持不变,即表示完成。

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

评估

技术栈
javascript, node.js
领域
networking
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 发到你的邮箱

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