Skip to content

Commit f846861

Browse files
committed
#4777 Fix webrtc crashing in worker threads on shutdown
1 parent 4b5d1c6 commit f846861

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

indra/llwebrtc/llwebrtc.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,6 @@ void LLWebRTCImpl::terminate()
374374

375375
mSignalingThread->BlockingCall([this]() { mPeerConnectionFactory = nullptr; });
376376

377-
mPeerConnections.clear();
378-
379377
mWorkerThread->BlockingCall(
380378
[this]()
381379
{
@@ -386,6 +384,14 @@ void LLWebRTCImpl::terminate()
386384
mDeviceModule = nullptr;
387385
mTaskQueueFactory = nullptr;
388386
});
387+
388+
// In case peer connections still somehow have jobs in workers,
389+
// only clear connections up after clearing workers.
390+
mNetworkThread = nullptr;
391+
mWorkerThread = nullptr;
392+
mSignalingThread = nullptr;
393+
394+
mPeerConnections.clear();
389395
webrtc::LogMessage::RemoveLogToStream(mLogSink);
390396
}
391397

@@ -766,6 +772,7 @@ void LLWebRTCImpl::freePeerConnection(LLWebRTCPeerConnectionInterface* peer_conn
766772
std::find(mPeerConnections.begin(), mPeerConnections.end(), peer_connection);
767773
if (it != mPeerConnections.end())
768774
{
775+
// Todo: make sure conection had no jobs in workers
769776
mPeerConnections.erase(it);
770777
if (mPeerConnections.empty())
771778
{
@@ -785,14 +792,19 @@ LLWebRTCPeerConnectionImpl::LLWebRTCPeerConnectionImpl() :
785792
mWebRTCImpl(nullptr),
786793
mPeerConnection(nullptr),
787794
mMute(MUTE_INITIAL),
788-
mAnswerReceived(false)
795+
mAnswerReceived(false),
796+
mPendingJobs(0)
789797
{
790798
}
791799

792800
LLWebRTCPeerConnectionImpl::~LLWebRTCPeerConnectionImpl()
793801
{
794802
mSignalingObserverList.clear();
795803
mDataObserverList.clear();
804+
if (mPendingJobs > 0)
805+
{
806+
RTC_LOG(LS_ERROR) << __FUNCTION__ << "Destroying a connection that has " << std::to_string(mPendingJobs) << " unfinished jobs that might cause workers to crash";
807+
}
796808
}
797809

798810
//
@@ -804,8 +816,10 @@ void LLWebRTCPeerConnectionImpl::init(LLWebRTCImpl * webrtc_impl)
804816
mWebRTCImpl = webrtc_impl;
805817
mPeerConnectionFactory = mWebRTCImpl->getPeerConnectionFactory();
806818
}
819+
807820
void LLWebRTCPeerConnectionImpl::terminate()
808821
{
822+
mPendingJobs++;
809823
mWebRTCImpl->PostSignalingTask(
810824
[this]()
811825
{
@@ -848,7 +862,9 @@ void LLWebRTCPeerConnectionImpl::terminate()
848862
observer->OnPeerConnectionClosed();
849863
}
850864
}
865+
mPendingJobs--;
851866
});
867+
mPeerConnectionFactory.release();
852868
}
853869

854870
void LLWebRTCPeerConnectionImpl::setSignalingObserver(LLWebRTCSignalingObserver *observer) { mSignalingObserverList.emplace_back(observer); }
@@ -869,6 +885,7 @@ bool LLWebRTCPeerConnectionImpl::initializeConnection(const LLWebRTCPeerConnecti
869885
RTC_DCHECK(!mPeerConnection);
870886
mAnswerReceived = false;
871887

888+
mPendingJobs++;
872889
mWebRTCImpl->PostSignalingTask(
873890
[this,options]()
874891
{
@@ -902,6 +919,7 @@ bool LLWebRTCPeerConnectionImpl::initializeConnection(const LLWebRTCPeerConnecti
902919
{
903920
observer->OnRenegotiationNeeded();
904921
}
922+
mPendingJobs--;
905923
return;
906924
}
907925

@@ -964,6 +982,7 @@ bool LLWebRTCPeerConnectionImpl::initializeConnection(const LLWebRTCPeerConnecti
964982

965983
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions offerOptions;
966984
mPeerConnection->CreateOffer(this, offerOptions);
985+
mPendingJobs--;
967986
});
968987

969988
return true;
@@ -1006,6 +1025,7 @@ void LLWebRTCPeerConnectionImpl::AnswerAvailable(const std::string &sdp)
10061025
{
10071026
RTC_LOG(LS_INFO) << __FUNCTION__ << " Remote SDP: " << sdp;
10081027

1028+
mPendingJobs++;
10091029
mWebRTCImpl->PostSignalingTask(
10101030
[this, sdp]()
10111031
{
@@ -1015,6 +1035,7 @@ void LLWebRTCPeerConnectionImpl::AnswerAvailable(const std::string &sdp)
10151035
mPeerConnection->SetRemoteDescription(webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp),
10161036
webrtc::scoped_refptr<webrtc::SetRemoteDescriptionObserverInterface>(this));
10171037
}
1038+
mPendingJobs--;
10181039
});
10191040
}
10201041

@@ -1037,6 +1058,7 @@ void LLWebRTCPeerConnectionImpl::setMute(bool mute)
10371058
mMute = new_state;
10381059

10391060

1061+
mPendingJobs++;
10401062
mWebRTCImpl->PostSignalingTask(
10411063
[this, force_reset, enable]()
10421064
{
@@ -1060,6 +1082,7 @@ void LLWebRTCPeerConnectionImpl::setMute(bool mute)
10601082
track->set_enabled(enable);
10611083
}
10621084
}
1085+
mPendingJobs--;
10631086
}
10641087
});
10651088
}
@@ -1081,6 +1104,7 @@ void LLWebRTCPeerConnectionImpl::resetMute()
10811104

10821105
void LLWebRTCPeerConnectionImpl::setReceiveVolume(float volume)
10831106
{
1107+
mPendingJobs++;
10841108
mWebRTCImpl->PostSignalingTask(
10851109
[this, volume]()
10861110
{
@@ -1099,11 +1123,13 @@ void LLWebRTCPeerConnectionImpl::setReceiveVolume(float volume)
10991123
}
11001124
}
11011125
}
1126+
mPendingJobs--;
11021127
});
11031128
}
11041129

11051130
void LLWebRTCPeerConnectionImpl::setSendVolume(float volume)
11061131
{
1132+
mPendingJobs++;
11071133
mWebRTCImpl->PostSignalingTask(
11081134
[this, volume]()
11091135
{
@@ -1114,6 +1140,7 @@ void LLWebRTCPeerConnectionImpl::setSendVolume(float volume)
11141140
track->GetSource()->SetVolume(volume*5.0);
11151141
}
11161142
}
1143+
mPendingJobs--;
11171144
});
11181145
}
11191146

@@ -1190,11 +1217,13 @@ void LLWebRTCPeerConnectionImpl::OnConnectionChange(webrtc::PeerConnectionInterf
11901217
{
11911218
case webrtc::PeerConnectionInterface::PeerConnectionState::kConnected:
11921219
{
1220+
mPendingJobs++;
11931221
mWebRTCImpl->PostWorkerTask([this]() {
11941222
for (auto &observer : mSignalingObserverList)
11951223
{
11961224
observer->OnAudioEstablished(this);
11971225
}
1226+
mPendingJobs--;
11981227
});
11991228
break;
12001229
}
@@ -1452,11 +1481,13 @@ void LLWebRTCPeerConnectionImpl::sendData(const std::string& data, bool binary)
14521481
{
14531482
webrtc::CopyOnWriteBuffer cowBuffer(data.data(), data.length());
14541483
webrtc::DataBuffer buffer(cowBuffer, binary);
1484+
mPendingJobs++;
14551485
mWebRTCImpl->PostNetworkTask([this, buffer]() {
14561486
if (mDataChannel)
14571487
{
14581488
mDataChannel->Send(buffer);
14591489
}
1490+
mPendingJobs--;
14601491
});
14611492
}
14621493
}

indra/llwebrtc/llwebrtc_impl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, public webrtc::AudioDeviceO
422422
~LLWebRTCImpl()
423423
{
424424
delete mLogSink;
425+
426+
// Explicit cleanup for the sake of debugging and crash stacks
427+
mPeerCustomProcessor = nullptr;
425428
}
426429

427430
void init();
@@ -669,6 +672,8 @@ class LLWebRTCPeerConnectionImpl : public LLWebRTCPeerConnectionInterface,
669672
// data
670673
std::vector<LLWebRTCDataObserver *> mDataObserverList;
671674
webrtc::scoped_refptr<webrtc::DataChannelInterface> mDataChannel;
675+
676+
std::atomic<int> mPendingJobs;
672677
};
673678

674679
}

indra/newview/llvoicewebrtc.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,8 @@ void LLWebRTCVoiceClient::terminate()
291291
LL_INFOS("Voice") << "Terminating WebRTC" << LL_ENDL;
292292

293293
mVoiceEnabled = false;
294+
sShuttingDown = true; // so that coroutines won't post more work.
294295
llwebrtc::terminate();
295-
296-
sShuttingDown = true;
297296
}
298297

299298
//---------------------------------------------------
@@ -2659,6 +2658,11 @@ void LLVoiceWebRTCConnection::breakVoiceConnectionCoro(connectionPtr_t connectio
26592658
void LLVoiceWebRTCSpatialConnection::requestVoiceConnection()
26602659
{
26612660
LL_PROFILE_ZONE_SCOPED_CATEGORY_VOICE;
2661+
if (LLWebRTCVoiceClient::isShuttingDown())
2662+
{
2663+
mOutstandingRequests--;
2664+
return;
2665+
}
26622666

26632667
LLViewerRegion *regionp = LLWorld::instance().getRegionFromID(mRegionID);
26642668

@@ -3266,6 +3270,12 @@ void LLVoiceWebRTCAdHocConnection::requestVoiceConnection()
32663270
{
32673271
LL_PROFILE_ZONE_SCOPED_CATEGORY_VOICE;
32683272

3273+
if (LLWebRTCVoiceClient::isShuttingDown())
3274+
{
3275+
mOutstandingRequests--;
3276+
return;
3277+
}
3278+
32693279
LLViewerRegion *regionp = LLWorld::instance().getRegionFromID(mRegionID);
32703280

32713281
LL_DEBUGS("Voice") << "Requesting voice connection." << LL_ENDL;

0 commit comments

Comments
 (0)