Skip to content

Commit cd66bca

Browse files
authored
fix: stop exceptions from callbacks unwinding through WebRTC (#335)
The callbacks WebRTC invokes ended with ExceptionCheck(), which turns a Java exception thrown by the application into a C++ exception. It then unwound through WebRTC's frames, which are built without unwinding support, leaving WebRTC's locks held or ending the process, depending on the platform. The same held for C++ exceptions thrown while converting a callback's arguments. Every such callback now catches its own C++ exceptions, turns them into Java ones, and ends with ReportPendingException(), which clears the pending exception and hands it to the thread's uncaught exception handler: the peer connection, session description, data channel, DTLS, DTMF and stats observers, the audio and video track sinks, the track observer, the audio device's recorded-data sink and the desktop capture callback. The add-ICE-candidate and data channel send observers, which already reported exceptions this way, now share the helper. While in these callbacks: - the audio sinks hold their byte array in a JavaLocalRef, so it is freed when the callback throws, and pass jint values to the Java callback instead of size_t through varargs; - the track observer no longer leaks a local reference per event; - CustomVideoSource.pushFrame catches conversion errors instead of letting a C++ exception leave the JNI call.
1 parent 2b78158 commit cd66bca

19 files changed

Lines changed: 510 additions & 140 deletions

‎webrtc-jni/src/main/cpp/include/api/PeerConnectionObserver.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ namespace jni
7272
};
7373

7474
private:
75-
// Reports a Java exception thrown by a callback without letting it
76-
// unwind through WebRTC.
77-
void ReportException(JNIEnv * env) noexcept;
78-
7975
JavaGlobalRef<jobject> observer;
8076

8177
const std::shared_ptr<JavaPeerConnectionObserverClass> javaClass;

‎webrtc-jni/src/main/cpp/include/api/WebRTCUtils.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ namespace jni
8282
// uncaught exception handler, as the JVM does for an exception escaping a
8383
// Java thread. Callbacks that WebRTC invokes use it, because a C++
8484
// exception must not unwind through WebRTC's frames.
85-
void ReportUncaughtException(JNIEnv * env, jthrowable exception);
85+
void ReportUncaughtException(JNIEnv * env, jthrowable exception) noexcept;
86+
87+
// Clears the Java exception pending on the calling thread, if any, and
88+
// reports it as uncaught. A callback that WebRTC invokes ends with this
89+
// instead of ExceptionCheck(), which throws, after turning any C++
90+
// exception of its own into a Java one with ThrowCxxJavaException().
91+
void ReportPendingException(JNIEnv * env) noexcept;
8692
}
8793

8894
#endif

‎webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_push
6767
CHECK_HANDLE(source);
6868

6969
if (javaFrame != nullptr) {
70-
auto frame = jni::JavaLocalRef<jobject>(env, javaFrame);
71-
webrtc::VideoFrame nativeFrame = jni::VideoFrame::toNative(env, frame);
72-
73-
source->PushFrame(nativeFrame);
70+
try {
71+
auto frame = jni::JavaLocalRef<jobject>(env, javaFrame);
72+
webrtc::VideoFrame nativeFrame = jni::VideoFrame::toNative(env, frame);
73+
74+
source->PushFrame(nativeFrame);
75+
}
76+
catch (...) {
77+
ThrowCxxJavaException(env);
78+
}
7479
}
7580
}
7681

@@ -81,9 +86,14 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_push
8186
CHECK_HANDLE(source);
8287

8388
if (javaFrame != nullptr) {
84-
auto frame = jni::JavaLocalRef<jobject>(env, javaFrame);
85-
webrtc::VideoFrame nativeFrame = jni::VideoFrame::toNative(env, frame);
86-
87-
source->PushFrame(nativeFrame, timestampUs);
89+
try {
90+
auto frame = jni::JavaLocalRef<jobject>(env, javaFrame);
91+
webrtc::VideoFrame nativeFrame = jni::VideoFrame::toNative(env, frame);
92+
93+
source->PushFrame(nativeFrame, timestampUs);
94+
}
95+
catch (...) {
96+
ThrowCxxJavaException(env);
97+
}
8898
}
8999
}

‎webrtc-jni/src/main/cpp/src/api/AddIceCandidateObserver.cpp‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ namespace jni
7979
}
8080

8181
// A Java exception must not escape into WebRTC's signaling thread.
82-
if (env->ExceptionCheck()) {
83-
JavaLocalRef<jthrowable> exception(env, env->ExceptionOccurred());
84-
env->ExceptionClear();
85-
ReportUncaughtException(env, exception.get());
86-
}
82+
ReportPendingException(env);
8783
}
8884

8985
AddIceCandidateObserver::JavaAddIceCandidateObserverClass::JavaAddIceCandidateObserverClass(JNIEnv * env)

‎webrtc-jni/src/main/cpp/src/api/AudioTrackSink.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616

1717
#include "api/AudioTrackSink.h"
18+
#include "api/WebRTCUtils.h"
1819
#include "JavaClasses.h"
20+
#include "JavaUtils.h"
1921
#include "JNI_WebRTC.h"
2022

2123
namespace jni
@@ -37,12 +39,15 @@ namespace jni
3739
const jbyte * buffer = static_cast<const jbyte *>(data);
3840
jsize dataSize = static_cast<jsize>(frames * channels * (bitsPerSample / 8));
3941

40-
jbyteArray dataArray = env->NewByteArray(dataSize);
41-
env->SetByteArrayRegion(dataArray, 0, dataSize, buffer);
42+
JavaLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataSize));
4243

43-
env->CallVoidMethod(sink, javaClass->onData, dataArray, bitsPerSample, sampleRate, channels, frames);
44-
ExceptionCheck(env);
45-
env->DeleteLocalRef(dataArray);
44+
if (dataArray.get() != nullptr) {
45+
env->SetByteArrayRegion(dataArray.get(), 0, dataSize, buffer);
46+
env->CallVoidMethod(sink, javaClass->onData, dataArray.get(), static_cast<jint>(bitsPerSample),
47+
static_cast<jint>(sampleRate), static_cast<jint>(channels), static_cast<jint>(frames));
48+
}
49+
50+
ReportPendingException(env);
4651
}
4752

4853
AudioTrackSink::JavaAudioTrackSinkClass::JavaAudioTrackSinkClass(JNIEnv * env)

‎webrtc-jni/src/main/cpp/src/api/CreateSessionDescriptionObserver.cpp‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "api/RTCSessionDescription.h"
1919
#include "api/WebRTCUtils.h"
2020
#include "JavaString.h"
21+
#include "JavaUtils.h"
2122
#include "JNI_WebRTC.h"
2223

2324
namespace jni
@@ -36,11 +37,16 @@ namespace jni
3637
return;
3738
}
3839

39-
JavaLocalRef<jobject> javaDesc = jni::RTCSessionDescription::toJava(env, desc);
40+
try {
41+
JavaLocalRef<jobject> javaDesc = jni::RTCSessionDescription::toJava(env, desc);
4042

41-
env->CallVoidMethod(observer, javaClass->onSuccess, javaDesc.get());
43+
env->CallVoidMethod(observer, javaClass->onSuccess, javaDesc.get());
44+
}
45+
catch (...) {
46+
ThrowCxxJavaException(env);
47+
}
4248

43-
ExceptionCheck(env);
49+
ReportPendingException(env);
4450
}
4551

4652
void CreateSessionDescriptionObserver::OnFailure(webrtc::RTCError error)
@@ -51,11 +57,16 @@ namespace jni
5157
return;
5258
}
5359

54-
JavaLocalRef<jstring> errorMessage = JavaString::toJava(env, RTCErrorToString(error));
60+
try {
61+
JavaLocalRef<jstring> errorMessage = JavaString::toJava(env, RTCErrorToString(error));
5562

56-
env->CallVoidMethod(observer, javaClass->onFailure, errorMessage.get());
63+
env->CallVoidMethod(observer, javaClass->onFailure, errorMessage.get());
64+
}
65+
catch (...) {
66+
ThrowCxxJavaException(env);
67+
}
5768

58-
ExceptionCheck(env);
69+
ReportPendingException(env);
5970
}
6071

6172
CreateSessionDescriptionObserver::JavaCreateSessionDescObserverClass::JavaCreateSessionDescObserverClass(JNIEnv * env)

0 commit comments

Comments
 (0)