mineflayer icon indicating copy to clipboard operation
mineflayer copied to clipboard

Multiple bot doesnt works - only 1 bot gets connected.

Open OlympicAngel opened this issue 2 years ago • 5 comments

  • [ ] The FAQ doesn't contain a resolution to my issue

Versions

  • mineflayer: 4.4.0
  • server: paper (1.18.2)
  • node: 18.7.0

Detailed description of a problem

Cant create mutiple bots, the first bots goes correctly the others doesnt event connect and only emmits "error" event with the error "socketClosed".

What did you try yet?

my code is simple , same way of creating the bots(aside from names).. the server is on offline mode as my main goal is stress-test it on large scale. The only way i mannaged to get in 2 bots to the same server is by running the same JS file seperatly in parallel(running 2 Node.js scripts), but this isnt ideal as i want to run 100+ of thos..

Did you try any method from the API? Did you try any example? Any error from those? copy pasted https://github.com/PrismarineJS/mineflayer/blob/master/examples/multiple.js, and it has the same behavior, only the first bot gets logged into the server.

Mycurrent code

const mineflayer = require('mineflayer')
const globalArgs = {
    host: '*****', //my ip goes here - i removed it as it is a senesitive ip i dont want to expose
    port: 25576,   
    logErrors: true,
    viewDistance: "short"
};
const bots = [];

(async () => {
    for (let i = 0; i <= 2; i++) {
        bots.push(new MCBOT(i + ~~(Math.random() * 1000)));
        await sleep(1);
    }
})()

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms * 1000));
}

class MCBOT {
    constructor(index) {
        const botArgs = Object.assign({}, globalArgs);
        botArgs.username = "BOT" + index;
        this.bot = mineflayer.createBot(botArgs)
        this.username = botArgs.username;

        this.EventsINI();
    }

    EventsINI() {
        this.bot.once('spawn', (e) => {
            this.log("spawn event")
        })

        this.bot.on("error", (err) => {
            if (err.code == 'ECONNREFUSED') {
                this.log(`Failed to connect to ${err.address}:${err.port}`)
            }
            else {
                this.log(` Unhandled error: ${err}`);
            }
        })

        this.bot.on("end", (e) => {
            this.log("Connection Ended - " + e)
        })

        this.bot.on("login", () => {
            this.log(`Logged into ${globalArgs.host}`);
        })
    }

    log(str) {
        console.log("[" + this.username + "] " + str);
    }
}

(even the example listed above didnt worked for me & has the same behavior)

Expected behavior

Mutilpe bots to login to a simpe paper server

OlympicAngel avatar Aug 20 '22 22:08 OlympicAngel

~~You're only waiting 1ms between each bot, maybe try increasing that?~~

amoraschi avatar Aug 20 '22 22:08 amoraschi

You're only waiting 1ms between each bot, maybe try increasing that?

My sleep func is seconds based and not ms, so its a wait of 1000ms And even with a wait of 10 seconds each - the same behavior.

OlympicAngel avatar Aug 20 '22 22:08 OlympicAngel

You're only waiting 1ms between each bot, maybe try increasing that?

My sleep func is seconds based and not ms, so its a wait of 1000ms And even with a wait of 10 seconds each - the same behavior.

After some weird messing arounds, i mannged to get it to work, Its still seems like a bug tho, With a wait of atleast 15 seconds it doest work, if its less it doesnt work, but at 15+ it is, its weird behavior and doesnt make sense to me, im not closeing this issue as i do want a way to not wait 15 sec each..

OlympicAngel avatar Aug 20 '22 22:08 OlympicAngel

Does this happen on a vanilla server ?

extremeheat avatar Aug 21 '22 00:08 extremeheat

With a wait of atleast 15 seconds it doest work, if its less it doesnt work

You might be having Connection throttled! Please wait before reconnecting. error. ~~This is the expected vanilla behaviour afaik.~~ Try adding bot.on('kicked', console.log) to your code and see what it prints Edit: this is most likely something that paper/spigot/whatever does.

Kistras avatar Aug 24 '22 13:08 Kistras

So I experimented with this a little bit. It seems to stem from how Node manages its stack and whatnot. For whatever reason, mineflayer in its current state combined with Node as it is, isn't capable of doing more than 30-50 connections at a time. I tested this on 1.18.2 a couple months back with a Ryzen 9 3900X in a server configuration. Even at the local loopback address, mineflayer could only keep those connections open for so long before either the connection dies due to a missed heartbeat or the process becomes blocked due to the synchronous nature of Node.

You're going to need to implement some sort of multi-threading to partially resolve this issue.

kashalls avatar Oct 31 '22 16:10 kashalls

Echoing my earlier comment -- Does this happen on a vanilla server ?

If no then this seems not like a mineflayer problem and more like a server software-side throttle

extremeheat avatar Nov 01 '22 21:11 extremeheat

Echoing my earlier comment -- Does this happen on a vanilla server ?

If no then this seems not like a mineflayer problem and more like a server software-side throttle

I've seen it happen even on vanilla. I can set something up here in a minute.

kashalls avatar Nov 01 '22 21:11 kashalls

yea my bad for now answering.. yea it happends on purpor server - havent tested it on others, and i basicly droped the use of mineflayer case of it been using other staff so yea..

OlympicAngel avatar Nov 21 '22 22:11 OlympicAngel