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

NSAlert.runModal causes App Hanging report

Open sindresorhus opened this issue 2 years ago • 26 comments

Platform

macOS

Installed

Swift Package Manager

Version

8.0.0

Steps to Reproduce

  • Call NSAlert#runModal()

Expected Result

While calling NSAlert#runModal() does block the main thread, it's not a useful hanging report as calling it is intentional.

Actual Result

It causes "App hanging for at least 2000 ms." report.

Example: https://sentry.io/organizations/sindresorhus/issues/3891646204/events/de1b4f59dea84b9a850ae44232603b16/

Are you willing to submit a PR?

No

sindresorhus avatar Jan 23 '23 10:01 sindresorhus

Unrelated, but the report also has this warning:

There was 1 problem processing this event threads.values.14.stacktrace.frames: Missing value for required attribute

sindresorhus avatar Jan 23 '23 10:01 sindresorhus

Thanks @sindresorhus. This is an interesting use case for App Hangs.

brustolin avatar Jan 23 '23 10:01 brustolin

@sindresorhus, I just tried adding the following code to our macOS sample app and couldn't reproduce the issue.

let alert = NSAlert()
alert.messageText = "This is an alert!"
alert.informativeText = "You can close it."
alert.addButton(withTitle: "Close")
let response = alert.runModal()

if (response == .OK) {
    //
}

Please share how you call your NSAlert to us so we can reproduce the issue.

philipphofmann avatar Jan 23 '23 14:01 philipphofmann

That's weird. It's basically the same.

Here's the exact code being called that causes the report:

https://github.com/sindresorhus/Plash/blob/7df0b3cbed4e74df2395d7dd6bd6afeed6692f86/Plash/WelcomeScreen.swift#L11-L25

https://github.com/sindresorhus/Plash/blob/7df0b3cbed4e74df2395d7dd6bd6afeed6692f86/Plash/Utilities.swift#L1209-L1226

sindresorhus avatar Jan 23 '23 15:01 sindresorhus

Completely unrelated, but you may want to participate in this discussion: https://forums.swift.org/t/pitch-swift-backtracing-api/62741

sindresorhus avatar Jan 23 '23 15:01 sindresorhus

I thought I had it but was mistaken, I can't reproduce either.

brustolin avatar Jan 23 '23 15:01 brustolin

I could reproduce the issue by following these steps

  1. Clone https://github.com/sindresorhus/Plash
  2. Apply the patch below
  3. Start the app
  4. Wait for around 10 seconds
diff --git a/Plash/Utilities.swift b/Plash/Utilities.swift
index a3eff1e..5857967 100644
--- a/Plash/Utilities.swift
+++ b/Plash/Utilities.swift
@@ -455,12 +455,11 @@ extension SSApp {
 	Initialize Sentry.
 	*/
 	static func initSentry(_ dsn: String) {
-		#if !DEBUG && canImport(Sentry)
 		SentrySDK.start {
 			$0.dsn = dsn
 			$0.enableSwizzling = false
+			$0.debug = true
 		}
-		#endif
 	}
 }
 
diff --git a/Plash/WelcomeScreen.swift b/Plash/WelcomeScreen.swift
index 2de9373..f54335c 100644
--- a/Plash/WelcomeScreen.swift
+++ b/Plash/WelcomeScreen.swift
@@ -2,9 +2,9 @@ import Cocoa
 
 extension AppState {
 	func showWelcomeScreenIfNeeded() {
-		guard SSApp.isFirstLaunch else {
-			return
-		}
+//		guard SSApp.isFirstLaunch else {
+////			return
+//		}
 
 		NSApp.activate(ignoringOtherApps: true)
 

philipphofmann avatar Jan 25 '23 09:01 philipphofmann

Tried Sentry v8 in another app and it seems there are many other things that triggers false-positives for this feature:

  • Internal AppKit API: https://sindresorhus.sentry.io/issues/3936549454/events/8a09c43e59ca4d34aa2241127596b2d8/
  • [NSStatusBarButtonCell performClick:] https://sindresorhus.sentry.io/issues/3936549602/events/947e78778ff54c46add5f1ddd63086bf/
    • My guess would be because it opens a NSMenu which has a tracking mode runloop.

sindresorhus avatar Feb 14 '23 17:02 sindresorhus

Thanks for the update, @sindresorhus. Now we know this doesn't only happen for NSAlert#runModal() so we will reevaluate our priority.

philipphofmann avatar Feb 16 '23 14:02 philipphofmann

@philipphofmann we started getting these false positive app hanging errors with iOS, tvOS and Mac Catalyst apps as well fwiw.

gabors avatar Feb 26 '23 20:02 gabors

Hello @gabors. Can you share what the app was doing when you received this false positive errors?

brustolin avatar Feb 27 '23 09:02 brustolin

getting same error

App Hanging -[REANodesManager performOperations]
App hanging for at least 2000 ms.

numandev1 avatar Mar 20 '23 10:03 numandev1

@numandev1, on which platform do you get the error?

philipphofmann avatar Mar 20 '23 10:03 philipphofmann

@philipphofmann I am getting on IOS here is the sentry report of this error https://ananinja.sentry.io/share/issue/16baedb7fcbc4594b4fba363ddee149e/

image

numandev1 avatar Mar 20 '23 11:03 numandev1

@numandev1 Why do you believe this is an error? It looks like your app was waiting for a semaphore to be release and may have take more than 2 seconds, hence the App Hanging report.

brustolin avatar Mar 20 '23 12:03 brustolin

Is there some way to entirely disable the "App hanging for at least 2000 ms" reports? I'm finding them really unhelpful.

zcohan avatar Mar 21 '23 11:03 zcohan

@zcohan, yes from our docs

options.enableAppHangTracking = false

@zcohan, can you elaborate on why you find them really unhelpful, so we can improve them?

philipphofmann avatar Mar 21 '23 16:03 philipphofmann

Hi @philipphofmann!

We also tried app hangs monitoring, and also seeing false positives with NSApp.presentError / runModal, as well as possibly some other false positives.

However, those are not the biggest problem, which is App Hangs are not grouped by stack trace. Therefore it's impossible to filter them / analyze / close as false-positive, because in every App Hang issue there are a lot of different events with different stack traces.

If app hangs would be grouped by stack trace, false-positives with NSApp would not be an issue, since you could just ignore them, which is currently not possible.

DenTelezhkin avatar Mar 21 '23 16:03 DenTelezhkin

@zcohan, yes from our docs

options.enableAppHangTracking = false

@zcohan, can you elaborate on why you find them really unhelpful, so we can improve them?

Thanks a lot @philipphofmann.

Well, mainly because I'm getting lots of such reports and so far there hasn't been a case that I think I can actually do something about, so it's noise. Most of the time these hangs are being caused by AppKit/macOS itself, not my own code.

zcohan avatar Mar 21 '23 17:03 zcohan

We have a GH discussion https://github.com/getsentry/sentry-cocoa/discussions/2715 for tracking false app hang positives. Let's keep this issue focused on NSAlert.runModal

philipphofmann avatar Mar 29 '23 12:03 philipphofmann

We're getting a bunch of these as well in our macOS app for NSAlert.runModal and other AppKit APIs that we don't control. We're going to have to disable the app hanging detection for now, which I hate to do since I'm sure there are some real issues in the noise. A quick look at all the issues though have this chunk in common, seemingly whenever AppKit starts an event tracking loop internally, wondering if it would be enough to filter out backtraces with that?

Screenshot 2023-05-22 at 11 49 21 AM

https://developer.apple.com/documentation/appkit/nsapplication/1428485-nexteventmatchingmask

zachwaugh avatar May 22 '23 15:05 zachwaugh

Thanks for the info, @zachwaugh.

philipphofmann avatar May 23 '23 15:05 philipphofmann

Have this warning for UIApplication.shared.open(url) line, not sure what to do about this rather than disabling this app hanging warnings. 🤔

akbashev avatar Jul 17 '23 10:07 akbashev

Yes, @akbashev. Not much you can do than turn this off until we find a fix for the root cause.

philipphofmann avatar Jul 18 '23 08:07 philipphofmann

Just a tip. If you have a runModal() inside an async function you might indeed block the concurrency executor and you should migrate away from runModal() anyway.

eaigner avatar Jun 28 '24 21:06 eaigner

@eaigner Thanks for the tip.

krystofwoldrich avatar Jul 02 '24 14:07 krystofwoldrich

It's unlikely that we can fix the root cause, but 8.30.0 we added an API to pause/resume App Hang tracking, which can be used to mitigate this problem

kahest avatar Mar 10 '25 17:03 kahest