Users of a note-taking phone app report freezes and the app vanishing, yet no crash reports arrive; how should the app detect and report hangs and OS kills?
answer
- no exception, no handler
- a kill cannot be caught
- a second thread watching the first
- sample the stack while it lasts
- markers read at the next launch
basics
~20 sA watchdog thread checks that the main thread answers within a chosen threshold and, if not, saves its stack to disk while the hang lasts. Markers left on disk, plus any exit reason the OS records, reveal kills that left no report.
solid answer
~50 sA hang is the main thread not answering input. Nothing is thrown, and if the OS or the user ends the app, the process is killed with a signal it cannot catch, so no crash handler runs. So the app watches itself: a **watchdog thread** posts a tiny task to the main thread and, if it has not run within the app's threshold, samples the main thread's stack repeatedly and writes a hang record to disk as it goes. If the main thread recovers, the record becomes a hang report with its duration. The app also keeps **markers** — 'in the foreground' and 'hang in progress' — and at the next launch turns a leftover marker with no crash report into a 'killed while hung' or 'ended unexpectedly' report, attaching the OS's recorded exit reason where one exists. Thresholds differ by platform, so the app's own is a stated choice.
code
pseudocode · 23 lineshang_threshold = 2.0 # seconds, assumption
check_interval = 0.5
sample_interval = 0.25
watchdog_thread:
loop while app_in_foreground:
posted_at = monotonic_now()
ran_at = none
post_to_main_thread(() => ran_at = monotonic_now())
sleep(check_interval)
if monotonic_now() - posted_at > check_interval + 1.0:
continue # the watchdog itself was suspended; not a hang
if ran_at != none:
continue # main thread answered
wait_until(ran_at != none or monotonic_now() - posted_at >= hang_threshold)
if ran_at != none:
continue # answered within the threshold
record = disk.create("hang_in_progress", breadcrumbs, custom_keys)
while ran_at == none:
record.append(sample_stack(main_thread)) # written at once; a kill may follow
sleep(sample_interval)
record.finish(duration = ran_at - posted_at) # recovered: becomes a hang report
upload_queue.add(record)go deeper
Recall that a hang is the main thread not answering input, and that a frozen app killed from outside never reaches its crash handler.
Explain the watchdog thread, why it samples repeatedly and writes to disk at once, and why each platform's hang threshold differs.
Show the next-launch logic: markers, the OS's exit reason where it exists, the unexplained-exit rate per version, and the suspended-process and debugger false positives.
Decide which hang threshold and which exit categories every team reports, and how unexplained exits count in release health when their cause cannot be proven.
## What a hang is A **hang** is the app's **main thread** — the single thread that handles input and updates the screen — failing to respond for long enough that a person notices. Nothing is thrown; the thread is busy or blocked: a very large note parsed on the main thread, a synchronous database migration, a lock held by the sync thread, a network call made synchronously. Slow or dropped frames during scrolling are a related but separate problem, measured per frame; a hang is the app not answering at all. Each platform draws its own line: - on one platform, an app that has not responded to an input event within **5 seconds** is declared not responding, and a foreground app's user is offered a dialog to close it; - on the other, the vendor's tools start reporting when the main thread is unresponsive for more than **250 ms**, a delay over **100 ms** in a discrete interaction is described as noticeable, and an OS watchdog terminates an app that takes too long to launch or stops updating its screen. So the app's own threshold is a design choice — this scenario assumes 2 seconds for a reported hang — and never a universal number. ## Why hangs leave no crash report A crash handler runs because the process received an exception or a catchable signal. A hang ends in one of two ways, and neither runs it: - the main thread **recovers**, and nothing is recorded at all; - the process is **killed** — by the OS watchdog, or by a user closing the frozen app — with a kill signal that cannot be caught, so no handler runs and no file is written. The crash-free rate looks healthy while users stare at a frozen editor. On one platform, the OS's own record of a watchdog termination does not even appear in the store's list of crash reports. The app has to watch for hangs itself. ## A watchdog on its own thread 1. A background **watchdog thread** wakes on a short interval and posts a tiny task to the main thread that records the time it ran. 2. If the task has not run within the threshold, the main thread is hung. The watchdog **samples the main thread's stack**, and keeps sampling at intervals while the hang lasts. 3. It **writes a hang-in-progress record to disk at once**, with the samples, breadcrumbs and custom keys, and appends samples as they arrive: the process may be killed at any moment. 4. If the main thread recovers, the record becomes a **hang report** with its duration, queued for upload. 5. If the process dies, the record is still on disk for the next launch. Repeated sampling matters. The stack at the moment of a kill shows what the thread was doing then, not what consumed the time: work that took four seconds of a five-second allowance and then finished does not appear in the final stack. Grouping hangs by the frames most common across the samples is more honest than trusting the last one. ## Finding the kill on the next launch The app keeps two small **markers** on disk: *in the foreground*, set when the app comes to the foreground and cleared when it leaves, and *hang in progress*, set by the watchdog and cleared when the main thread recovers. At launch, before anything else, it reads them: | Left by the last run | Crash report waiting? | Conclusion | |---|---|---| | no marker | no | clean: left the foreground, later ended by the OS as usual | | either | yes | a crash, already recorded | | foreground and hang | no | killed while hung, by the OS or by the user | | foreground only | no | ended unexpectedly: a memory kill, another uncatchable kill, or a crash the reporter missed | Where the OS records why the previous process ended — one platform reports reasons such as a crash, not responding, low memory, excessive CPU use or a user request — the app reads it at the same moment and attaches it. Where it does not, the markers are the evidence. A rise in *ended unexpectedly* on a new version is a signal even before its cause is known; if memory turns out to be the cause, reducing it is memory work. ## Pitfalls - **Suspended processes.** When the app is suspended in the background, the watchdog is suspended too, and on resume the clock has jumped. Measure with a monotonic clock, and discard a check when the watchdog's own wake-up was late. - **Debuggers** pause the main thread; turn the watchdog off under one. - **Cost.** Sampling stacks is cheap but not free; sample only after a hang has begun, and stop when it ends. - **Scope.** Watch only the main thread, and only in the foreground; long background work is not a hang.
- Why does the stack captured at the moment of a kill often blame the wrong code?The kill comes at the end of the allowance and records whatever the thread is doing then. Work that used most of the time may already have finished and left no frame. Sampling from the start of the hang and grouping by the frames that appear most often across samples points at the code that actually held the thread.
- How do you keep the watchdog from reporting hangs that never happened?Run it only while the app is in the foreground, turn it off under a debugger, and use a monotonic clock. When the watchdog's own wake-up arrives much later than scheduled, the whole process was suspended, so that check is discarded rather than counted as a hang.
- A leftover foreground marker could also mean a memory kill; how do you tell them apart?The hang marker separates a kill during a hang from other exits. Where the OS records an exit reason, it names low memory directly; elsewhere, memory warnings in the breadcrumbs and low free memory in the last state point that way. When the evidence is thin, report the exit as unexplained rather than guessing.
- A frozen app throws an exception, so the crash reporter will catch it.
- The app's crash handler runs when the OS kills it for not responding.
- A hang means five seconds without a response on every phone.
- The stack at the moment of the kill always shows the slow code.
- No crash reports from a version means its users had no failures.