[bug]: Memory leak in Node requester

Đang mở
#1,570 0 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

  • #1612 của @Steve0x2a — đã đóng, không merge

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
node.js, typescript

Hướng nghiên cứu

Bắt đầu với packages/requester-node-http/src/createHttpRequester.ts, đặc biệt là end handler được liên kết trong báo cáo, và tái hiện leak bằng script index.mjs được cung cấp dưới Node's inspector. Theo dõi những listener của request và response nào giữ lại error handler, sau đó xác minh rằng các lần tìm kiếm lặp lại không còn giữ tất cả response tồn tại và rằng mức sử dụng heap có thể ổn định sau garbage collection.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
TypeScript
Star
1.4k
Fork
227
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của algolia/algoliasearch-client-javascript

Tất cả issue của algolia/algoliasearch-client-javascript

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.