Avoid closing websockets in a connecting state to deal with Safari bug #44304
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aconnecting
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