Disclosure: I'm the developer of Relaymote, and this post is published from my account. It was drafted and published by an AI agent working on my behalf. Relaymote is unofficial; not affiliated with or endorsed by Anthropic.

Relaymote is a small local Node.js daemon that puts Claude Desktop's Code-tab sessions on your phone. In early November, requests from the phone started taking up to 44 seconds. Nothing in the logs could say why.

Why the existing timing could not tell

I already logged a "poll: TOTAL" time. But that number includes every await in the poll, so a poll that is slow because it is waiting looks identical to one that is slow because the event loop is blocked. Those need opposite fixes: wait less, or stop running synchronous work on the main thread. The log could not separate them.

The idea: watch from outside the thing that is stuck

A blocked main thread cannot report on itself. So the main thread does one trivial thing, and a worker thread does the watching:

  1. The main thread writes Date.now() into a SharedArrayBuffer every 100 ms. That is the heartbeat.
  2. A worker thread checks that value every 200 ms. If it is older than STALL_MS (2 s by default), a stall is open.
  3. The worker keeps a rolling CPU profile of the main thread, in 30 s windows, using Node's inspector module: session.connectToMainThread() lets a worker attach to the main thread's inspector without opening a port.
  4. When a stall closes, the worker reads the window's profile for just the stall's time range and logs what was running.
const session = new inspector.Session(); session.connectToMainThread(); await post('Profiler.enable'); await post('Profiler.setSamplingInterval', { interval: 10000 }); // 10 ms for (;;) { const wall0 = Date.now(); await post('Profiler.start'); await sleep(WINDOW_MS); while (open) await sleep(200); // a stall still open ends in the next window const { profile } = await post('Profiler.stop'); for (const s of stalls.splice(0)) report(attribute(profile, wall0, s.t0, s.t1)); } 

One detail that matters: a stall that is still open when the window ends is not cut off. The worker waits for it to close and reads it from the same profile, otherwise the longest stalls, which are the ones you care about, would be the ones you lose.

"readFileSync" is not an answer

A CPU profile's leaf frame is usually something like readFileSync or statSync. That tells you the call, not who made it. So the attribution does two extra things:

  • it walks up from each sample to the nearest frame that belongs to our own code (a path check against the project root) and charges the time to that;
  • it also keeps the chain of our own callers above that frame, up to three, as frame < caller < caller.

The second part came from a real failure: a log line that said only a mtime helper in core.js was hot could not say which caller was stat-ing. With the caller chain, each stall line now names the call path that asked.

The log line for one stall looks like this (the shape is real, the values here are made up):

loop stall 6.2 s from 2026-11-07T10:14:03Z (5900 ms sampled) ours: poll core.js:210 4100ms, ... | leaf: statSync 3900ms, ... | via: mtime core.js:67 < readCached core.js:90 < poll core.js:210 3800ms 

A second job: "blocked" is not "dead"

On 16 November a PC-wide stall lasted 186 seconds. The tray's watchdog saw two missed health probes (about 40 s) and ended the daemon, which cost two restarts and a 72 s boot stall. The daemon had not crashed; it was blocked.

The same worker keeps running while the main thread is blocked, so it can say so. After NOTE_AFTER_MS (5 s) of stall it writes a small JSON file every 5 s:

{ "pid": 4120, "stalledSince": 1763000000000, "at": 1763000012000 } 

The watchdog now treats a fresh note as "blocked, not dead" and waits up to a configured grace period before ending the process. A hang where the worker is also silent is still ended at once. The file is written to a temp name and renamed, and a write that is still in flight is not started twice.

What it costs and how to turn it off

The worker is unref'd so it never keeps the process alive, the sampling interval is 10 ms, and RELAYMOTE_LOOP_WATCH=0 turns the whole thing off. The thresholds are environment variables too.

Takeaways

  • Measure a blocked event loop from another thread; a timer on the blocked thread cannot fire to tell you it is blocked.
  • Total request time that includes awaits cannot separate waiting from blocking. A heartbeat can.
  • Attribute profile samples to your frames and keep a short caller chain, because the leaf is almost always a library call.
  • Give your watchdog a way to tell "blocked" from "dead", or it will kill a process that was about to recover.

Source is MIT-licensed in the repo (lib/loop-watch.js).

Unofficial; not affiliated with or endorsed by Anthropic.