question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I auto reconnect on disconnect?

See original GitHub issue

I’m trying to call ib_insync.Client.connect(…) in a loop with a delay in wrapper.apiEnd and got an exception

RuntimeWarning: coroutine ‘Client.connectAsync’ was never awaited`

Is this the correct way to reconnect? Or do I need to del my client instance and create a new one?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nurettincommented, Oct 28, 2022

Sorry for commenting on a closed issue, but I wanted to share a solution.

If peer closes connection (TWS being shutdown), you can catch this using the disconnectedEvent. Then you will need to break the run loop. Instead of dealing with async, I use a simple SIGINT signal for this. The program also needs to know whether if we are restarting due to a disconnect issue, or the user just wants to stop the program. This is why I set a class variable called reconnecting to True if our SIGINT’s intention is to reconnect. This way, we know if we want to stop retrying and exit to program.

If TWS loses connection to internet temporarily, then reconnects to market data (errorCode = 1102), we need to handle this inside errorEvent. I just set reconnecting to True, call disconnect() and raise SIGINT. If you dont’ disconnect, reconnect will not succeed due to clientId = 1 still connected.

If we lose connection to TWS due to API being disabled (errorCode = 1300) we need to set reconnecting to True, call disconnect() (might not be needed but it is idempotent) and raise SIGINT.

After doing all of this, there is still a problem. If I login from another computer, then logout, we still need to manually click the “Reconnect this session” button of the modal dialog that appears in the TWS application.

1reaction
erdewitcommented, Jan 30, 2018

The assert is only hit in the case of nested event loops. To allow nested event loops, use util.patchAsyncio().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Turning Auto Reconnect On or Off on Mobile Devices - Webroot
Tap Auto Reconnect. In the Auto Reconnect area, do either of the following: Tap the slider to the left to turn Auto Reconnect...
Read more >
Is there a way to auto-disconnect and reconnect WiFi ... - Quora
However, providing devices are set to auto-connect, simply reboot the router and devices will be disconnected and can reconnect automatically as the router ......
Read more >
How to get computer to reconnect automatically after ...
Make your connection as your default internet connection. Open Network Connection in Control Panel. Right click your Internet connection and ...
Read more >
Auto-disconnect and auto-reconnect
The auto-reconnect process ensures that any connections that were auto-disconnected are automatically reconnected. Then, if configured, Double-Take Availability ...
Read more >
Enabling Server Auto-reconnect - Micro Focus
This opens the Configure Server dialog box. Select the General tab. Check Reconnect timeout. Type the number of minutes in the text box...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found