Skip to content

Fix reconnection bug on Twisted#154

Merged
gonzalocasas merged 1 commit into
mainfrom
fix-twisted-reconnect-after-idle
Jun 25, 2026
Merged

Fix reconnection bug on Twisted#154
gonzalocasas merged 1 commit into
mainfrom
fix-twisted-reconnect-after-idle

Conversation

@gonzalocasas

@gonzalocasas gonzalocasas commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

After a disconnect, if you reconnect within 5 seconds, everything works, but after that, the service gives up. Here's a fix for it.

This should address the bugs on #97 (comment), #98 and #121

What type of change is this?

  • Bug fix in a backwards-compatible manner.
  • New feature in a backwards-compatible manner.
  • Breaking change: bug fix or new feature that involve incompatible API changes.
  • Other (e.g. doc update, configuration, etc)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • [X ] I added a line to the CHANGELOG.rst file in the Unreleased section under the most fitting heading (e.g. Added, Changed, Removed).
  • I ran all tests on my computer and it's all green (i.e. invoke test).
  • I ran lint on my computer and there are no errors (i.e. invoke check).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

`AutobahnRosBridgeClientFactory.connect()` registered the connector by
calling `connectWS()` directly from the caller's thread, which does not
wake the Twisted reactor. On the first connection this is harmless because
the reactor is started (and processes the pending connector) right after.
But on a later reconnect with the reactor already running, an idle reactor
sits blocked in `select()` with no file descriptors and never notices the
connector added from another thread, so the connection never completes and
`run()` times out.

This surfaced when reconnecting after a disconnect: for roughly
`closeHandshakeTimeout` (5s) the closing connection keeps the reactor
cycling, so a quick reconnect works, but waiting longer leaves the reactor
idle and the reconnect hangs until timeout (e.g. toggling a Grasshopper ROS
client off, waiting, and back on).

Schedule the connection through `reactor.callFromThread()` (as
`send_message`/`send_close` already do), which wakes the reactor. It is also
safe before the reactor has started, since the call is queued and runs once
it does.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@gonzalocasas gonzalocasas requested review from chenkasirer and Copilot and removed request for Copilot June 24, 2026 23:31

@chenkasirer chenkasirer left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM!

@gonzalocasas gonzalocasas merged commit 67ee49d into main Jun 25, 2026
10 checks passed
@gonzalocasas gonzalocasas deleted the fix-twisted-reconnect-after-idle branch June 25, 2026 17:10
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 this pull request may close these issues.

2 participants