Skip to content

omamobile-shell 0.2.134: the voice preview can no longer be left holding the microphone - #890

Merged
derekross merged 1 commit into
derekross:mainfrom
W4HO-ham:shell-voice-preview-stop
Nov 18, 2026
Merged

derekross merged 1 commit into
derekross:mainfrom
W4HO-ham:shell-voice-preview-stop

Conversation

@W4HO-ham

Copy link
Copy Markdown
Contributor

omamobile-voice-preview promises to end after 75 s whatever happens, "so the microphone is never left open". On my Pixel 9a this morning it had been running for 1 h 54 min, long after the voice bar session that started it:

  • the process had been handed to the user's systemd (the shell had been restarted since), with its pw-record still capturing: pw-top showed the node omamobile-voice-preview running on the microphone, with the screen off;
  • its main thread sat in futex(..., FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 0, NULL, ...), an untimed lock wait, with no context switch in seconds, while the capture thread went on reading the pipe;
  • it held 211 MB, which is the audio of those two hours at 16 kHz. Nothing was written to disk or sent: the main thread does the transcribing, and it was the one that was blocked.

I killed it by hand, and the microphone went idle.

The cause. The SIGTERM handler called stopping.set() on a threading.Event. Event.set() takes the event's own lock, and the loop spends its idle time in stopping.wait(0.1), which holds that lock for a few bytecodes on every round. A handler runs in the main thread between two of its bytecodes, so a SIGTERM that lands in that stretch waits for a lock its own thread holds, for good. The 75 s limit was the loop's, so nothing was left to end it. The bar had done its part: it stops the preview with SIGTERM when listening ends. The same call from a handler with the lock held never returns on Python 3.14.8 and 3.14.9:

import os, signal, threading
ev = threading.Event()
signal.signal(signal.SIGTERM, lambda *_: ev.set())
with ev._cond:                       # where wait() is between taking the lock and waiting
    os.kill(os.getpid(), signal.SIGTERM)
    for _ in range(1000): pass       # the handler runs here and never returns

The fix.

  • The handler only sets a flag and kills the pass under way. No Event, no lock.
  • The loop sleeps a tenth of a second and looks at the flag.
  • The kernel keeps a second limit: an alarm at 90 s with SIGALRM's default action. It ends the process whatever its threads are doing, and pw-record with it (PR_SET_PDEATHSIG, as before). It is armed before the microphone is opened.

Tested:

  • test/voice-preview.py gains three checks: thirty stops at different moments of a waiting loop each end at the loop's next look; the handler has no Event and takes no lock; a process whose main thread is blocked on a lock for good is ended by the alarm. test/all, tests/all and tools/pr-check origin/main pass.
  • On the 9a (7.3.0-rc6-omamobile-tegu-s2mpg15-m1-g991042e2eff3-dirty, Python 3.14.9): the test file passed there, and the installed script, started on the real microphone and stopped with SIGTERM fifteen times at different moments, ended each time within 0.6 s, with no pw-record left and no audio node running.

Not tested: the stuck state itself can't be produced on demand with the old script (it needs the signal to land in a few bytecodes), so the proof of the cause is the main thread's untimed lock wait on the stuck process and the five lines above. The package installed as a package on a phone (I replaced /usr/bin/omamobile-voice-preview by hand).

docs/VOICE-PLAN.md phase 3 has the record. It bumps omamobile-shell to 0.2.134, as #886, #887 and #888 do; this one should go first if you agree it matters most, and I'll rebase the others as they land.

…ing the microphone

omamobile-voice-preview promises to end after 75 s whatever happens, so that the microphone is never left open. On a Pixel 9a it ran for 1 h 54 min after a voice bar session: its main thread sat in an untimed lock wait while the capture thread went on reading the microphone into memory (211 MB by then; nothing was written to disk or sent). The bar had stopped it with SIGTERM, as it should.

The handler called threading.Event.set(), which takes the event's own lock, and the loop spends its idle time in Event.wait(0.1), which holds that lock for a few bytecodes on every round. A handler runs in the main thread between two of its bytecodes, so a SIGTERM that landed in that stretch waited for a lock its own thread held. The 75 s limit was the loop's, so nothing was left to end it. The same call from a handler with the lock held never returns on Python 3.14.8 and 3.14.9.

Now the handler only sets a flag and kills the pass under way, the loop sleeps and looks at the flag, and the kernel keeps a second limit: an alarm at 90 s with SIGALRM's default action, which ends the process whatever its threads are doing, and pw-record with it (PR_SET_PDEATHSIG).

Tested: test/voice-preview.py gains three checks (thirty stops at different moments of a waiting loop each end at the loop's next look; the handler has no Event and takes no lock; a process whose main thread is blocked on a lock for good is ended by the alarm, and the alarm is armed before the microphone is opened), and test/all. On the 9a (Python 3.14.9) the test file passed, and the installed script, started on the real microphone and stopped with SIGTERM fifteen times at different moments, ended each time within 0.6 s with no pw-record left and no audio node running.

@derekross derekross left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is a good find and a careful fix. I reproduced the cause here: your five lines never return on Python 3.14.8, and the process then ignores SIGTERM as well, which is exactly what you found on the palmtop. I read the new handler down to Popen.send_signal (it polls with a non-blocking lock, so nothing in it can wait on the main thread), checked that the alarm is armed before the microphone is opened and that nothing in the bar reads the preview's exit status, and ran test/voice-preview.py here: 9 of 9, the three new cases included.

I'm merging this first of the shell pull requests, so it keeps 0.2.134 and the sentence in VOICE-PLAN stays right. #886 and #896 will each need the next free number when their turn comes; I'll say which.

One thought for later, not for this PR: the stuck preview had outlived a shell restart. With the alarm that is now 90 s at most; a parent-death signal on the preview itself would make it immediate.

@derekross
derekross merged commit 70c06e6 into derekross:main Nov 18, 2026
2 checks passed
derekross added a commit to kdegeek/omarchy-mobile-fork that referenced this pull request Nov 18, 2026
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

2 participants