Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid closing websockets in a connecting state to deal with Safari bug #44304

Merged
merged 1 commit into from Feb 2, 2022

Conversation

jorgemanrubia
Copy link
Contributor

@jorgemanrubia jorgemanrubia commented Feb 1, 2022

This addresses a problem where Safari's WebSockets gets permanently broken when closing them in a connecting state due to a bug in Safari.

This problem was introduced in recent Safari versions (15.1+), in their new WebSockets implementation, called NSURLSession Websockets. When it happens, it results in WebSockets being completely borked, requiring a full restart of Safari to fix (just reloading won't fix it).

This problem hit us with Basecamp campfires, and we validated this patch fixes the issue. It just avoids closing WebSockets in a connecting state. Other users have reported this same problem: see this and this. It's not specific to ActionCable, but ActionCable's monitoring system can close connections in a connecting state as part of its stale-connection detection logic, triggering this problem.

Despite Safari-specific, we decided to upstream the patch to Rails because this will impact any app using ActionCable. It's also a very tricky problem to troubleshoot (we spent weeks tracking this one down). It won't cause trouble in other browsers, so we keep it simple without doing browser detection. The worst that can happen is that a websocket connection in a connecting state that is not closed might eventually connect, and then the browser would have 2 connections, but it would die on its own out of inactivity since it won't be monitored or used by Action cable.

At some point, I want to think that Safari will fix this for good and then we can consider reverting this change.

See #43835 (comment) for more details. There is a script to reproduce the problem here.

@jeremy @dhh

This addresses a problem where Safari's websockets gets permanently broken
when closing them in a "connecting" state.

See rails#43835 (comment)
@rails-bot rails-bot bot added the actioncable label Feb 1, 2022
@jorgemanrubia jorgemanrubia changed the title Only close websockets when connection is open Avoid closing websockets in a connecting state to deal with Safari bug Feb 1, 2022
@dhh dhh merged commit ed8e767 into rails:main Feb 2, 2022
@dhh dhh deleted the close-websockets-patch branch February 3, 2022 14:52
public-rant pushed a commit to opensource-rant/rails that referenced this pull request Feb 17, 2022
This addresses a problem where Safari's websockets gets permanently broken
when closing them in a "connecting" state.

See rails#43835 (comment)
matthewd added a commit that referenced this pull request Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants