memory leak on client side with websocket sessions

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

Maintainer thường phản hồi trong vòng 2 ngày

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

  • #154 của @VastBlast — đã đó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
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
networking

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện TypeScript của server và client được cung cấp, sau đó kiểm tra vòng đời của newWebSocketRpcSession và các promises được nó giữ lại bằng --inspect. Công việc được xem là hoàn tất khi các lời gọi websocket lặp lại không còn giữ các promises không được sử dụng và bộ nhớ của client ổn định mà không cần khởi động lại session.

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

Mô tả

There seems to be a memory leak on the client side with websocket sessions, unless I am doing something wrong. I understand there are disposal rules to consider, especially with websockets per the docs, but I tried many different things and the only way to clear the memory of unused promises seems to be to restart the session. See the reproduction below.

I also ran it with --inspect and it seems there are forever-growing Promise's kept in the memory.

Server:

import { RpcTarget, newWebSocketRpcSession } from "capnweb";
import { WebSocketServer } from "ws";

class Api extends RpcTarget {
  sendMeText(text: string): string {
    return `got: ${text}`;
  }
}

const server = new WebSocketServer({ port: 8787, perMessageDeflate: false });

server.on("connection", ws => {
  newWebSocketRpcSession(ws as any, new Api());
});

console.log("ws://localhost:8787");

Client:

import { newWebSocketRpcSession } from "capnweb";

type Api = {
  sendMeText(text: string): string;
};

const session = newWebSocketRpcSession<Api>("ws://localhost:8787");
const text = "abc123".repeat(2000);

let calls = 0;

setInterval(() => {
  console.log(
    `calls=${calls} heapUsed=${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(1)}MB`
  );
}, 1000);


while (true) {
  await session.sendMeText(text);
  ++calls;
  await new Promise(resolve => setTimeout(resolve, 1)); // delay 1ms
}

// this doesn't prevent a memory leak either, same result:
while(true){
  const res = session.sendMeText(text);
  await res;
  res[Symbol.dispose]();
  ++calls;
  await new Promise(resolve => setTimeout(resolve, 1)); // delay 1ms
}
Ngôn ngữ chính
TypeScript
Star
4k
Fork
151
Merge trung bình
16 giờ 46 phút
Pull request đã merge (30 ngày)
2

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

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 cloudflare/capnweb

Tất cả issue của cloudflare/capnweb

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.