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

Node warning after langchain upgrade to 0.2.13 Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. MaxListeners is 10. Use events.setMaxListeners() to increase limit #6461

Closed
5 tasks done
djaffer opened this issue Aug 8, 2024 · 6 comments · Fixed by #6497
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@djaffer
Copy link
Contributor

djaffer commented Aug 8, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

Getting error when running langchain

Error Message and Stack Trace (if applicable)

(node:62032) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. MaxListeners is 10. Use events.setMaxListeners() to increase limit
at [kNewListener] (node:internal/event_target:566:17)
at [kNewListener] (node:internal/abort_controller:240:24)
at EventTarget.addEventListener (node:internal/event_target:679:23)
at /home/langchainnode_modules/@langchain/core/dist/utils/signal.cjs:18:20
at new Promise ()
at raceWithSignal (/home/langchainnode_modules/@langchain/core/dist/utils/signal.cjs:17:9)
at RunnableMap._transform (/home/langchainnode_modules/@langchain/core/dist/runnables/base.cjs:1397:79)
at _transform.next ()
at RunnableMap._transformStreamWithConfig (/home/langchainnode_modules/@langchain/core/dist/runnables/base.cjs:307:30)
at _transformStreamWithConfig.next ()
at PromptTemplate.transform (/home/langchainnode_modules/@langchain/core/dist/runnables/base.cjs:390:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ChatOpenAI.transform (/home/langchainnode_modules/@langchain/core/dist/runnables/base.cjs:390:26)
at async wrapInputForTracing (/home/langchainnode_modules/@langchain/core/dist/runnables/base.cjs:274:30)
at async pipeGeneratorWithSetup (/home/langchainnode_modules/@langchain/core/dist/utils/stream.cjs:260:19)
at async StringOutputParser._transformStreamWithConfig (/home/langchainnode_modules/@langchain/core/dist/runnables/base.cjs:295:26)

Description

Getting the warning in node after upgrading langchain to latest versions.

"@langchain/community": "0.2.25",
"@langchain/openai": "0.2.6",
"langchain": "0.2.13",

System Info

"@langchain/community": "0.2.25",
"@langchain/openai": "0.2.6",
"langchain": "0.2.13",

Copy link

dosubot bot commented Aug 8, 2024

To resolve the "MaxListenersExceededWarning" in Node.js after upgrading LangChain to the latest versions, you can increase the maximum number of listeners for an event emitter. This can be done using the setMaxListeners method provided by the events module in Node.js.

Here is an example of how you can set this up:

const events = require('events');
events.EventEmitter.defaultMaxListeners = 20; // Set the maximum number of listeners to 20

// Alternatively, you can set it for a specific event emitter instance
const emitter = new events.EventEmitter();
emitter.setMaxListeners(20);

This code sets the default maximum number of listeners to 20 for all event emitters. You can adjust the number as needed based on your application's requirements [1].

Additionally, the raceWithSignal function in @langchain/core/dist/utils/signal.cjs manages event listeners and could potentially contribute to this warning. Ensuring proper management of event listeners and avoiding unnecessary accumulation can help mitigate this issue [2].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Aug 8, 2024
@jacoblee93
Copy link
Collaborator

Hey @djaffer, thanks for reporting this - could you share the code that triggers this?

@djaffer
Copy link
Contributor Author

djaffer commented Aug 10, 2024

(node:64750) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. MaxListeners is 10. Use events.setMaxListeners() to increase limit

ragChainWithSource = ragChainWithSource.assign({
  answer: ragChainFromDocs,
});

// Example chat history
const chatHistory = [
  { role: "human", content: "Hi" },
  { role: "ai", content: "Hello! How can I help you today?" },
];


 for await (const chunk of await ragChainFromDocs.stream(
          {
            question: "What is the capital of France?",
            chatHistory:chatHistory.map(msg => `${msg.role.toUpperCase()}: ${msg.content}`).join('\n'),
       
        )) {
console.log(chunk);         
}
}

@jacoblee93
Copy link
Collaborator

And then you pass a signal?

@djaffer
Copy link
Contributor Author

djaffer commented Aug 10, 2024

updated response. I am not passing any signal.

@jacoblee93
Copy link
Collaborator

Will go live in next core release, thanks for flagging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants