Skip to content
localwork
Go back

The Bug That Made My Mac App Use an Entire CPU Core While Doing Nothing

The second item on the wish list I wrote for Local Transcriber was “Quiet when idle”. I wanted an app I could leave open all day without thinking about it, busy while it transcribed and practically silent the rest of the time.

On 22 October I was scrolling through a long YouTube transcript in the development build, and it was laggy. The app’s CPU reading went to about 99.4%, and the CPU temperature went from about 34°C to 51°C. When I stopped scrolling, the load came back down. Idle, I’d seen about 0.3%. So idle was fine and scrolling was expensive. That was the story for about a day, and the first half of it was wrong.

It looked like a scrolling problem

That evening I asked Claude Code for a read-only look at the transcript view, with my numbers and a list of suspects: text layout, Markdown parsing, unnecessary view updates, views that weren’t lazy, work on the main thread. The answer was careful and mostly about rows: every line of the transcript was a fairly heavy row on macOS, with selectable text, a tooltip and a tap gesture, measured again as it scrolled into view.

All plausible. But one line in the report rested on my number: idle CPU is near zero, so nothing runs when nobody is scrolling. I’d told it idle was fine, and it reasoned from there. So had I.

A profiling run in Instruments showed the main thread taking about 99% of the sampled CPU time, with about 2 million SwiftUI updates in 11.5 seconds. That was a lot of work but not an explanation, and I said so in the next brief: excessive work, cause not established.

The CPU stayed up between scrolls

The second session started at 03:39 on 24 October. This time I asked for measurements, the same build and conditions before and after any change, and the row theories treated as hypotheses.

The agent built a long test transcript, an hour and 43 minutes of real recognition repeated, and scrolled through it while recording a trace. The CPU sat at about 100% for the whole 40 seconds, including the pauses between scroll steps, when nothing on screen was moving.

So it stopped scrolling. Freshly launched, on the empty New Transcription screen, with no transcript and no profiler attached: 98 to 99%. Then it brought Finder to the front, and the same process dropped to 0.0%. Whatever was using the CPU only did it while the window was in front.

That explained my 0.3% as well. To read a CPU number you look at something that shows CPU numbers, and that becomes the app in front. The agent’s reading was that I’d seen the idle figure with another app frontmost, and that the load fell when I switched away, not when I stopped scrolling. That’s an inference, not something I recorded, but it fits every number I had.

Activity Monitor and top count CPU per core, so 100% means one core kept fully busy, and a Mac with several cores can go well past 100% in total. Local Transcriber was keeping one core permanently busy to show a window with a drop zone in it, and its second requirement was to be quiet when idle.

A loop through the menu bar

The trace showed the window being redrawn on every display frame, and on every pass the app also rebuilt its main menu. Why would an idle window touch the menu bar?

A Mac app has one menu bar, but some commands belong to the window in front. In Local Transcriber, Paste adds a copied link or media file to that window, Refresh Transcripts reloads its library, and the Transcript menu acts on the transcript open in it. In SwiftUI a window offers these with .focusedSceneValue, and the app reads them with @FocusedValue to build the menu. Only the frontmost window’s values count.

This is what the main view, ContentView, published:

.focusedSceneValue(\.pasteLinks, pasteFromClipboard)
.focusedSceneValue(\.refreshTranscripts) { refreshLibrary() }
.focusedSceneValue(\.libraryCommands, LibraryCommands(
    selected: selectedTranscript,
    canCreateFolder: destination.isReady,
    request: handle))

Each of the three is or contains a closure, a piece of code to run later. These lines run every time SwiftUI evaluates the view, and every time they create new closures. Swift closures can’t be compared for equality, and in this app each new one was treated as different from the last. As far as the menu could tell, the window’s commands kept changing.

The agent’s hypothesis was a circle:

  1. ContentView runs and publishes new closures.
  2. The menu commands count as changed, so the app rebuilds the main menu.
  3. That update reaches the window’s root view and its environment.
  4. Which invalidates ContentView, and it runs again.

Once around per frame, and only for the frontmost window, because that’s the only one whose focused values exist. Put Finder in front and the circle has nothing to go around.

Putting one closure back

It was still a hypothesis, and when I authorized the fix at 04:13 I put that in capitals in the brief: “THIS IS A HYPOTHESIS, NOT A CONFIRMED ROOT CAUSE”. I wanted a diagnostic change first.

There were two diagnostic builds. The first removed all three .focusedSceneValue lines. Idle CPU with the window in front fell to 0.1%, and the samples no longer showed the view or the menu being rebuilt. The second build put back only the Paste closure, and CPU went back up to 85.5%.

The first build only showed that the problem was somewhere in the code that had been removed. The second showed which piece, and that a single closure-valued focused value was enough to keep the loop going.

The fix kept the closures and changed what counts as equal:

struct WindowAction: Equatable {
    let window: UUID
    let perform: () -> Void

    func callAsFunction() { perform() }

    static func == (a: Self, b: Self) -> Bool { a.window == b.window }
}

Each window gets its own ID, and two actions are equal when they come from the same window, whatever closure they carry. The Transcript menu’s value compares the window, the selected transcript and whether a new folder is possible, which is everything that menu shows. An unchanged window now publishes the same value as last time, so the menu has no reason to rebuild.

The catch is that the menu now keeps the first closure a window gave it and ignores the newer ones. The agent’s notes said that was fine, because an old closure still reaches the window’s state through its @State storage. That turned out to be only partly true.

Before and after

Window in front · nothing happening · CPU of the app

New Transcription, nothing open

Before86.5%
After0.0%

A long transcript open

Before99.2%
After0.3%

Bars are drawn to scale. The full track is 100%, one CPU core kept busy. Debug build, measured with top over about eight seconds. This is a share of one core, not of the whole machine.

A 3-second SwiftUI trace of the idle window held about 6 million update events before the fix and none after. That’s one short recording of a window doing nothing, but the loop that filled the earlier one was gone. The scroll sequence that had started all this, on the same test transcript, took 47.1 CPU-seconds before the fix and 1.51 after. Most of what I’d taken for a scrolling problem was the window being busy all the time. What the rows themselves cost is still open, and nobody measured real trackpad scrolling, only scroll steps driven by an automation tool.

The first fix report ticked five of its six acceptance criteria. The open one was whether Paste and Refresh Transcripts were still greyed out with no transcript window in front, and I asked for that before anything was committed. The menu states matched the unchanged build. At 04:45 I accepted it and the fix was committed.

Later that day I went back over the task. Two criteria had been ticked without being fully tested: queuing a real transcription through Paste, and the guard that keeps you from losing a recording when you close the window. The commit doesn’t touch that guard, but nobody had run it again either. Both went back to unticked, with the gaps written next to them.

The fix broke Paste

The next day, during benchmark work, the footer language was switched from Russian to English, and a file pasted with ⌘V was transcribed as Russian. The same file added through Choose files came out in English. Five times out of five.

It was that note about @State. The menu was holding a Paste action from an older copy of ContentView. @State really does read live through an old copy, but the language comes from @AppStorage, and in an old copy it keeps the value from before the switch.

The second fix was small: the action now also compares the selected language, so changing it publishes one new action, while a window where nothing changed still publishes an equal one. After that, ⌘V followed the footer in both directions, and idle CPU with the window in front stayed at 0.0%.

Still quiet

The fix shipped in v0.8.0 on 25 October, and the signed Release build read 0.0% with its window in front. Nobody measured a Release build before the fix, or checked older builds with the window in front, although the Paste closure had been there since YouTube support arrived on 1 October. The way I’d been checking idle CPU wouldn’t have caught it anyway.

I’d pointed the agent and myself at the transcript view. The cause turned out to be in the code that decides whether Paste is greyed out.

Since then, CPU with the window in front and nothing happening has been a line in the checks for the features and releases that followed. The one for v0.9.0 reads 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0%. That’s item 02 on the wish list, ten seconds at a time.


Share this post:

Previous Post
I Wanted a Local Transcription App, So I Started Building One
Next Post
The Friction I Didn't Notice Until I Bought a MacBook