Skip to content

Reconnect through normal websocket setup#40

Open
StantonMatt wants to merge 2 commits into
NullVoxPopuli:masterfrom
StantonMatt:fix-reconnect-callbacks
Open

Reconnect through normal websocket setup#40
StantonMatt wants to merge 2 commits into
NullVoxPopuli:masterfrom
StantonMatt:fix-reconnect-callbacks

Conversation

@StantonMatt

@StantonMatt StantonMatt commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • make reconnect! close the stale websocket and reconnect through connect!
  • preserve headers, TLS options, received handlers, and error handlers across reconnects
  • guard stale websocket callbacks so old close/message/error events cannot mutate the new connection state

Fixes #33

Verification

  • bundle exec rspec spec/unit/action_cable_client_spec.rb
  • bundle exec rspec
  • bundle exec rubocop
  • git diff --check origin/master...HEAD

Hosted CI

  • Run 26799993832: RuboCop and Ruby 2.3/2.4/2.5/2.6/2.7 test jobs passed

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@NullVoxPopuli

Copy link
Copy Markdown
Owner

thanks for this! would you be willing to help me update the ci/lint infra on this repo?

@StantonMatt

Copy link
Copy Markdown
Contributor Author

Yes, happy to help. I’ll keep this reconnect PR scoped and take a separate pass at the CI/lint infra so it’s easier to review. I’ll first reproduce the current lint/CI failures locally, then open a focused follow-up PR with the smallest set of changes needed to get the checks useful again.

@StantonMatt

StantonMatt commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

Update after #42 merged: I rebuilt this branch on current master and dropped the duplicate CI cleanup commit, so the PR diff is back to the reconnect change plus its unit specs only.

Local verification on the rebuilt branch:

  • bundle exec rspec spec/unit/action_cable_client_spec.rb
  • bundle exec rspec
  • bundle exec rubocop
  • git diff --check origin/master...HEAD
  • clean review-fix-loop pass

The new commit is d622036df189a04955f9aa58eaf9ee5860e3d0d2. GitHub created CI run 26799993832 for it, but that run is currently action_required with no child jobs until maintainer approval. The WIP check is green. I am not counting the CI suite as green until those Ruby/RuboCop jobs actually run.

Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
@StantonMatt StantonMatt force-pushed the fix-reconnect-callbacks branch from f1290c0 to d622036 Compare June 2, 2026 05:16
uri = URI(@_uri)
EventMachine.reconnect uri.host, uri.port, @_websocket_client
@_websocket_client.post_init
self._connection_id += 1

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

since we increment this before calling _websocket_close, could there not be a race condition here between this function and the onclose hook above?

@StantonMatt StantonMatt Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point to call out. The increment before close is meant to invalidate the old websocket's onclose handler before close can synchronously fire it.

I added a regression in 3e4419f where close invokes the previous onclose block during reconnect!; it asserts the disconnected callback is not called and the new websocket is still installed.

Local verification against 3e4419f:

  • RUBYOPT=-rem/pure_ruby bundle exec rspec
  • bundle exec rubocop
  • git diff --check

The GitHub Actions run for 3e4419f is action_required with no jobs, so it looks like it needs maintainer approval before CI can run.

Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
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.

reconnect! method not calling the connected callback?

2 participants