Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition around startup can cause null reference exception if application focus changes #781

Closed
uncapped-shattuck opened this issue Mar 29, 2024 · 2 comments
Labels
released This feature/bug fix has been released

Comments

@uncapped-shattuck
Copy link

Describe the bug

There is a race condition between the BugsnagUnity.Bugsnag.InternalClient being initialized to a non-null value and attempting to use the InternalClient indirectly via TimingTrackingBehaviour.OnApplicationFocus - the ctor of BugsnagUnity.Client initializes the timing tracker, which means that focus changes that happen after the timing tracker starts but before the ctor finishes and the assignment completes will cause a null reference exception.

Steps to reproduce

This is a race condition with a very narrow window, so the change of reproducing it is low

  1. Call Bugsnag.Start() in Unity to initialize the client
  2. Alt-tab in and out of the Unity window
  3. If the timing lines up, see a NullReferenceException in BugsnagUnity.Bugsnag.SetApplicationState

Environment

  • Bugsnag version: 7.5.2
  • Unity version: 2022.3.6f1
  • iOS/Android/macOS/Windows/browser version: Windows 10
  • simulator/emulator or physical device: Windows client
  • Initializing bugsnag via a Unity GameObject or in code?: Code with backing config scriptable object
  • Player Settings:
    • Scripting backend (Mono or IL2CPP): IL2CPP
    • API compatibility level for .NET: .NET Standard 2.1
    • Stack Trace level for all error types (None/ScriptOnly/Full): Full
Error messages:
NullReferenceException: Object reference not set to an instance of an object
  at BugsnagUnity.Bugsnag.SetApplicationState (System.Boolean inFocus) [0x00005] in <de0ea49b4c10449e892e594537b56279>:0 
  at BugsnagUnity.TimingTrackerBehaviour.OnApplicationFocus (System.Boolean hasFocus) [0x00000] in <de0ea49b4c10449e892e594537b56279>:0 
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
BugsnagUnity.BugsnagLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:CallOverridenDebugHandler(Exception, Object)
UnityEngine.SystemInfo:GetDeviceModel()
UnityEngine.SystemInfo:get_deviceModel()
BugsnagUnity.Payload.Device:.ctor(Configuration, String)
BugsnagUnity.SessionTracker:StartManagedSession()
BugsnagUnity.SessionTracker:StartSession()
BugsnagUnity.Client:StartInitialSession()
BugsnagUnity.Client:.ctor(INativeClient)
BugsnagUnity.Bugsnag:Start(Configuration)
... <redacted app frames>

The stack shows that the client was still being constructed (but was past the point of enabling BugsnagUnity.TimingTrackerBehaviour) at the time that Unity broadcast an OnApplicationFocus method, and BugsnagUnity.TimingTrackerBehaviour.OnApplicationFocus ultimately depends on the client being fully constructed.

@clr182
Copy link
Contributor

clr182 commented Apr 4, 2024

Hi @uncapped-shattuck

Thanks for reaching out and highlighting this issue. We do now have an item on our backlog aimed at investigating this issue further when priority allows.

Once we have more information to share we will be sure to follow up and inform you of any updates.

@clr182 clr182 added the backlog We hope to fix this feature/bug in the future label Apr 4, 2024
@mclack
Copy link

mclack commented Jul 9, 2024

Hi @uncapped-shattuck

We have now implemented a fix for this in the v8.0.0 release of bugsnag-unity: https://github.com/bugsnag/bugsnag-unity/releases/tag/v8.0.0

Please let us know if you notice any other issues after upgrading to this version.

@mclack mclack closed this as completed Jul 9, 2024
@mclack mclack added released This feature/bug fix has been released and removed backlog We hope to fix this feature/bug in the future labels Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This feature/bug fix has been released
Projects
None yet
Development

No branches or pull requests

3 participants