Skip to content

Turn AVFAudio exceptions in mic start into errors, and say so when a call can't be recorded - #73

Open
ddobrinskiy wants to merge 1 commit into
gsamat:masterfrom
ddobrinskiy:fix/avfaudio-exception-in-mic-start
Open

ddobrinskiy wants to merge 1 commit into
gsamat:masterfrom
ddobrinskiy:fix/avfaudio-exception-in-mic-start

Conversation

@ddobrinskiy

Copy link
Copy Markdown

Fixes #72.

AVFAudio reports misuse by raising an Objective-C exception, which Swift cannot catch. When the mic start raised, the exception skipped RecordingSession.start's catch, so the system tap kept recording, and it killed the auto-record timer (or aborted the app when the start came from amanu record start). Details and a one-file reproduction are in the issue.

What changes

  • AmanuObjC: a small Objective-C target with AmanuObjCExceptionCatcher.catching(_:), which runs a block inside @try/@catch and returns the exception as an NSError (name and reason kept). SwiftPM needs a separate target for Objective-C; it adds no dependency.
  • MicRecorder: engine.connect, prepare()/start() and both installTap calls in the mic start go through MicRecorder.guarded, and installVoiceTap now throws. A raise then takes the path that already exists: attachWithFallbacks, then StartFailure.microphone, which stops the system tap and removes the folder. Auto-record keeps running.
  • Banner: StartFailure.bannerBody replaces the raw error text in the start-failed banner with one sentence saying the call is not being recorded and what to try (English and Russian, like the rest of the banners). The system's error text stays in the log.

Tests

  • MicExceptionGuardTests: an exception raised from the body becomes a thrown error with its reason (no hardware needed); a body that does not raise runs untouched; a real second installTap and a real format mismatch become thrown errors (these two are cancelled when the machine has no audio input).
  • RecordingSessionTests: an Objective-C exception inside the mic start ends as StartFailure.microphone, with the system tap stopped and no folder left. No hardware needed.
  • StartFailureBannerTests: the banner says the call is not being recorded and does not contain the raw error.
  • Without the catcher the new hardware-free tests abort the test process with an uncaught NSException.
  • swift test: 773 tests pass. landing/tests/check.py passes. Tests/scripts has 3 errors that are identical on an untouched origin/master on my machine (the compatibility tests cannot link against the Command Line Tools SDK here).
  • In the real app, with the exception injected into installTap (a test-only dylib on an ad-hoc-signed build): 0.6.5 aborts on amanu record start; with this change the failure is logged, the banner appears, the app keeps running, and the folder is removed. A normal recording started and stopped with amanu record still works: mic.caf and system.caf while it runs, then audio.m4a and meta.json.

Not changed

  • A failed mic start still abandons the whole session; it does not fall back to recording the system track alone. The existing test says why ("half a session must never run silently"). If you would rather record the far end with a visible "no microphone" state, that is a separate decision.
  • The tone test in Setup/Permissions.swift also calls AVAudioEngine.connect/start; it is not on the recording path and I left it alone.
  • I could not confirm what raised on 2026-11-16 (the log redacts it), so the issue describes the mechanism and the two exceptions I could produce.

…call can't be recorded

AVFAudio reports misuse (a second installTap, a tap format that doesn't match the
input) by raising an Objective-C NSException. Swift cannot catch it. Raised from
MicRecorder.start, it unwound through RecordingSession.start without running the
catch that stops the system tap, and:

- from the auto-record timer it killed that timer for the rest of the run, so no call
  was recorded automatically until amanu was restarted, while the system tap kept
  recording with the menu showing idle (2026-11-16: a 74 h system-only recording);
- from `amanu record start` it terminated the app.

A small Objective-C target (AmanuObjC) wraps the AVAudioEngine calls in
@try/@catch and returns an NSError, so the existing failure path runs: the tap is
stopped, the folder is removed and the start-failed banner is shown.

The banner now says that the call is not being recorded and what to try, without
the system's error text (that stays in the log).

Tested against real AVFAudio exceptions (double tap, format mismatch); without the
catcher the test process aborts. Also exercised in the real app with the exception
injected into installTap: the original build aborts, this build logs the failure,
shows the banner and keeps running.
@ddobrinskiy

Copy link
Copy Markdown
Author

@gsamat could you review this when you have a moment? It fixes #72. Two things worth a look: the new AmanuObjC target in Package.swift (SwiftPM needs a separate target for Objective-C), and that a failed mic start still abandons the whole session rather than falling back to the system track alone (see "Not changed").

This branch has not been deployed

No deployments
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.

[Bug]: An AVFAudio exception while the mic starts kills auto-record and leaves the system tap recording (a 74 h, 35 GB runaway)

1 participant