sentry-unity icon indicating copy to clipboard operation
sentry-unity copied to clipboard

automatic screenshot feature creates an exception

Open johnfoconnor opened this issue 2 years ago • 5 comments

Environment

How do you use Sentry? Sentry SaaS (sentry.io

Which version of the SDK? 0.22.2 How did you install the package? Git-URL

Which version of Unity? 2021.3.5f1

Is this happening in Unity (editor) or on a player like Android, iOS, Windows? Editor (only tested in editor)

Steps to Reproduce

  1. Enable automatic screenshot capturing
  2. "Play" my unity scene
  3. Sentry detects a Sentry: (Info) Detected an ANR event: Application not responding for at least 5000 ms.

Expected Result

Sentry should either

A) correctly capture the screenshot B) not try to capture a screenshot if it cant (e.g. if it's not on the main thread)

Actual Result

Sentry produces an exception and fails to capture a screenshot

Sentry: (Error) Failed to add attachment: screenshot.jpg. System.Exception: Sentry: cannot capture screenshot attachment on other than the main (UI) thread.
  at Sentry.Unity.ScreenshotAttachmentContent.GetStream () [0x0002d] in /sentry-unity/src/Sentry.Unity/ScreenshotAttachment.cs:38 
  at Sentry.Protocol.Envelopes.EnvelopeItem.FromAttachment (Sentry.Attachment attachment) [0x00000] in /sentry-unity/src/sentry-dotnet/src/Sentry/Envelopes/EnvelopeItem.cs:264 
  at Sentry.Protocol.Envelopes.Envelope.FromEvent (Sentry.SentryEvent event, Sentry.Extensibility.IDiagnosticLogger logger, System.Collections.Generic.IReadOnlyCollection`1[T] attachments, Sentry.SessionUpdate sessionUpdate) [0x00036] in /sentry-unity/src/sentry-dotnet/src/Sentry/Envelopes/Envelope.cs:153 
UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
Sentry.Unity.Integrations.UnityLogHandlerIntegration:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[]) (at /sentry-unity/src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs:80)
UnityEngine.Debug:LogError (object)
Sentry.Unity.UnityLogger:Log (Sentry.SentryLevel,string,System.Exception,object[]) (at /sentry-unity/src/Sentry.Unity/UnityLogger.cs:47)
Sentry.Extensibility.DiagnosticLoggerExtensions:LogIfEnabled<string> (Sentry.Extensibility.IDiagnosticLogger,Sentry.SentryLevel,System.Exc 

Any logs or screenshots

johnfoconnor avatar Aug 03 '22 22:08 johnfoconnor

Thanks for letting us know of this issue. Indeed sounds like a bug, we'll find a fix to it.

bruno-garcia avatar Aug 03 '22 22:08 bruno-garcia

The exception you see was and seemingly still is the only way we can indicate to sentry-dotnet SDK that the preconfigured screenshot attachment cannot be produced for the current event. The error is only printed to logs and not sent to Sentry. https://github.com/getsentry/sentry-unity/blob/37d60f6f3100bd640cd4ed5d4df946cfaba9a2ef/src/Sentry.Unity/ScreenshotAttachment.cs#L32-L38

https://github.com/getsentry/sentry-dotnet/blob/ae2b7c06dc9b4d27a4334b1538a234405b6940ce/src/Sentry/Envelopes/EnvelopeItem.cs#L264

https://github.com/getsentry/sentry-dotnet/blob/94e4e8ee4a1c89811d0aad7cdc645b00b11ea75d/src/Sentry/Envelopes/Envelope.cs#L181-L188

vaind avatar Aug 04 '22 08:08 vaind

As an end user, I never want Sentry to emit an error of exception unless there's an actual problem. Automatic screenshots not working in certain situations seems like at most a warning, or maybe even a breadcrumb or extra field in the event itself. My application shouldn't start having errors because of this situation. I don't want to see sentry error logs polluting my application unless it's actual an error with sentry reporting things

On Thu, Aug 4, 2022, 1:03 AM Ivan Dlugos @.***> wrote:

The exception you see was and seemingly still is the only way we can indicate to sentry-dotnet SDK that the preconfigured screenshot attachment cannot be produced for the current event. The error is only printed to logs and not sent to Sentry.

https://github.com/getsentry/sentry-unity/blob/37d60f6f3100bd640cd4ed5d4df946cfaba9a2ef/src/Sentry.Unity/ScreenshotAttachment.cs#L32-L38

https://github.com/getsentry/sentry-dotnet/blob/ae2b7c06dc9b4d27a4334b1538a234405b6940ce/src/Sentry/Envelopes/EnvelopeItem.cs#L264

https://github.com/getsentry/sentry-dotnet/blob/94e4e8ee4a1c89811d0aad7cdc645b00b11ea75d/src/Sentry/Envelopes/Envelope.cs#L181-L188

— Reply to this email directly, view it on GitHub https://github.com/getsentry/sentry-unity/issues/925#issuecomment-1204906903, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN3LMPA4S5S2QNEUQV3Y6TVXN2MZANCNFSM55QM5OJQ . You are receiving this because you authored the thread.Message ID: @.***>

johnfoconnor avatar Aug 04 '22 14:08 johnfoconnor

I tend to agree this should just be a warning level log and no exception. Can we just skip the screenshot in this case and let it continue?

bruno-garcia avatar Aug 05 '22 14:08 bruno-garcia

Can we just skip the screenshot in this case and let it continue?

that is what happens in the end, it just logs as an error: https://github.com/getsentry/sentry-dotnet/blob/94e4e8ee4a1c89811d0aad7cdc645b00b11ea75d/src/Sentry/Envelopes/Envelope.cs#L187

I'd suggest recognizing an empty stream returned from this call and skipping the attachment in that case. Maybe we could even add a custom SkipAttachmentStream to sentry-dotnet just for that purpose. Otherwise, we can check if the stream is empty.

@mattjohnsonpint

vaind avatar Aug 05 '22 15:08 vaind

Im seeing this still happen on 0.22.2 where it produces an error.

Sentry: (Error) Failed to add attachment: screenshot.jpg. System.Exception: Sentry: cannot capture screenshot attachment on other than the main (UI) thread.
  at Sentry.Unity.ScreenshotAttachmentContent.GetStream () [0x0002d] in /sentry-unity/src/Sentry.Unity/ScreenshotAttachment.cs:37 
  at Sentry.Protocol.Envelopes.EnvelopeItem.FromAttachment (Sentry.Attachment attachment) [0x00000] in /sentry-unity/src/sentry-dotnet/src/Sentry/Envelopes/EnvelopeItem.cs:264 
  at Sentry.Protocol.Envelopes.Envelope.FromEvent (Sentry.SentryEvent event, Sentry.Extensibility.IDiagnosticLogger logger, System.Collections.Generic.IReadOnlyCollection`1[T] attachments, Sentry.SessionUpdate sessionUpdate) [0x00036] in /sentry-unity/src/sentry-dotnet/src/Sentry/Envelopes/Envelope.cs:183 
UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
Sentry.Unity.Integrations.UnityLogHandlerIntegration:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[]) (at /sentry-unity/src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs:80)
UnityEngine.Debug:LogError (object)
Sentry.Unity.UnityLogger:Log (Sentry.SentryLevel,string,System.Exception,object[]) (at /sentry-unity/src/Sentry.Unity/UnityLogger.cs:47)
Sentry.Extensibility.DiagnosticLoggerExtensions:LogIfEnabled<string> (Sentry.Extensibility.IDiagnosticLogger,Sentry.SentryLevel,System.Exception,string,string) (at /sentry-unity/src/sentry-dotnet/src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs:377)
Sentry.Extensibility.DiagnosticLoggerExtensions:LogError<string> (Sentry.Extensibility.IDiagnosticLogger,string,System.Exception,string) (at /sentry-unity/src/sentry-dotnet/src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs:236)
Sentry.Protocol.Envelopes.Envelope:FromEvent (Sentry.SentryEvent,Sentry.Extensibility.IDiagnosticLogger,System.Collections.Generic.IReadOnlyCollection`1<Sentry.Attachment>,Sentry.SessionUpdate) (at /sentry-unity/src/sentry-dotnet/src/Sentry/Envelopes/Envelope.cs:187)
Sentry.SentryClient:DoSendEvent (Sentry.SentryEvent,Sentry.Scope) (at /sentry-unity/src/sentry-dotnet/src/Sentry/SentryClient.cs:218)
Sentry.SentryClient:CaptureEvent (Sentry.SentryEvent,Sentry.Scope) (at /sentry-unity/src/sentry-dotnet/src/Sentry/SentryClient.cs:71)
Sentry.Internal.Hub:CaptureEvent (Sentry.SentryEvent,Sentry.Scope) (at /sentry-unity/src/sentry-dotnet/src/Sentry/Internal/Hub.cs:318)
Sentry.SentryClientExtensions:CaptureException (Sentry.ISentryClient,System.Exception) (at /sentry-unity/src/sentry-dotnet/src/Sentry/SentryClientExtensions.cs:20)
Sentry.Unity.AnrIntegration/<>c__DisplayClass4_0:<Register>b__0 (object,Sentry.Unity.ApplicationNotResponding) (at /sentry-unity/src/Sentry.Unity/Integrations/AnrIntegration.cs:39)
Sentry.Unity.AnrWatchDog:Report () (at /sentry-unity/src/Sentry.Unity/Integrations/AnrIntegration.cs:76)
Sentry.Unity.AnrWatchDogMultiThreaded:Run () (at /sentry-unity/src/Sentry.Unity/Integrations/AnrIntegration.cs:146)
System.Threading.ThreadHelper:ThreadStart ()

johnfoconnor avatar Aug 26 '22 00:08 johnfoconnor

@bitsandfoxes You mentioned you were looking into this today, any updates?

bruno-garcia avatar Aug 29 '22 16:08 bruno-garcia

Im seeing this still happen on 0.22.2 where it produces an error.

I'm reasonably sure that this is not coming from 0.22.2. The error message from your logs is outdated; currently (with 0.22.2), we simply debug log here.

I gave it a try with simulating an ANR event and the result is an ANR event in Sentry (without a screenshot) and the following logs in the console:

Sentry: (Debug) Can't capture screenshots on other than main (UI) thread. 
Sentry: (Warning) Did not add 'screenshot.jpg' to envelope because the stream was empty.  

bitsandfoxes avatar Aug 30 '22 08:08 bitsandfoxes