Skip to content

fix(final): stop each final pass from keeping 2.7 GB of models, which filled the disk with swap - #179

Merged
GeiserX merged 2 commits into
mainfrom
fix/final-pass-memory
Sep 29, 2026
Merged

GeiserX merged 2 commits into
mainfrom
fix/final-pass-memory

Conversation

@GeiserX

@GeiserX GeiserX commented Sep 29, 2026 •

Copy link
Copy Markdown
Owner

After its first start, 0.5.0 ran the final pass over four earlier calls whose pass had failed on 0.4.0. All four finished. Two hours later the app's main process had a 10 GB footprint with 49 MB resident, so almost all of it sat in swap. Swap grew until the disk had 0 bytes free. No call was recording. Quitting and reopening akou brought the footprint back to 195 MB.

Cause

Each final pass runs in its own Worker, and finalizeCall terminates that Worker as soon as the pass answers. sherpa-onnx frees Parakeet and Silero only in their finalizers. Bun runs a collected object's finalizer on a later turn of the event loop, and a Worker terminated before that turn never runs it. So every pass left its whole model set in the process, about 2.7 GB with Parakeet fp32. The catch-up at start runs the backlog one call after another, which is how four passes added up to 10 GB.

I measured it on a 16 GB Apple silicon Mac mini with scripts/measure-final-memory.ts. The run creates three ended calls of two 20-minute stereo Opus parts each, written by akou-capture run --from-wav, and runs their final passes back to back through finalizeCall with the real models: Parakeet TDT v3 fp32, Silero, and Nemotron through akou-diarize. After each pass and every minute of a 10-minute idle it records the process footprint (macOS footprint, which counts swapped and compressed memory).

process footprint before after
after pass 1 2800 MiB 89 MiB
after pass 2 5504 MiB 79 MiB
after pass 3 8653 MiB 1224 MiB
after 10 minutes idle 7703 MiB (292 MiB resident) 1065 MiB

To find which part keeps the memory, I ran pieces of the pass in a Worker, terminated it, and read the footprint afterwards:

  • Decoding a call's Opus parts through the helper, the way the pass reads them: 14 MiB, with no growth over three rounds.
  • The Nemotron diarizer on 40 minutes of audio: 13 MiB.
  • The pass with the diarizer stubbed out: 2613 MiB, then 5074 MiB after a second round. That is the leak.
  • The same pass, but the Worker lets go of its models and collects before it ends: 803 MiB, then 68 MiB.

Inside the Worker, the first collection freed under a quarter of the models and the second freed the rest, which is why one Bun.gc(true) was not enough.

The ~1 GB left after the third pass is not live memory. heap finds 6 MB of live malloc allocations in the idle process. The rest is freed memory that the allocator keeps for reuse (736 MB of MALLOC_LARGE, 308 MB of MALLOC_SMALL). A second run with six passes shows it does not grow: 246, 223, 1225, 1270, 1270 and 1196 MiB after passes 1 to 6. Six minutes into the idle that followed, the allocator gave it back and the footprint fell to 126 MiB.

Fix

  • ModelSet.release() lets go of every model the set holds. SherpaModels.release() drops its references and runs four rounds of Bun.gc(true) plus a turn of the event loop, so the finalizers run while the Worker is still alive.
  • The final pass's Worker awaits release() before it sends done. The host terminates it only after that.
  • Test: a pass with fake models whose release is slow must have finished releasing by the time finalizeCall returns. I checked that it fails when the Worker answers before the release finishes.

Not changed

  • The catch-up still retries a failed call on every start with no limit, as the 0.5.0 changelog says. This fix does not touch catchUpFinals, so I left it alone.
  • A pass stuck in a native call and stopped at its budget is still terminated without a release. It keeps its models, but that is one rare pass and not every pass.
  • The live Worker (terminated on quit or after a crash) and a server job's Worker (terminated on cancel) follow the same rule when they end. Neither ends after every pass, so neither is part of this leak.

A final pass ends in terminate, and a terminated Bun Worker never runs the
sherpa-onnx finalizers still pending, so every pass left its model set in
the app: about 2.7 GB of Parakeet and Silero per pass, for the life of the
app. The first-start catch-up runs every unfinalized call back to back, so
a backlog of four calls held about 10 GB, all of it pushed to swap.

The pass now drops its models and collects until their finalizers have run
before it answers. scripts/measure-final-memory.ts measures it.
@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 16 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: GeiserX/akou/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 5b00240c-1e16-4799-ae02-eab01cb801db

📥 Commits

Reviewing files that changed from the base of the PR and between 79fe432 and eeea6dc.

📒 Files selected for processing (6)
  • scripts/measure-final-memory.ts
  • src/main/asr/engine.ts
  • src/main/asr/finalize-worker.ts
  • src/main/asr/sherpa.ts
  • tests/asr-final.test.ts
  • tests/fixtures/asr-fake.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@GeiserX
GeiserX merged commit 17ff7ad into main Sep 29, 2026
28 checks passed
@GeiserX
GeiserX deleted the fix/final-pass-memory branch September 29, 2026 18:07
GeiserX added a commit that referenced this pull request Oct 4, 2026
…ng go of models nothing uses (#322)

akou held about 3.7 GB while sitting idle on the reference Mac, hours after the last call, with only 56 MB of it resident. On a 24 GB Mac that is enough to start freezing it. Nothing was leaking. With dictation on, the live Worker loaded Parakeet (about 2.2 GB) and the streaming model (about 1.8 GB) at start and kept both for good, even though a live dictation never decodes with Parakeet. A call loaded Parakeet too, and nothing ever let it go.

Now the dictation warm-up loads Parakeet only when the dictation's text comes from it (`dictation.final` `parakeet`, or no streaming model). A new setting, `asr.modelIdleMinutes` (default 5; 0 lets go at once, as `server.model_idle_minutes` does; up to 10080 keeps them loaded on a Mac with memory to spare), lets the live Worker drop every model once no call and no dictation has used it for that long. The app then loads back only what a dictation needs to start fast. The method is `LiveAsr.releaseIdle()`, named and shaped like the job Worker's idle release in #270. That PR covers file-job Workers, this one covers the live Worker. They share no code because they are different Workers.

Footprint (`footprint -p`, which counts the compressed and swapped pages RSS hides) of the app process, measured on a 16 GB M4 with dictation on in English and Spanish:

| | main | this branch |
|---|---|---|
| fresh start | 3,612 to 4,104 MB | 1,692 to 1,921 MB |
| after a 30 s call and its final pass | 3,889 MB | 4,111 MB |
| after 5 to 30 dictations | 3,903 MB | 3,941 MB |
| idle, before the timer | 3,899 MB (unchanged after 3 min) | 3,938 MB (4 min) |
| idle, after the timer (5 min) | n/a | 2,031 MB (the streaming model loaded back) |

Thirty dictations did not grow the footprint on main (3,904 MB after 10, 3,903 after 20 and 30). Each finished final pass already lets go of its models (#179).

The cost is load time. These numbers come from a loaded machine:
- A `fast` dictation, a retry on `fast`, or a live dictation whose stream fails and falls back to `fast` now waits for Parakeet when it is not loaded. The first `fast` clip after a fresh start took 8.4 s wall time on this branch against 1.4 s on main. Parakeet loads in 2 to 3 s on an idle machine.
- A call's first lines wait for Parakeet the same way.
- A press while the streaming model reloads after an idle period waits for it, about 1 to 2 s.
- Live dictations, the default, are unaffected: 1.3 to 2.3 s for a 6 s clip on both.

`tests/idle-memory.e2e.test.ts` runs the app in its own process on the real models: a fresh start, a 20 s call with its final pass, 5 dictations, then an idle minute (`asr.modelIdleMinutes: 1`). It asserts that the call adds Parakeet to the fresh start (over 1.5 GB) and that the idle app is back within 1000 MB of its fresh start. Over seven runs on a busy 16 GB Mac the idle app read from 1,158 MB under to 529 MB over its fresh start (the allocator's freed small regions and swap), so the margin sits above that spread and below Parakeet; the run without a release read 2,300 MB over. It is skipped off macOS or without `AKOU_LIVE_MODELS`, and floors go +1 skip. Final runs, on this branch merged with main:
- main: red. Fresh 3,439 MB, after the call 4,303 MB (+864), idle 3,998 MB. The fresh start already holds Parakeet.
- this branch: green three times. Fresh 1,427 MB, after the call 4,375 MB, idle 1,429 MB; fresh 1,787 MB, after 4,411 MB, idle 2,193 MB; and, at the 1000 MB margin, fresh 1,781 MB, after 4,463 MB, idle 2,194 MB.
- this branch with the idle setting at 1440: red on the idle check. Fresh 1,542 MB, idle 3,842 MB.

Unit tests in `tests/asr-live.test.ts` drive `releaseIdle` with a manual clock: release after a call, a long call holding the models, an open dictation holding them, and nothing released without the setting. There is one test per busy rule (a call on the Worker, an open dictation, a decode, a speech check and a warm-up in flight), and replacing any one rule with `false` turns exactly its own test red. The warm-up rule means a release never undoes a warm-up still loading. The Settings page has a row for the new setting under Speech engines. DC-E7 in DICTATION.md and its tests now say that Parakeet loads ahead only when the dictation's text comes from it.

Not changed: `asr.qwenIdleMinutes` (the `best` dictation server) still defaults to never, and its memory is in a separate llama-server process.

Next step, not built here: int8 variants of Parakeet and the streaming model could cut the idle footprint further, but that trades accuracy against the fp32 choice the benchmark made, so it is a decision for later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant