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

Re-connect after TWS restart not working properly #195

Closed
mafianekcek opened this issue Oct 26, 2023 · 5 comments · Fixed by #192
Closed

Re-connect after TWS restart not working properly #195

mafianekcek opened this issue Oct 26, 2023 · 5 comments · Fixed by #192

Comments

@mafianekcek
Copy link
Contributor

mafianekcek commented Oct 26, 2023

Hi, I found a very strange thing in re-connection. I am using interval for re-connections, however when I restart TWS (to simulate auto-restart) sometimes it hit a very strange point (bad timing) and the whole connections starts messing up and I am unable to reconnect until I restart the script. It creates bugged connection in TWS which is somehow disconnected (but not disconnected) exactly in one minute after this error message appears "Decoding error on undefined: unprocessed data left on queue..." and then the infinite loop of re-connection begin because in TWS it is already connected on the same port so it is unable to connect.
By the way, I also tried different time for the interval, like 10 seconds or one minute, it happens less often because its harder to hit the point, but it still happens.

Please check attached video please, it explains everything. I needed to upload it external as I am unable to upload more than 10mb file so here is the link: https://mega.nz/file/koMBUZSL#F1xQ09isob8gSLckJhH0WUzFX-sqyUz3yVw_XDeAP1E (the first TWS restart on the video was fine, the second time what I described above happened)

Also here you can find whole output of everything you can see in the video:
debug.log

Here is my test code:

const { IBApi, EventName } = require("@stoqey/ib")
const fs = require("fs")
const log_file = fs.createWriteStream("debug.log", {flags : 'a'})

function writeLog(...args) {
    let text = ""
    if (args.length > 1) {
        args.forEach(arg => {
            text += arg+" "
        })
    } else if (args[0] != undefined) {
        text = args[0].toString()
    }

    if (text == "")
        return

    let dat = new Date()
    let ms = dat.getMilliseconds().toString().padStart(3, '0')
    dat = `${dat.toLocaleDateString()} ${dat.toLocaleTimeString()}.${ms}`
    const finalText = `${dat}\t${text}`
    log_file.write(finalText+"\n")
    console.log(finalText)
}

const ib = new IBApi({
    clientId: 101,
    host: "127.0.0.1",
    port: 7497,
})

ib.on(EventName.all, (eName, args) => {
    writeLog(eName, JSON.stringify(args))
})
ib.on(EventName.info, (msg, code) => {
    writeLog("[info]", code, msg)
})
ib.on(EventName.error, (err, code, reqId) => {
    writeLog("[error]", reqId, code, err)
})
ib.connect()

setInterval(() => {
    if (ib.isConnected == false) {
        writeLog("Disconnected, restarting...")
        ib.connect()
    }
}, 1000)

Thanks for checking it out. Have a nice day.

@rylorin
Copy link
Member

rylorin commented Oct 26, 2023

Thanks for reporting with much details.

@rylorin rylorin linked a pull request Oct 26, 2023 that will close this issue
@rylorin
Copy link
Member

rylorin commented Oct 26, 2023

Your use case is passing with latest changes.
Could you please check and let us know?

@mafianekcek
Copy link
Contributor Author

Hello, I tried multiple TWS restarts even with 1ms interval and the problem seems to be fixed now, it does not return decoding error anymore and also does not stack connections. Great job!

@mafianekcek
Copy link
Contributor Author

mafianekcek commented Oct 26, 2023

@rylorin Hello again, by this change a new "minibug" appeared. The API trigger EventName.disconnected always when I send ib.connect() and API is not connected. So in my case when I am restarting TWS or I turn it off, my reconnect interval calls ib.connect() every 10 seconds and when TWS is off it trigger EventName.disconnected every 10 seconds as well. However it is not bug at all, I just wanted to let you know.
I solved it by this in my code

	let ibApiConnected  = false
	ib.on(EventName.connected, () => {
		ibApiConnected = true
		console.log("IB API connected")
	})
	
	ib.on(EventName.disconnected, () => {
		if (ibApiConnected) {
			ibApiConnected = false
			console.log("IB API disconnected")
		}
	})

@rylorin
Copy link
Member

rylorin commented Oct 26, 2023

Thanks for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants