Skip to content

EventHub: Port websocket robustness fixes from ayon-core - #386

Open
iLLiCiTiT wants to merge 3 commits into
developfrom
bugfix/event-hub-port-core-fixes
Open

iLLiCiTiT wants to merge 3 commits into
developfrom
bugfix/event-hub-port-core-fixes

Conversation

@iLLiCiTiT

@iLLiCiTiT iLLiCiTiT commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Changelog Description

Port websocket fixes from the ayon-core event hub (ayon_core/tools/common_models/websocket.py) to EventHub, so both implementations behave the same.

New connection.interrupted event is emitted when a working connection drops and is reconnected silently. Anything that caches server data and resyncs on connection.closed should handle connection.interrupted the same way.

Additional info

  • Silent reconnect: when a working connection is closed (client did not answer server pings because the host held the GIL, proxy, network hiccup), the hub emits new connection.interrupted (EventHub.TOPIC_CONNECTION_INTERRUPTED) and reconnects. connection.opened is emitted once reconnected, connection.closed only if reconnecting fails. Server restart and logout (token removed) are still reported as closed right away.
  • Receive timeouts are counted instead of measured with time.monotonic(). The thread might not run for a long time while messages are waiting in the socket, so wall-clock time made a stalled-but-alive connection look dead.
  • SIGPIPE is blocked in the websocket thread. Some hosts (e.g. Silhouette on Linux) reset it to the default action which terminates the process when writing to a closed connection.

Behavior change: listeners of connection.closed won't receive short drops anymore, and reconnect() called on an open connection emits connection.interrupted instead of connection.closed.

Testing notes:

  1. pytest tests/test_event_hub_ping.py tests/test_event_hub_lifecycle.py
  2. Connect a tool to the server, kill/restart the server's proxy (or briefly drop network) and check the connection is not reported as lost when it comes back quickly.
  3. Restart the server from the UI and check the restart is still reported right away.
  4. Log out (reset_token()) with the hub connected and check connection.closed is emitted.

- Reconnect a working connection silently before reporting it as lost,
  emit 'connection.interrupted' instead. Server restart is still
  reported as closed right away.
- Count receive timeouts instead of measuring wall-clock time, so a
  host holding the GIL does not make the connection look dead.
- Block SIGPIPE in the websocket thread, some hosts reset it to the
  default action which terminates the process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iLLiCiTiT iLLiCiTiT added the type: bug Something isn't working label Nov 23, 2026
@iLLiCiTiT iLLiCiTiT self-assigned this Nov 23, 2026
@iLLiCiTiT
iLLiCiTiT requested a review from BigRoy November 23, 2026 13:26

@BigRoy BigRoy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked it against the ayon-core hub, the logic matches apart from _wait. One real issue (logout), the rest is small.

Comment thread ayon_api/events.py Outdated
# by a proxy or a network hiccup. Report it as lost only if
# reconnecting fails. Server restart is reported right away, the
# restart overlay is hidden only by 'connection.opened'.
if received_message and not state.server_is_restarting:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core's hub has no reconnect()/reauthenticate(), so this branch now also catches logout: reset_token() -> reauthenticate() -> reconnect() emits connection.interrupted, leaves the state None and never reports connection.closed (develop: closed, state False).
Only reconnecting silently when there is a token fixes it, existing tests still pass. Worth a regression test in test_event_hub_lifecycle.py (con.reset_token() -> expect connection.closed).

Suggested change
if received_message and not state.server_is_restarting:
if (
received_message
and not state.server_is_restarting
and self._connection.access_token
):

Comment thread ayon_api/events.py
Internal topics:
- 'connection.opened' first message received after (re)connection.
- 'connection.closed' connection was lost or could not be created.
- 'connection.interrupted' connection was lost and is reconnected

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listeners that resync on connection.closed won't see short drops anymore, core needed a follow-up for its folders cache for exactly this (also listens to connection.interrupted now). Maybe say here and in the changelog that it should be handled like closed by anything caching server data.

Comment thread ayon_api/events.py
# Python ignores it, but some hosts reset it to the default
# action which terminates the process (e.g. Silhouette on Linux).
if hasattr(signal, "pthread_sigmask"):
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGPIPE})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callbacks run in this thread, so threads and subprocesses started from a callback inherit the blocked SIGPIPE too (checked on Rocky 9: yes | head started from the hub thread prints Broken pipe instead of exiting quietly). Low risk and core does the same, maybe just a line in the Warnings of the class docstring.

assert fake_ws.pings == 2


def test_timeouts_are_counted_not_measured(run_hub, monkeypatch):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one also passes against develop's wall-clock code, so it doesn't guard the change. Jumping time.monotonic forward a few minutes on each TIMEOUT (like a GIL stall) would make the old code report closed. Patching both constants to their default values doesn't do anything either.

iLLiCiTiT and others added 2 commits November 24, 2026 17:24
- Reconnect silently only while there is a token, logout closed the
  connection and was reported as 'connection.interrupted' with unknown
  state instead of 'connection.closed'.
- Say in docstring that 'connection.interrupted' should be handled like
  'connection.closed' by anything caching server data.
- Timeout counting test now simulates GIL stall with a jumping clock,
  it passed with wall-clock based detection before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants