I have a very simple dummy Node.js + GraphQL + Apollo project that you can download from here, or test on StackBlitz, or just inspect the code at the end of this post.
My problem is: When testing a subscription multiple times (2 different processes involved at a time, one for the server and another one for the tester) and that subscription doesn't yield a value, then the event listener for that subscription remains active even if the process that performed the test finishes. Then I get the error:
(node:20064) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 CHANNEL_MESSAGE listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
You can test it with:
Terminal 1: $ yarn; yarn dev
Terminal 2: $ clear; for i in {1..11}; do echo -e "\n\033[33m### Iteration: $i\033[0m"; node src/test.js; done
Then you will get the following:
However, if the subscription always yield a value (in the example above making: const IGNORE_MESSAGE_EVENTS = false), then we don't get that MaxListenersExceededWarning.
What I need: If the test process finishes gracefully/abruptly/unexpectedly the event listener should be removed automatically from the server so there are no unused resources around.
On the tester I even have the instructions below in the finally (try-catch-finally) but didn't work:
if (subscription) {
subscription.unsubscribe();
}
if (client1) {
client1.stop();
client1 = null;
}
if (client2) {
client2.stop();
client2 = null;
}
however I think the server should not necessary expect that the tester-client finishes the connection properly but just remove listeners that were originated by a connection that doesn't exist anymore, on this case the one for the tester process.
I have a very simple dummy
Node.js + GraphQL + Apollo
project that you can download from here, or test on StackBlitz, or just inspect the code at the end of this post.My problem is: When testing a subscription multiple times (2 different processes involved at a time, one for the server and another one for the tester) and that subscription doesn't yield a value, then the event listener for that subscription remains active even if the process that performed the test finishes. Then I get the error:
You can test it with:
Then you will get the following:
However, if the subscription always yield a value (in the example above making:
const IGNORE_MESSAGE_EVENTS = false
), then we don't get thatMaxListenersExceededWarning
.What I need: If the test process finishes gracefully/abruptly/unexpectedly the event listener should be removed automatically from the server so there are no unused resources around.
On the tester I even have the instructions below in the finally (try-catch-finally) but didn't work:
however I think the server should not necessary expect that the tester-client finishes the connection properly but just remove listeners that were originated by a connection that doesn't exist anymore, on this case the one for the tester process.
file:
/.env
file:
/package.json
file:
/src/index.js
file:
/src/test.js