peterszombati / xapi-node

xStation5 Trading API for NodeJS/JS
https://peterszombati.github.io/xapi-node/
Other
58 stars 19 forks source link

Unable to destroy instance when login fails #12

Closed black-hawk85 closed 3 years ago

black-hawk85 commented 3 years ago

Sorry to bother again but I'm having trouble with accounts which fail to login (e.g. expired demo accounts or when entered wrong password).

They try to reconnect all the time even when I call disconnect method. On the contrary, when I call disconnect method after failed login I keep getting uncaught exception 'Not logged - logout' (BE103)

I have a doubt that this may lead to memory leaks.

Is there a way to disable tryReconnect or stop reconnecting and close the socket after calling disconnect even when authentication was unsuccessful?

peterszombati commented 3 years ago

@black-hawk85 you can catch login fails with x.onReject callback and then you can disconnect with x.disconnect

black-hawk85 commented 3 years ago

Ya, that's exactly what I tried. That is where the error 'Not logged - logout' (BE103) comes in and spams my error log. Strange thing is that the disconnect's promise won't get resolved if I try to await it.

black-hawk85 commented 3 years ago

Here's some test code

Catching and printing unhandled exceptions

process.on('unhandledRejection', (reason, p) => {
  if (reason.reason && reason.reason.code && reason.reason.explain) {
    console.log("unhandledRejection: " + reason.reason.explain + "(" + reason.reason.code + ")")
  }
});

Connecting with wrong login data

options.password = "1234"

this.connector = new XAPI(options);
this.connector.connect();  // if login data is incorrect we'll get keep getting unhandledRejection which we cannot catch
this.connector.onReject(async err => {
  await this.connector.disconnect();
  console.log(e + " => disconnected")  // this line is never reached
})

This is my output

unhandledRejection: Not logged - logout(BE103)
unhandledRejection: Socket closed(XAPINODE_1)
unhandledRejection: Not logged - logout(BE103)
unhandledRejection: Socket closed(XAPINODE_1)
unhandledRejection: Not logged - logout(BE103)
unhandledRejection: Socket closed(XAPINODE_1)
unhandledRejection: Not logged - logout(BE103)
unhandledRejection: Socket closed(XAPINODE_1)
....
peterszombati commented 3 years ago

@black-hawk85 I pushed a new version can you try it ?

black-hawk85 commented 3 years ago

Yesss, this looks good now ;) thanks for the quick fix! Will test a little more in detail when I have more time but I think this fixes the issue

peterszombati commented 3 years ago

@black-hawk85 problem not solved :D I found another stuff related to this :D

peterszombati commented 3 years ago

okay I solved it, if you find another bug just create an issue.