Skip to content

disconnectedEvent fires twice per disconnect #207

@msdavid

Description

@msdavid

Bug: disconnectedEvent fires twice per disconnect

Version

ib_async 2.1.0

Description

Every disconnect causes disconnectedEvent to fire twice because it is wired from two independent sources:

  1. IB.__init__ connects client.apiEnd to disconnectedEvent (so when the client layer detects disconnect, the event fires)
  2. IB.disconnect() explicitly calls self.disconnectedEvent.emit() after calling self.client.disconnect()

When IB.disconnect() is called, the client disconnect triggers apiEnd (which fires disconnectedEvent via the wiring in #1), and then IB.disconnect() fires it again directly (#2).

Impact

Any handler registered on disconnectedEvent executes twice per disconnect. This can cause issues with cleanup logic, reconnection state machines, and logging that assumes a 1:1 relationship between disconnects and events.

Steps to reproduce

from ib_async import IB

ib = IB()
count = 0

def on_disconnect():
    global count
    count += 1
    print(f"disconnectedEvent fired (count={count})")

ib.disconnectedEvent += on_disconnect
await ib.connectAsync('127.0.0.1', 4002, clientId=1)
ib.disconnect()
print(f"Total fires: {count}")  # Prints 2, expected 1

Expected behavior

disconnectedEvent should fire exactly once per disconnect, regardless of whether the disconnect was initiated by IB.disconnect() or by a connection drop.

Suggested fix

Either remove the explicit self.disconnectedEvent.emit() from IB.disconnect() (relying on the apiEnd wiring), or guard the explicit emit with a flag to prevent double-firing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions