PassTheMayo / minecraft-server-util

A Node.js library for Minecraft servers that can retrieve status, query, RCON, and send Votifier votes.
https://passthemayo.gitbook.io/minecraft-server-util/
MIT License
140 stars 24 forks source link

bug: "MaxListenersExceededWarning: Possible EventEmitter memory leak detected." when pinging in batches of 10. #66

Closed mve closed 2 years ago

mve commented 2 years ago

Describe the bug: I'm pinging a list of around 100 servers, batching them up in groups of 10 and waiting with Promise.allSettled for them to resolve. I get the following error:

(node:18269) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [TCPClient]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18269) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TCPClient]. Use emitter.setMaxListeners() to increase limit

Running with --trace-warnings i see the following:

(node:18393) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [TCPClient]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:465:17)
    at TCPClient.addListener (node:events:487:10)
    at /Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:449:18
    at new Promise (<anonymous>)
    at TCPClient._waitForData (/Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:438:16)
    at TCPClient.<anonymous> (/Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:77:28)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
(node:18393) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TCPClient]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:465:17)
    at TCPClient.addListener (node:events:487:10)
    at /Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:450:18
    at new Promise (<anonymous>)
    at TCPClient._waitForData (/Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:438:16)
    at TCPClient.<anonymous> (/Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:77:28)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/mve/code/mc-pinger-serverless/node_modules/minecraft-server-util/dist/structure/TCPClient.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I feel like increasing the maxlisteners shouldn't be needed as i'm waiting in batches of 10, which should mean that i'm never waiting on more than 10 servers, right?

Code: I set up this code example that you can use to recreate the issue. You need a list of many servers, you need to add them to the servers array in src/index.js.

package.json

{
  "name": "server-util-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "minecraft-server-util": "^5.2.6"
  }
}

src/index.js

import { status } from "minecraft-server-util";

const servers = [
  {
    "host": "play.minehalla.com",
    "port": 25565,
  },
// Large list of servers.
]

const promiseAllSettledInBatches = async (task, items, batchSize) => {
  let position = 0;
  let results = [];

  console.log("Starting on batches...");

  while (position < items.length) {

    console.log(`Position ${position} of ${items.length}.`);

    const itemsForBatch = items.slice(position, position + batchSize);

    results = [...results, ...await Promise.allSettled(
      task(itemsForBatch)
    )];

    position += batchSize;
  }

  console.log("All batches are done.");

  return results;
}

const getServersByHost = (hosts) => {
  const results = [];
  for (const host of hosts) {

    results.push(status(host.host, host.port));

  }
  return results;
}

const result = promiseAllSettledInBatches(getServersByHost, servers, 10);

Run the code node src/index.js

Run this for more info on the error. node --trace-warnings src/index.js

If you don't get the error on the first run, quickly run the same code again.

You can use this set of servers to test with:


{
            "host": "play.wildprison.net",
            "port": 25565
        },
        {
            "host": "mc.minecraftexperience.it",
            "port": 25565
        },
        {
            "host": "mp.brutalprison.com",
            "port": 25565
        },
        {
            "host": "mcprison.com",
            "port": 25565
        },
        {
            "host": "mp.jartex.fun",
            "port": 25565
        },
        {
            "host": "play.vipermc.net",
            "port": 25565
        },
        {
            "host": "grmpixelmon.com",
            "port": 25565
        },
        {
            "host": "mp.archonhq.net",
            "port": 25565
        },
        {
            "host": "play.minelatino.com",
            "port": 25565
        },
        {
            "host": "play.minemalia.com",
            "port": 25565
        },
        {
            "host": "ms.pika-network.net",
            "port": 25565
        },
        {
            "host": "mc.gamersreact.net",
            "port": 25565
        },
        {
            "host": "play.chocohills.com",
            "port": 25565
        },
        {
            "host": "play.foxmc.net",
            "port": 25565
        },
        {
            "host": "join.mineheroes.org",
            "port": 25565
        },
        {
            "host": "play.nls.uhst.eu",
            "port": 25565
        },
        {
            "host": "HylexMC.net",
            "port": 25565
        },
        {
            "host": "org.mcglobe.net",
            "port": 25565
        },
        {
            "host": "mc.megaplanet.net",
            "port": 25565
        },
        {
            "host": "play.noblerealms.com",
            "port": 25565
        },
        {
            "host": "sentouki.net",
            "port": 25565
        },
        {
            "host": "play.guildcraft.net",
            "port": 25565
        },
        {
            "host": "mc.centranetwork.net",
            "port": 25565
        },
        {
            "host": "mc.ventureland.net",
            "port": 25565
        },
        {
            "host": "join.insanitycraft.net",
            "port": 25565
        },
        {
            "host": "mc.akarcraft.es",
            "port": 25565
        },
        {
            "host": "jogarbrasilcraft.com",
            "port": 25565
        },
        {
            "host": "play.pokefind.co",
            "port": 25565
        },
        {
            "host": "play.z-mc.net",
            "port": 25565
        },
        {
            "host": "play.hoaxmc.com",
            "port": 25565
        },
        {
            "host": "play.therepublic.io",
            "port": 25565
        },
        {
            "host": "play.mc-ages.com",
            "port": 25565
        },
        {
            "host": "play.hycraft.us",
            "port": 25565
        },
        {
            "host": "server.mcs.gg",
            "port": 25565
        },
        {
            "host": "play.chasecraft.gg",
            "port": 25565
        },
        {
            "host": "play.cultivatemc.com",
            "port": 25565
        },
        {
            "host": "org.AspiriaMc.com",
            "port": 25565
        },
        {
            "host": "play.spacedelta.net",
            "port": 25565
        },
        {
            "host": "legionmc.club",
            "port": 25565
        },
        {
            "host": "play.almightys.in",
            "port": 25565
        },
        {
            "host": "arefy.net",
            "port": 25565
        },
        {
            "host": "blotcraft.com",
            "port": 25565
        },
        {
            "host": "play.thepondmc.com",
            "port": 25565
        },
        {
            "host": "mythosrealms.minecraft.best",
            "port": 25565
        },
        {
            "host": "mc.ancestrycraft.es",
            "port": 25565
        },
        {
            "host": "play.vectorcraftid.com",
            "port": 25565
        },
        {
            "host": "mc.craftmc.pro",
            "port": 25565
        },
        {
            "host": "skyblock.net",
            "port": 25565
        },
        {
            "host": "shinybounds.net",
            "port": 25565
        },
        {
            "host": "mc.pombacraft.xyz",
            "port": 25565
        },
        {
            "host": "kchscraft.sparked.network",
            "port": 25565
        },
        {
            "host": "play.wildercraft.net",
            "port": 25565
        },
        {
            "host": "play.pokewild.com",
            "port": 25565
        },
        {
            "host": "uismine.vn",
            "port": 25565
        },
        {
            "host": "server.proyecto40.es",
            "port": 25565
        },
        {
            "host": "claritynetwork.id",
            "port": 25565
        },
        {
            "host": "org.mc-blaze.com",
            "port": 25565
        },
        {
            "host": "play.topstrix.net",
            "port": 25565
        },
        {
            "host": "mc.mineverso.com",
            "port": 25565
        },
        {
            "host": "mc.keinett.us",
            "port": 25565
        },
        {
            "host": "play.ImmortalRealms.co",
            "port": 25565
        },
        {
            "host": "play.snapcraft.net",
            "port": 25565
        },
        {
            "host": "mc.voxcraft.us",
            "port": 25565
        },
        {
            "host": "mc-wal.net",
            "port": 25565
        },
        {
            "host": "mc.pandacraft.cc",
            "port": 25565
        },
        {
            "host": "play.ham5teak.net.au",
            "port": 25565
        },
        {
            "host": "minerefuge.com",
            "port": 25565
        },
        {
            "host": "play.luckysb.org",
            "port": 25565
        },
        {
            "host": "play.breakdowncraft.com",
            "port": 25565
        },
        {
            "host": "hub.primemc.org",
            "port": 25565
        },
        {
            "host": "volatik.net",
            "port": 25565
        },
        {
            "host": "play.dipcraft.ro",
            "port": 25565
        },
        {
            "host": "mc.vortexnetwork.net",
            "port": 25565
        },
        {
            "host": "play.ecc.eco",
            "port": 25565
        },
        {
            "host": "play.sgcraft.net",
            "port": 25565
        },
        {
            "host": "Join.Performium.co",
            "port": 25565
        },
        {
            "host": "pvpwars.net",
            "port": 25565
        },
        {
            "host": "sf4.craftersland.net",
            "port": 25565
        },
        {
            "host": "play.gemprison.com",
            "port": 25565
        },
        {
            "host": "play.foxcraft.net",
            "port": 25565
        },
        {
            "host": "play.battleasya.com",
            "port": 25565
        },
        {
            "host": "woodymc.de",
            "port": 25565
        },
        {
            "host": "mc.saucerlandia.net",
            "port": 25565
        },
        {
            "host": "play.minehalla.com",
            "port": 25565
        },
        {
            "host": "mc.skyblocknetwork.com",
            "port": 25565
        },
        {
            "host": "mc.lotc.co",
            "port": 25565
        },
        {
            "host": "hub.ody.gg",
            "port": 25565
        },
        {
            "host": "VortexNetwork.me",
            "port": 25565
        },
        {
            "host": "mc.divictusgaming.com",
            "port": 25565
        }

Expected behavior: Not show this warning.

Additional context Using version 5.2.6 of minecraft-server-util. Using Node version 17.3.0.

PassTheMayo commented 2 years ago

Unfortunately I haven't been able to reproduce this warning message through your extensively detailed code, but I have gone ahead and fixed the code to reduce the amount of listeners that it uses in version 5.2.7. Please try updating and verify that this issue has been fixed on your end.

mve commented 2 years ago

Just did some quick testing and it seems to be working for me. I'm not getting the error anymore.

PassTheMayo commented 2 years ago

Good news, I'll go ahead and close this issue.