From f994878fcd035ef1e455dbe27197343db0e7db72 Mon Sep 17 00:00:00 2001 From: Kershaw Chang Date: Fri, 23 Aug 2024 20:40:13 +0000 Subject: [PATCH] Bug 1907726 - Make sure WebTransportSessionProxy::NotifyDatagramReceived is called after OnStopRequest, r=necko-reviewers,jesup The crash occurs because WebTransportSessionProxy::OnDatagramReceivedInternal is called before WebTransportSessionProxy::OnStopRequest. When this happens, WebTransportSessionProxy::mTarget is the main thread, so a task is dispatched to the main thread. This causes WebTransportSessionProxy::NotifyDatagramReceived to be called on the main thread. If WebTransportSessionProxy::NotifyDatagramReceived is invoked while WebTransportSessionProxy::mStopRequestCalled is true, it can lead to OnDatagramReceived being called on the main thread (instead of the socket thread), resulting in a crash. Differential Revision: https://phabricator.services.mozilla.com/D220013 --- .../webtransport/WebTransportSessionProxy.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/netwerk/protocol/webtransport/WebTransportSessionProxy.cpp b/netwerk/protocol/webtransport/WebTransportSessionProxy.cpp index dd01cc6ff8d2..e0a80c25ba12 100644 --- a/netwerk/protocol/webtransport/WebTransportSessionProxy.cpp +++ b/netwerk/protocol/webtransport/WebTransportSessionProxy.cpp @@ -1078,15 +1078,6 @@ void WebTransportSessionProxy::NotifyDatagramReceived( MutexAutoLock lock(mMutex); MOZ_ASSERT(mTarget->IsOnCurrentThread()); - if (!mStopRequestCalled) { - CopyableTArray copied(aData); - mPendingEvents.AppendElement( - [self = RefPtr{this}, data = std::move(copied)]() mutable { - self->NotifyDatagramReceived(std::move(data)); - }); - return; - } - if (mState != WebTransportSessionProxyState::ACTIVE || !mListener) { return; } @@ -1102,6 +1093,15 @@ NS_IMETHODIMP WebTransportSessionProxy::OnDatagramReceivedInternal( { MutexAutoLock lock(mMutex); + if (!mStopRequestCalled) { + CopyableTArray copied(aData); + mPendingEvents.AppendElement( + [self = RefPtr{this}, data = std::move(copied)]() mutable { + self->OnDatagramReceivedInternal(std::move(data)); + }); + return NS_OK; + } + if (!mTarget->IsOnCurrentThread()) { return mTarget->Dispatch(NS_NewRunnableFunction( "WebTransportSessionProxy::OnDatagramReceived",