Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket EventEmitter memory leak #1

Open
rwilmar opened this issue Jan 7, 2025 · 0 comments
Open

Socket EventEmitter memory leak #1

rwilmar opened this issue Jan 7, 2025 · 0 comments

Comments

@rwilmar
Copy link

rwilmar commented Jan 7, 2025

Behavior:
After several iterations using the same connection node shows the warning:
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. XX data listeners added to [Socket]

Identified cause: socket.onData functions are not unsubscribed after returning promise in connect() and write() methods()
Proposed patch:

use a helper function to resolve the promise, using it to unsubscribe from event:
Original code:
`
public write(queries: string[]): Promise<{ [prop: string]: string }[] | []> {
return new Promise((resolve, reject) => {
this.writeWords(queries);

  const result: {
    type: string;
    messages: string[];
    data: any[];
  } = {
    type: "",
    messages: [],
    data: [],
  };

  let isDone = false;

  this.socket.on("data", (bufferData) => {
    useSentenceParser(bufferData, (line) => {
      if (line === "!re") {
        result.data.push({});
        result.type = "success";
      } else if (line === "!trap") {
        result.type = "error";
      } else if (isDone && line.startsWith("=ret=")) {
        const [, key, value] = line.split("=");
        resolve([{ [key]: value }]);
      } else if (line.startsWith("=") && result.type !== "error") {
        const [, key, value] = line.split("=");
        result.data[result.data.length - 1][key] = value;
      } else if (line.startsWith("=") && result.type === "error") {
        const [, key, value] = line.split("=");
        result.messages.push(value);
      } else if (line === "!done") {
        isDone = true;
      } else if (line === "" && isDone) {
        if (result.type === "success") {
          resolve(result.data);
        } else if (result.type === "") {
          resolve(result.data);
        } else {
          reject(new RouterosException(result.messages.join(". ")));
        }
      }
    });
  });
});

}
proposed change
public write(queries: string[]): Promise<{ [prop: string]: string }[] | []> {
return new Promise((resolve, reject) => {
this.writeWords(queries);

  const result: {
    type: string;
    messages: string[];
    data: any[];
  } = {
    type: "",
    messages: [],
    data: [],
  };

  let isDone = false;
  const promiseResolve = (data: any) => {
      this.socket.off("data", dataRecvFn);
      resolve(data);
  };
  const promiseReject = (error: any) => {
      this.socket.off("data", dataRecvFn);
      reject(error);
  };
  const dataRecvFn = (bufferData: Buffer) => {
    useSentenceParser(bufferData, (line) => {
      if (line === "!re") {
        result.data.push({});
        result.type = "success";
      } else if (line === "!trap") {
        result.type = "error";
      } else if (isDone && line.startsWith("=ret=")) {
        const [, key, value] = line.split("=");
        promiseResolve([{ [key]: value }]);
      } else if (line.startsWith("=") && result.type !== "error") {
        const [, key, value] = line.split("=");
        result.data[result.data.length - 1][key] = value;
      } else if (line.startsWith("=") && result.type === "error") {
        const [, key, value] = line.split("=");
        result.messages.push(value);
      } else if (line === "!done") {
        isDone = true;
      } else if (line === "" && isDone) {
        if (result.type === "success") {
          promiseResolve(result.data);
        } else if (result.type === "") {
          promiseResolve(result.data);
        } else {
          promiseReject(new RouterosException(result.messages.join(". ")));
        }
      }
    });
  }
  this.socket.on("data", dataRecvFn);
});

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant