cartesia-ai / cartesia-js

The JavaScript client for the Cartesia API.
MIT License
54 stars 10 forks source link

Memory leak related to callbacks #13

Closed soylomass closed 4 hours ago

soylomass commented 1 week ago

I noticed that there are callbacks created in the send method of your websockets implementations that are never cleared, including:

this.socket?.addEventListener("message", handleMessage, {
    signal: streamCompleteController.signal,
});
this.socket?.addEventListener(
    "close",
    () => {
        streamCompleteController.abort();
    },
    {
        once: true,
    },
);
this.socket?.addEventListener(
    "error",
    () => {
        streamCompleteController.abort();
    },
    {
        once: true,
    },
);

I guess the message callback is cleared when the signal is called, but the other too will exist until they are called once, which may never happen.

Node.js throws the following error to notify about this:

(node:44746) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 close listeners added to _ReconnectingWebSocket. Use events.setMaxListeners() to increase limit

Also if getEmitteryCallbacks creates new callbacks, they are not cleared anywhere either.

kbrgl commented 4 hours ago

Closed by #17.