[bug]: Memory leak in Node requester

オープン
#1,570 コメント 0 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

  • #1612 @Steve0x2a による — マージされずにクローズ

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
node.js, typescript

調査の方向性

packages/requester-node-http/src/createHttpRequester.ts から始め、特にレポートでリンクされている end handler に注目し、提供された index.mjs スクリプトを Node's inspector 下で実行してリークを再現します。どの request および response listener が error handler を保持しているかを追跡し、繰り返し検索してもすべての response が保持され続けることはなく、garbage collection 後に heap 使用量が安定できることを確認します。

索引モデルが 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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

algolia/algoliasearch-client-javascript のほかの issue

algolia/algoliasearch-client-javascript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。