Make UI hang detector activation and teardown lifetime-safe #99
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #99"
general
weight: 0.35
fuzzybinary
Description
Make
UiHangDetectoractivation transactional and its teardown lifetime-safe: a hook notification never reaches a partially initialized or destroyed detector, andStopnever holds_instanceMutexwhile it joins the watchdog or removes the hook._instanceor creates the stop event._instanceand_instanceMutexare now private.MonitorWindowHangsruns the activation stages in order and sets_instancelast. A failed stage or an exception releases the earlier stages, and the same detector can be activated again. While another detector is active, activation is rejected before anything is acquired.Stopstops the watchdog, removes the hook, then clears_instance. It is idempotent, final andnoexcept, and the destructor calls it.IUiHangDetectorRuntimeseam. The public constructor uses a file-scope production runtime; the private injected constructor is reserved forUiHangDetectorLifetimeTests. The runtime owns theWH_GETMESSAGEhook: its hook procedure passes each removedMSGto the detector and always callsCallNextHookEx.Activation stages
| Stage | Acquires | Released on a later failure by |
|---|---|---|
| 1 | Stop event |
CloseHandle|| 2 | Window and thread validation:
IsWindow,GetWindowThreadProcessId, same process, monitoredThreadInfo| Clearing_hWndand_pThreadInfo|| 3 | Message hook on the window thread |
RemoveMessageHook|| 4 | Watchdog thread (a null or non-joinable thread is a failure) | Not needed: the last stage cannot fail |
| 5 |
_instance = this| Not needed |The watchdog posts its first probe only once
_instanceis set. A probe processed earlier would never be observed, so the watchdog would report a hang that never ends.Teardown order (
Stop, destructor and activation rollback)1. Request the stop, signal the stop event, then join the watchdog. Hook notifications keep reaching the detector until the watchdog has exited, so a probe in flight is still acknowledged.
2. Remove the hook.
3. Clear
_instanceunder_instanceMutex, only if it points to this detector. The hook callback holds this lock while it calls into the detector, so this waits for a notification that is being processed. Later notifications, which Windows can still deliver after the hook is removed, find no detector.4. Close the stop event, then clear the window and thread references.
Stopalso clears the provider references._instanceMutexis never held while joining or removing the hook. Each step is skipped when already done, which makesStopand destruction idempotent. Only one detector is active at a time, andMonitorWindowHangsandStopcalls must not overlap, on any detector;Profilerguarantees this with its lifecycle mutex.Motivation
Previously:
_thisbefore the event, hook or watchdog existed._thispublished and kept partial state. For example,_hWndstayed set when the window thread was not monitored, so a retry was rejected as a second window.MonitorWindowHangsinstead of failing cleanly.Stopheld_instanceMutexwhile it joined the watchdog, closed the event and unhooked, and_thiswas cleared unconditionally.Testing
Local results at 865b6fa: Debug and Release with the Visual Studio generator (MSVC 19.35), and an ASan Debug build with Ninja (MSVC 14.51) and the CI
ASAN_OPTIONS:Tests.exe --gtest_filter=UiHangDetectorLifetimeTests.*passes 26/26. Shuffled repeats also pass: 40 in Debug and 20 in Release.Tests.exepasses 263/263 in Debug, Release and under ASan.test_ui_hangs.ps1passes 23/23 checks with both-Config Debugand-Config Release.check-formatpasses with clang-format 20.1.8.Race and failure tests (
UiHangDetectorLifetimeTests)The tests are deterministic. A fake runtime records each call together with what a hook notification would find at that moment: whether
_instanceMutexis free and which detector_instancepoints to. It also injects failures and parks callers at points chosen by the test. Its clock never advances, and the watchdog only ticks when the test grants a tick._instanceMutex. Nothing is left behind, the providers are kept, and the same detector then activates. A nullThreadList, and a second detector while one is active, are rejected without any runtime call._instanceset. The watchdog runs one loop iteration before_instanceis set without posting a probe, and posts once it is set.Stoporder:SetEvent, watchdog exit,RemoveMessageHook, thenCloseHandle, each with_instanceMutexfree. A message delivered during the hook removal still reaches the live detector.Stopand destruction: while a notification is parked inside the detector, holding_instanceMutex,Stopand destruction stop the watchdog and remove the hook, then wait. The notification completes on the live detector before anything is released. Later notifications never reach the destroyed detector, and ASan covers the use-after-free.Stop: while the watchdog is parked inPostMessageW,Stophas signaled and waits injoin, with_instanceMutexfree and the detector still receiving notifications.Stop,Stopon an unused detector, and destruction afterStopmake no runtime calls. Destroying an active detector runs the full teardown, and a stopped detector refuses to monitor.Checklist
UiHangDetectoris internal, andProfileruses the public constructor. Behavior changes: a stopped detector cannot monitor again, which matches howProfileralready creates one detector perMonitorWindowHangscall; and activation is rejected instead of taking over when another detector is active.https://datadoghq.atlassian.net/browse/PROF-16779
[PROF-16788]: https://datadoghq.atlassian.net/browse/PROF-16788?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ