Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

LogSubscription warning MaxListenersExceededWarning: Possible EventTarget memory leak detected. #3624

Closed
codemedian opened this issue Nov 25, 2024 · 6 comments · Fixed by #3661
Assignees
Labels
bug Something isn't working

Comments

@codemedian
Copy link

codemedian commented Nov 25, 2024

Overview

Hi all, I've started playing with the 2.x version of the SDK and pretty happy with it overall. I've now wanted to add 3 logsListeners with a filter of my address for the different commitment levels and while it works fine for 2 logs subscriptions, once I add a 3rd node events gets unhappy with

When I reduce the maxSubscriptionsPerChannel config in the createSolanaRpcSubscriptions to say 2, the warning goes away fyi. The issue varies and sometimes complains about AbortSignal and sometimes about EventTarget

Steps to reproduce

const abortController = new AbortController();
const solanaRpcSubscriptions = createSolanaRpcSubscriptions(...);

const sub1 = solanaRpcSubscriptions.logsNotifications(
        { mentions: ["someAddress"] },
        { commitment: "processed" }).subscribe({abortSignal: abortController.signal}).then(...);
const sub2 = solanaRpcSubscriptions.logsNotifications(
        { mentions: ["someAddress"] },
        { commitment: "confirmed" }).subscribe({abortSignal: abortController.signal}).then(...);
const sub3 = solanaRpcSubscriptions.logsNotifications(
        { mentions: ["someAddress"] },
        { commitment: "finalized" }).subscribe({abortSignal: abortController.signal}).then(...);

Description of bug

I did not expect to see this warning and more so would like for a way to set the limits such that it does not warn

EventTarget warning:

(node:54008) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 error listeners added to EventTarget. MaxListeners is 10. Use events.setMaxListeners() to increase limit
    at [kNewListener] (node:internal/event_target:566:17)
    at EventTarget.addEventListener (node:internal/event_target:679:23)
    at Object.on (/Users/chris/Projects/spot/node_modules/@solana/subscribable/src/data-publisher.ts:28:26)
    at Object.on (/Users/chris/Projects/spot/node_modules/@solana/rpc-subscriptions-spec/src/rpc-subscriptions-channel.ts:38:32)
    at Object.on (/Users/chris/Projects/spot/node_modules/@solana/rpc-subscriptions-spec/src/rpc-subscriptions-pubsub-plan.ts:207:36)
    at Object.createAsyncIterableFromDataPublisher (/Users/chris/Projects/spot/node_modules/@solana/subscribable/src/async-iterable.ts:87:19)
    at Object.subscribe (/Users/chris/Projects/spot/node_modules/@solana/rpc-subscriptions-spec/src/rpc-subscriptions.ts:72:20)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

AbortSignal warning:

(node:54231) 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 Object.createAsyncIterableFromDataPublisher (/Users/chris/Projects/spot/node_modules/@solana/subscribable/src/async-iterable.ts:81:17)
    at Object.subscribe (/Users/chris/Projects/spot/node_modules/@solana/rpc-subscriptions-spec/src/rpc-subscriptions.ts:72:20)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
@codemedian codemedian added the bug Something isn't working label Nov 25, 2024
@steveluscher
Copy link
Contributor

I just learned that the setMaxEventListeners API in Node can target one particular EventTarget instance. We can make a shim that does this automatically, but just for the event targets that we create inside the subscriptions implementation.

@steveluscher steveluscher self-assigned this Dec 2, 2024
@steveluscher
Copy link
Contributor

I guess we'll have to operate over the AbortSignals that get passed in too. Sheesh.

Note to future self:

  1. Create disableMaxListenersWarning shim (private npm module similar to crypto-impl)
  2. Make it a noop for browser and react-native builds
  3. Make it do et => { setMaxListeners(Number.MAX_SAFE_INTEGER, et); } for node

@steveluscher
Copy link
Contributor

Hey @codemedian, do you have a repro for me that triggers the AbortSignal warning? I'd like to test it against #3661.

@codemedian
Copy link
Author

Unfortunately I don't, no and I seem to have lost my POC branch that I was messing around with.
We've dropped the integration based on websocket/webhooks for now and went to polling for statuses as the listeners against our quicknode RPC node kept missing updates from chain which caused us problems, polling is a bit slower but it's working consistently at least.

When/if we get back to trying websockets I'm happy to do some more testing around this, though no plans to do it in the near future.

steveluscher added a commit that referenced this issue Dec 4, 2024
… targets for internal use (#3661)

# Summary

Read all about why, here: https://solana.stackexchange.com/a/17971/75

Fixes #3624.

# Test Plan

```ts
const {createSolanaRpcSubscriptions} = require('./dist/index.node.cjs');

const abortController = new AbortController();
const solanaRpcSubscriptions = createSolanaRpcSubscriptions('ws://localhost:8900');

const sub1 = solanaRpcSubscriptions.logsNotifications(
        { mentions: ['1'.repeat(32)] },
        { commitment: "processed" }).subscribe({abortSignal: abortController.signal}).then(() => {});
const sub2 = solanaRpcSubscriptions.logsNotifications(
        { mentions: ['1'.repeat(32)] },
        { commitment: "confirmed" }).subscribe({abortSignal: abortController.signal}).then(() => {});
const sub3 = solanaRpcSubscriptions.logsNotifications(
        { mentions: ['1'.repeat(32)] },
        { commitment: "finalized" }).subscribe({abortSignal: abortController.signal}).then(() => {});
```

No more warning.
Copy link
Contributor

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
2 participants