Repository navigation
omamobile-shell 0.2.134: the voice preview can no longer be left holding the microphone - #890
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
omamobile-voice-previewpromises 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:pw-recordstill capturing:pw-topshowed the nodeomamobile-voice-previewrunning on the microphone, with the screen off;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;I killed it by hand, and the microphone went idle.
The cause. The SIGTERM handler called
stopping.set()on athreading.Event.Event.set()takes the event's own lock, and the loop spends its idle time instopping.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:The fix.
Event, no lock.pw-recordwith it (PR_SET_PDEATHSIG, as before). It is armed before the microphone is opened.Tested:
test/voice-preview.pygains three checks: thirty stops at different moments of a waiting loop each end at the loop's next look; the handler has noEventand takes no lock; a process whose main thread is blocked on a lock for good is ended by the alarm.test/all,tests/allandtools/pr-check origin/mainpass.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 nopw-recordleft 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-previewby hand).docs/VOICE-PLAN.mdphase 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.