com.unity.webrtc icon indicating copy to clipboard operation
com.unity.webrtc copied to clipboard

[BUG]: NativeCrash in Unity.WebRTC.NativeMethods:ContextDeleteStatsReport

Open streunerlein opened this issue 2 years ago • 2 comments

Package version

2.4.0-exp.9

Environment

* OS: Win10
* Unity version: 2021.3

Steps To Reproduce

  1. Have the following code to get an audioLevel estimation for an incoming audio stream:
// after setting up Peer Connection
StartCoroutine(FetchAudioStats());

private IEnumerator FetchAudioStats()
{
    while (connection != null)
    {
        var statsOperation = connection.GetStats();
        yield return statsOperation;
        var statsReport = statsOperation.Value;
        foreach (KeyValuePair<string, RTCStats> stat in statsReport.Stats)
        {
            if (stat.Value is RTCMediaStreamTrackStats audioStat && audioStat.remoteSource)
            {
                double audioLevel = audioStat.audioLevel;
                audioLevelIncoming = (float)audioLevel;
                break;
            }
        }
    }
}
  1. Not really a good reproduction, but in our case it is a VR headset going to sleep and coming back triggers a crash reliably.

Current Behavior

Native Crash:

=================================================================
	Native Crash Reporting
=================================================================
Got a UNKNOWN while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at Unity.WebRTC.NativeMethods:ContextDeleteStatsReport <0x00094>
	  at Unity.WebRTC.Context:DeleteStatsReport <0x0002a>
	  at Unity.WebRTC.RTCStatsReport:Dispose <0x0008a>
	  at Unity.WebRTC.RTCStatsReport:Finalize <0x0001a>
	  at System.Object:runtime_invoke_virtual_void__this__ <0x0008b>
=================================================================

and

========== OUTPUTTING STACK TRACE ==================

0x00007FFA4F582DFD (webrtc) GetUpdateTextureFunc
0x00007FFA4F582DAF (webrtc) GetUpdateTextureFunc
0x000001E6B74A1125 (Mono JIT Code) (wrapper managed-to-native) Unity.WebRTC.NativeMethods:ContextDeleteStatsReport (intptr,intptr)
0x000001E6B74A103B (Mono JIT Code) Unity.WebRTC.Context:DeleteStatsReport (intptr)
0x000001E6B74A0F3B (Mono JIT Code) Unity.WebRTC.RTCStatsReport:Dispose ()
0x000001E6B74A0E1B (Mono JIT Code) Unity.WebRTC.RTCStatsReport:Finalize ()
0x000001E5BADEF85C (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_virtual_void__this__ (object,intptr,intptr,intptr)
0x00007FFA503CF215 (mono-2.0-bdwgc) mono_callspec_cleanup
0x00007FFA506479EE (mono-2.0-bdwgc) mono_breakpoint_clean_code
0x00007FFA503D0889 (mono-2.0-bdwgc) mono_gc_finalize_notify
0x00007FFA5036B94B (mono-2.0-bdwgc) mono_security_set_mode
0x00007FFA5036BB26 (mono-2.0-bdwgc) mono_security_set_mode
0x00007FFA888D4ED0 (KERNEL32) BaseThreadInitThunk
0x00007FFA8A08E39B (ntdll) RtlUserThreadStart

========== END OF STACKTRACE ===========

Expected Behavior

No crash.

Anything else?

No response

streunerlein avatar Aug 12 '22 14:08 streunerlein

I think this is related to #542, it seems also to be fixed by manually Disposeing the RTCStatsReport object.

streunerlein avatar Aug 16 '22 10:08 streunerlein

memo: WRS-146

karasusan avatar Aug 23 '22 05:08 karasusan

Just adding here that Disposeing the RTCStatsReport is only a partial fix, we are having multiple reports from users and internal testing that this still occasionally crashes clients, in our tests is 1 out of 5 clients.

streunerlein avatar Sep 08 '22 10:09 streunerlein

@streunerlein Thank you for testing!

karasusan avatar Sep 13 '22 02:09 karasusan