Skip to content

Commit 8d51a09

Browse files
Merge pull request #1482 from paullouisageneau/fix-ws-onerror-reset-crash
Fix crash if WebSocket is deleted in onError callback
2 parents 8c31097 + 9076d5f commit 8d51a09

File tree

7 files changed

+222
-228
lines changed

7 files changed

+222
-228
lines changed

.github/workflows/build-mbedtls.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ jobs:
1212
- name: Set up Homebrew
1313
uses: Homebrew/actions/setup-homebrew@master
1414
- name: Install Mbed TLS
15-
run: brew update && brew install mbedtls
15+
run: brew update && brew install mbedtls@3
1616
- name: submodules
1717
run: git submodule update --init --recursive --depth 1
1818
- name: cmake
19-
run: cmake -B build -DUSE_MBEDTLS=1 -DWARNINGS_AS_ERRORS=1 -DCMAKE_PREFIX_PATH=$(brew --prefix mbedtls)
19+
run: cmake -B build -DUSE_MBEDTLS=1 -DWARNINGS_AS_ERRORS=1 -DCMAKE_PREFIX_PATH=$(brew --prefix mbedtls@3)
2020
- name: make
2121
run: (cd build; make -j2)
2222
- name: test
@@ -26,11 +26,11 @@ jobs:
2626
steps:
2727
- uses: actions/checkout@v4
2828
- name: Install Mbed TLS
29-
run: brew update && brew install mbedtls
29+
run: brew update && brew install mbedtls@3
3030
- name: submodules
3131
run: git submodule update --init --recursive --depth 1
3232
- name: cmake
33-
run: cmake -B build -DUSE_MBEDTLS=1 -DWARNINGS_AS_ERRORS=1 -DENABLE_LOCAL_ADDRESS_TRANSLATION=1 -DCMAKE_PREFIX_PATH=$(brew --prefix mbedtls)
33+
run: cmake -B build -DUSE_MBEDTLS=1 -DWARNINGS_AS_ERRORS=1 -DENABLE_LOCAL_ADDRESS_TRANSLATION=1 -DCMAKE_PREFIX_PATH=$(brew --prefix mbedtls@3)
3434
- name: make
3535
run: (cd build; make -j2)
3636
- name: test

include/rtc/pacinghandler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class RTC_CPP_EXPORT PacingHandler : public MediaHandler {
4040
std::queue<message_ptr> mRtpBuffer;
4141

4242
void schedule(const message_callback &send);
43+
void run(const message_callback &send);
4344
};
4445

4546
} // namespace rtc

src/impl/peerconnection.cpp

Lines changed: 93 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -162,52 +162,52 @@ shared_ptr<IceTransport> PeerConnection::initIceTransport() {
162162
auto transport = std::make_shared<IceTransport>(
163163
config, weak_bind(&PeerConnection::processLocalCandidate, this, _1),
164164
[this, weak_this = weak_from_this()](IceTransport::State transportState) {
165-
auto shared_this = weak_this.lock();
166-
if (!shared_this)
167-
return;
168-
switch (transportState) {
169-
case IceTransport::State::Connecting:
170-
changeIceState(IceState::Checking);
171-
changeState(State::Connecting);
172-
break;
173-
case IceTransport::State::Connected:
174-
changeIceState(IceState::Connected);
175-
initDtlsTransport();
176-
break;
177-
case IceTransport::State::Completed:
178-
changeIceState(IceState::Completed);
179-
break;
180-
case IceTransport::State::Failed:
181-
changeIceState(IceState::Failed);
182-
changeState(State::Failed);
183-
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
184-
break;
185-
case IceTransport::State::Disconnected:
186-
changeIceState(IceState::Disconnected);
187-
changeState(State::Disconnected);
188-
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
189-
break;
190-
default:
191-
// Ignore
192-
break;
193-
}
165+
if (auto locked = weak_this.lock())
166+
std::invoke([=]() {
167+
switch (transportState) {
168+
case IceTransport::State::Connecting:
169+
changeIceState(IceState::Checking);
170+
changeState(State::Connecting);
171+
break;
172+
case IceTransport::State::Connected:
173+
changeIceState(IceState::Connected);
174+
initDtlsTransport();
175+
break;
176+
case IceTransport::State::Completed:
177+
changeIceState(IceState::Completed);
178+
break;
179+
case IceTransport::State::Failed:
180+
changeIceState(IceState::Failed);
181+
changeState(State::Failed);
182+
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
183+
break;
184+
case IceTransport::State::Disconnected:
185+
changeIceState(IceState::Disconnected);
186+
changeState(State::Disconnected);
187+
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
188+
break;
189+
default:
190+
// Ignore
191+
break;
192+
}
193+
});
194194
},
195195
[this, weak_this = weak_from_this()](IceTransport::GatheringState gatheringState) {
196-
auto shared_this = weak_this.lock();
197-
if (!shared_this)
198-
return;
199-
switch (gatheringState) {
200-
case IceTransport::GatheringState::InProgress:
201-
changeGatheringState(GatheringState::InProgress);
202-
break;
203-
case IceTransport::GatheringState::Complete:
204-
endLocalCandidates();
205-
changeGatheringState(GatheringState::Complete);
206-
break;
207-
default:
208-
// Ignore
209-
break;
210-
}
196+
if (auto locked = weak_this.lock())
197+
std::invoke([=]() {
198+
switch (gatheringState) {
199+
case IceTransport::GatheringState::InProgress:
200+
changeGatheringState(GatheringState::InProgress);
201+
break;
202+
case IceTransport::GatheringState::Complete:
203+
endLocalCandidates();
204+
changeGatheringState(GatheringState::Complete);
205+
break;
206+
default:
207+
// Ignore
208+
break;
209+
}
210+
});
211211
});
212212

213213
return emplaceTransport(this, &mIceTransport, std::move(transport));
@@ -241,34 +241,33 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
241241

242242
auto certificate = mCertificate.get();
243243
auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1);
244-
auto dtlsStateChangeCallback =
245-
[this, weak_this = weak_from_this()](DtlsTransport::State transportState) {
246-
auto shared_this = weak_this.lock();
247-
if (!shared_this)
248-
return;
249-
250-
switch (transportState) {
251-
case DtlsTransport::State::Connected:
252-
if (auto remote = remoteDescription(); remote && remote->hasApplication())
253-
initSctpTransport();
254-
else
255-
changeState(State::Connected);
256-
257-
mProcessor.enqueue(&PeerConnection::openTracks, shared_from_this());
258-
break;
259-
case DtlsTransport::State::Failed:
260-
changeState(State::Failed);
261-
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
262-
break;
263-
case DtlsTransport::State::Disconnected:
264-
changeState(State::Disconnected);
265-
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
266-
break;
267-
default:
268-
// Ignore
269-
break;
270-
}
271-
};
244+
auto dtlsStateChangeCallback = [this, weak_this = weak_from_this()](
245+
DtlsTransport::State transportState) {
246+
if (auto locked = weak_this.lock())
247+
std::invoke([=]() {
248+
switch (transportState) {
249+
case DtlsTransport::State::Connected:
250+
if (auto remote = remoteDescription(); remote && remote->hasApplication())
251+
initSctpTransport();
252+
else
253+
changeState(State::Connected);
254+
255+
mProcessor.enqueue(&PeerConnection::openTracks, shared_from_this());
256+
break;
257+
case DtlsTransport::State::Failed:
258+
changeState(State::Failed);
259+
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
260+
break;
261+
case DtlsTransport::State::Disconnected:
262+
changeState(State::Disconnected);
263+
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
264+
break;
265+
default:
266+
// Ignore
267+
break;
268+
}
269+
});
270+
};
272271

273272
shared_ptr<DtlsTransport> transport;
274273
auto local = localDescription();
@@ -329,28 +328,28 @@ shared_ptr<SctpTransport> PeerConnection::initSctpTransport() {
329328
lower, config, std::move(ports), weak_bind(&PeerConnection::forwardMessage, this, _1),
330329
weak_bind(&PeerConnection::forwardBufferedAmount, this, _1, _2),
331330
[this, weak_this = weak_from_this()](SctpTransport::State transportState) {
332-
auto shared_this = weak_this.lock();
333-
if (!shared_this)
334-
return;
335-
336-
switch (transportState) {
337-
case SctpTransport::State::Connected:
338-
changeState(State::Connected);
339-
assignDataChannels();
340-
mProcessor.enqueue(&PeerConnection::openDataChannels, shared_from_this());
341-
break;
342-
case SctpTransport::State::Failed:
343-
changeState(State::Failed);
344-
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
345-
break;
346-
case SctpTransport::State::Disconnected:
347-
changeState(State::Disconnected);
348-
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
349-
break;
350-
default:
351-
// Ignore
352-
break;
353-
}
331+
if (auto locked = weak_this.lock())
332+
std::invoke([=]() {
333+
switch (transportState) {
334+
case SctpTransport::State::Connected:
335+
changeState(State::Connected);
336+
assignDataChannels();
337+
mProcessor.enqueue(&PeerConnection::openDataChannels,
338+
shared_from_this());
339+
break;
340+
case SctpTransport::State::Failed:
341+
changeState(State::Failed);
342+
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
343+
break;
344+
case SctpTransport::State::Disconnected:
345+
changeState(State::Disconnected);
346+
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
347+
break;
348+
default:
349+
// Ignore
350+
break;
351+
}
352+
});
354353
});
355354

356355
return emplaceTransport(this, &mSctpTransport, std::move(transport));

src/impl/track.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void Track::close() {
7373
triggerClosed();
7474
setMediaHandler(nullptr);
7575
resetCallbacks();
76-
}
76+
}
7777
}
7878

7979
message_variant Track::trackMessageToVariant(message_ptr message) {
@@ -144,9 +144,9 @@ void Track::incoming(message_ptr message) {
144144
message_vector messages{std::move(message)};
145145
if (auto handler = getMediaHandler()) {
146146
try {
147-
handler->incomingChain(messages, [this, weak_this = weak_from_this()](message_ptr m) {
147+
handler->incomingChain(messages, [weak_this = weak_from_this()](message_ptr m) {
148148
if (auto locked = weak_this.lock()) {
149-
transportSend(m);
149+
locked->transportSend(m);
150150
}
151151
});
152152
} catch (const std::exception &e) {
@@ -186,9 +186,9 @@ bool Track::outgoing(message_ptr message) {
186186

187187
if (handler) {
188188
message_vector messages{std::move(message)};
189-
handler->outgoingChain(messages, [this, weak_this = weak_from_this()](message_ptr m) {
189+
handler->outgoingChain(messages, [weak_this = weak_from_this()](message_ptr m) {
190190
if (auto locked = weak_this.lock()) {
191-
transportSend(m);
191+
locked->transportSend(m);
192192
}
193193
});
194194

0 commit comments

Comments
 (0)