[bug]: Memory leak in Node requester

未关闭
#1,570 0 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

  • #1612 来自 @Steve0x2a —— 已关闭,未合并

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞

调研方向

从 packages/requester-node-http/src/createHttpRequester.ts 开始,尤其关注报告中链接的 end handler,并在 Node's inspector 下使用提供的 index.mjs 脚本重现泄漏。跟踪哪些 request 和 response listener 保留了 error handler,然后验证重复搜索不再使所有 response 保持存活,并且 heap 使用量可以在 garbage collection 后趋于稳定。

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

描述

bug
Description

We got reports of memory leaks from users of React InstantSearch with Next.js.

It's easy to reproduce, with an index.mjs file :

import process from 'process';

import { algoliasearch } from 'algoliasearch';

process.stdin.resume();

const client = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');

for (let i = 0; i < 1000; i++) {
  client.search({ requests: [{ indexName: 'instant_search' }] });
}

Then run node --inspect index.mjs, in Chrome open chrome://inspect and select index.mjs.

In the memory tab, the VM instance heap size hovers around 90MB without ever going down.

image

All responses are retained by the error handler.

I tried patching it by adding req.removeAllListeners() as well as response.removeAllListeners() in this end handler : https://github.com/algolia/algoliasearch-client-javascript/blob/36ccbc9799823ccb4868460292f921d1929941e6/packages/requester-node-http/src/createHttpRequester.ts#L60-L69

It's not perfect but it now settles around 20MB. Some unnecessary values are still there but maybe this will properly get GC'd if needed.
image

I told them to use requester-fetch instead as it's now supported by recent versions of Node.js. Maybe we could make that the default ? From my own benchmarks it seems better than the Node requester currently but with my proposed fix it would make the Node requester more performant than the fetch one 😅 (with undici)

Client

All

Version

5.15.0

Relevant log output

No response

主要语言
TypeScript
星标
1.4k
派生
227
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

algolia/algoliasearch-client-javascript 的其他 Issue

查看 algolia/algoliasearch-client-javascript 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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