The example code uses ruby and the bundler dependency system which you can install with
gem install bundler
Install dependencies using bundler
bundle install
Start simple websocket server
ruby echo-ws.rb
In another terminal start the web app
ruby app.rb
To reproduce
- Open http://localhost:4567/
- Click 'Test page'
- Verify that connection is opened in echo-ws
- Click HOME
- Note that first connection is closed, but a mysterious new connection is opened
This is the expected output by echo-ws.rb
Opened 1
Closed 1
Opened 2
Firefox 9: Tried to workaround this by using a timeout but it doesn't work unless the timeout quite large.
The transition seems to happen at around 105ms. Using a 103ms timer, connection usually creates a ghost conn, 104ms about half the time, and at 105ms it almost never does (note: only tested on OS X Lion). You can verify by changing the onclose
function in test.erb
to
ws.onclose = function() {
setTimeout(connect, 103);
};
Firefox 13: If anything this seems even more broken than 9.
Timeout 0 case: onclose
is called (with wasClean = true
) when you click to navigate away from the page, then a new connection is opened (by the onclose
handler). This connection then appears to be closed by firefox almost instantly (presumably this is the workaround to this bug, so that the ghost connection is not left lying about).
Timeout of 90ms in onclose
: Exactly the same behaviour as case 0ms above.
Timeout of 100ms in onclose
: Sometimes behaves as 0ms, sometimes as 103ms.
Timeout of 103ms in onclose
: Inconsistent behaviour - there is definitely a race condition in firefox here. Usually this works perfectly (i.e. existing connection is closed and new one isn't opened), but sometimes a new connection is established, and it's left around as a ghost connection after exiting the page. This is basically the behaviour I saw when we initially reported this issue, but it's even worse: The TCP connection actually stays around even when the browser tab is closed, and is only closed when Firefox quits!
My hunch based on all this experimentation is that if a WebSocket connection is in the process of opening when the page is finally cleaned up by firefox (which seems to be after ~ 105ms), that the connection is orphaned forever. This could potentially leave Firefox amassing large numbers of ghost WebSocket connections.
Firefox 17: Same behaviour as Firefox 13, including leaving the WebSocket open after the browser tab is closed with some values of timeout (described above)
@mloughran do you happen to have a bug report reference for this issue?
EDIT: https://bugzilla.mozilla.org/show_bug.cgi?id=765738