Skip to content

Commit

Permalink
Bug 1227136 - crash in mozilla::net::WebSocketChannel::StartWebsocket…
Browse files Browse the repository at this point in the history
…Data, r=bagder, r=baku
  • Loading branch information
vonasek committed Mar 9, 2016
1 parent b13e3eb commit 5e134b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
25 changes: 6 additions & 19 deletions dom/base/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,9 @@ class WebSocketImpl final : public nsIInterfaceRequestor
bool isBinary);

// ConnectionCloseEvents: 'error' event if needed, then 'close' event.
// - These must not be dispatched while we are still within an incoming call
// from JS (ex: close()). Set 'sync' to false in that case to dispatch in a
// separate new event.
nsresult ScheduleConnectionCloseEvents(nsISupports* aContext,
nsresult aStatusCode,
bool sync);
// 2nd half of ScheduleConnectionCloseEvents, sometimes run in its own event.
nsresult aStatusCode);
// 2nd half of ScheduleConnectionCloseEvents, run in its own event.
void DispatchConnectionCloseEvents();

nsresult UpdateURI();
Expand Down Expand Up @@ -516,14 +512,11 @@ WebSocketImpl::CloseConnection(uint16_t aReasonCode,

mWebSocket->SetReadyState(WebSocket::CLOSING);

// Can be called from Cancel() or Init() codepaths, so need to dispatch
// onerror/onclose asynchronously
ScheduleConnectionCloseEvents(
nullptr,
(aReasonCode == nsIWebSocketChannel::CLOSE_NORMAL ||
aReasonCode == nsIWebSocketChannel::CLOSE_GOING_AWAY) ?
NS_OK : NS_ERROR_FAILURE,
false);
NS_OK : NS_ERROR_FAILURE);

return NS_OK;
}
Expand Down Expand Up @@ -797,14 +790,12 @@ WebSocketImpl::OnStop(nsISupports* aContext, nsresult aStatusCode)
MOZ_ASSERT(mWebSocket->ReadyState() != WebSocket::CLOSED,
"Shouldn't already be CLOSED when OnStop called");

// called by network stack, not JS, so can dispatch JS events synchronously
return ScheduleConnectionCloseEvents(aContext, aStatusCode, true);
return ScheduleConnectionCloseEvents(aContext, aStatusCode);
}

nsresult
WebSocketImpl::ScheduleConnectionCloseEvents(nsISupports* aContext,
nsresult aStatusCode,
bool sync)
nsresult aStatusCode)
{
AssertIsOnTargetThread();

Expand All @@ -824,11 +815,7 @@ WebSocketImpl::ScheduleConnectionCloseEvents(nsISupports* aContext,

mOnCloseScheduled = true;

if (sync) {
DispatchConnectionCloseEvents();
} else {
NS_DispatchToCurrentThread(new CallDispatchConnectionCloseEvents(this));
}
NS_DispatchToCurrentThread(new CallDispatchConnectionCloseEvents(this));
}

return NS_OK;
Expand Down
24 changes: 15 additions & 9 deletions netwerk/protocol/websocket/WebSocketChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2803,29 +2803,35 @@ WebSocketChannel::StartWebsocketData()
MOZ_ASSERT(!mDataStarted, "StartWebsocketData twice");
mDataStarted = 1;

LOG(("WebSocketChannel::StartWebsocketData Notifying Listener %p\n",
mListenerMT ? mListenerMT->mListener.get() : nullptr));

if (mListenerMT) {
mListenerMT->mListener->OnStart(mListenerMT->mContext);
}

rv = mSocketIn->AsyncWait(this, 0, 0, mSocketThread);
if (NS_FAILED(rv)) {
LOG(("WebSocketChannel::StartWebsocketData mSocketIn->AsyncWait() failed "
"with error %0x%08x\n", rv));
return rv;
"with error 0x%08x", rv));
return mSocketThread->Dispatch(
NS_NewRunnableMethodWithArgs<nsresult>(this,
&WebSocketChannel::AbortSession,
rv),
NS_DISPATCH_NORMAL);
}

if (mPingInterval) {
rv = mSocketThread->Dispatch(
NS_NewRunnableMethod(this, &WebSocketChannel::StartPinging),
NS_DISPATCH_NORMAL);
if (NS_FAILED(rv)) {
LOG(("WebSocketChannel::StartWebsocketData Could not start pinging, "
"rv=0x%08x", rv));
return rv;
}
}

LOG(("WebSocketChannel::StartWebsocketData Notifying Listener %p",
mListenerMT ? mListenerMT->mListener.get() : nullptr));

if (mListenerMT) {
mListenerMT->mListener->OnStart(mListenerMT->mContext);
}

return NS_OK;
}

Expand Down

0 comments on commit 5e134b5

Please sign in to comment.