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

Graphql: Subscribing to same event more than 10 times throws a warning #21570

Open
joselcvarela opened this issue Feb 26, 2024 · 4 comments · May be fixed by #21728
Open

Graphql: Subscribing to same event more than 10 times throws a warning #21570

joselcvarela opened this issue Feb 26, 2024 · 4 comments · May be fixed by #21728

Comments

@joselcvarela
Copy link
Member

joselcvarela commented Feb 26, 2024

Describe the Bug

While trying to subscribe to same event more than 10 times, Directus throws the following warning:

(node:99122) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit
    at __node_internal_genericNodeError (node:internal/errors:865:15)
    at _addListener (node:events:591:17)
    at EventEmitter.addListener (node:events:609:10)
    at on (node:events:1124:13)
    at Object.subscribe (/directus/api/src/services/graphql/subscription.ts:93:26)
    at subscribe.next (<anonymous>)
    at <anonymous> (/directus/api/src/services/graphql/subscription.ts:26:20)
    at AsyncGenerator.next (<anonymous>)
    at Object.next (/directus/node_modules/.pnpm/[email protected]/node_modules/graphql/execution/mapAsyncIterator.js:43:39)
    at onMessage (file:///directus/directus/node_modules/.pnpm/[email protected][email protected]/node_modules/graphql-ws/lib/server.mjs:207:180)

To Reproduce

In order to replicate this issue you need to:

  • set WEBSOCKETS_ENABLED: 'true'
  • create collection example with string fields id and name
  • start listening with the following code and it will throw a warning on Directus
Code
import { createClient } from "graphql-ws";
import WebSocket from "ws";

async function start() {
  try {
    const client = createClient({
      url: "ws://localhost:8055/graphql",
      keepAlive: 30000,
      webSocketImpl: WebSocket,
      connectionParams: async () => {
        return { access_token: "123456789" };
      },
    });

    const subscription = client.iterate({
      query: "subscription { example_mutated { key event data { id name } } }",
    });

    for await (const event of subscription) {
      console.log(event);
    }
  } catch (error) {
    console.log(error);
  }
}

(async function () {
  const items = Array(15).fill(0);

  for (let i of items) {
    start();
    await new Promise((res) => setTimeout(res, 1000));
  }
})();

In docs says that WEBSOCKETS_GRAPHQL_CONN_LIMIT is set to Infinity: https://docs.directus.io/self-hosted/config-options.html#graphql
Although this configuration seems to not be used anywhere.

It is being set properly as mentioned by @br41nslug here: #21570 (comment)

Suggestion

Maybe this could be as simple as setting this:

const emitter = new EventEmitter();
emitter.setMaxListeners(Number(env['WEBSOCKETS_GRAPHQL_CONN_LIMIT']));
const messages = createPubSub(emitter);

In the following line?

const messages = createPubSub(new EventEmitter());

Directus Version

main (10.9.3)

Hosting Strategy

Self-Hosted (Custom)

@paescuj
Copy link
Member

paescuj commented Feb 26, 2024

Important to note: This is a warning only, nothing is actually blocked!

@joselcvarela

This comment was marked as resolved.

@br41nslug

This comment was marked as resolved.

@joselcvarela

This comment was marked as resolved.

@br41nslug br41nslug linked a pull request Mar 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

Successfully merging a pull request may close this issue.

3 participants