Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
742 stars 186 forks source link

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

Closed streunerlein closed 1 year ago

streunerlein commented 1 year ago

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 commented 1 year ago

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

karasusan commented 1 year ago

memo: WRS-146

streunerlein commented 1 year ago

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.

karasusan commented 1 year ago

@streunerlein Thank you for testing!