diff --git a/packages/omamobile-audio/lib/speaker_dsp.py b/packages/omamobile-audio/lib/speaker_dsp.py index 162bde627..6842cf3ee 100644 --- a/packages/omamobile-audio/lib/speaker_dsp.py +++ b/packages/omamobile-audio/lib/speaker_dsp.py @@ -221,6 +221,14 @@ def _load(self): except (OSError, ValueError): return {} + def _save(self, state): + # rename over the file, so the fake mixer reading it at the same + # moment never sees half of it + tmp = f'{self.path}.{os.getpid()}' + with open(tmp, 'w') as f: + json.dump(state, f) + os.replace(tmp, self.path) + def names(self): return list(self._load()) @@ -238,8 +246,7 @@ def read(self, name): if name.endswith('HALO_HEARTBEAT') and os.environ.get('FAKE_HB_STUCK') != '1' and not frozen: v += 1 # the firmware's heartbeat moves while it runs c['v'] = list(v.to_bytes(4, 'big')) - with open(self.path, 'w') as f: - json.dump(state, f) + self._save(state) return v if t == 'ENUMERATED': return c['items'][c['v']] @@ -274,8 +281,7 @@ def write_bytes(self, name, data): if log_path: with open(log_path, 'a') as f: f.write(name + '\n') - with open(self.path, 'w') as f: - json.dump(state, f) + self._save(state) return True diff --git a/packages/omamobile-audio/test/all b/packages/omamobile-audio/test/all index 4b17bed90..a694a0928 100755 --- a/packages/omamobile-audio/test/all +++ b/packages/omamobile-audio/test/all @@ -351,8 +351,12 @@ pass "watchdog: mutes on DSP errors, bypass, levels, runtime PM, a stuck heartbe # level drop, so a jump there must not mute; OMAMOBILE_SPK_TEMP_JUMP brings it back. jump() { # name [wprep...]: watchdog on a running stream; the bottom's estimate goes 25 -> 40 C a moment in local n=$1; shift; echo 'state: RUNNING' > "$r/asound/card0/pcm5p/sub0/status"; wprep=("$@") - ( sleep 0.4; amx cset "name=Bottom DSP1 Protection cd CSPL_TEMPERATURE" 0x00,0x0a,0x00,0x00 >/dev/null ) & - watchrun; wait + # the jump comes once the watchdog has its first reading (its start line, + # then a few intervals), not at a fixed time: a slow runner started it late + rm -f "$r/watch.out" + ( for _ in $(seq 1 100); do grep -c 'watchdog: watching' "$r/watch.out" >/dev/null 2>&1 && break; sleep 0.05; done + sleep 0.3; amx cset "name=Bottom DSP1 Protection cd CSPL_TEMPERATURE" 0x00,0x0a,0x00,0x00 >/dev/null ) & + WATCH_N=60 watchrun; wait } startok w-jump8; jump w-jump8 ((rc == 0)) && ! muted && grep -c 'jump rule off' "$r/watch.out" >/dev/null || { cat "$r/watch.out" >&2; fail "watchdog: at analog 10 or below a 15 C jump of the estimate does not mute (C9j)"; } diff --git a/packages/omamobile-audio/test/fake-amixer b/packages/omamobile-audio/test/fake-amixer index 79d6b8776..fd00ead52 100755 --- a/packages/omamobile-audio/test/fake-amixer +++ b/packages/omamobile-audio/test/fake-amixer @@ -76,8 +76,12 @@ def load(): def save(c): - with open(state_path, 'w') as f: + # write and rename, so a reader running at the same moment (the + # watchdog while a test changes a control) never sees half a file + tmp = f'{state_path}.{os.getpid()}' + with open(tmp, 'w') as f: json.dump(c, f) + os.replace(tmp, state_path) def show(name, ctl): diff --git a/packages/omamobile-audio/test/fake-pw b/packages/omamobile-audio/test/fake-pw index 2577c4e6c..7a23b7c54 100755 --- a/packages/omamobile-audio/test/fake-pw +++ b/packages/omamobile-audio/test/fake-pw @@ -62,8 +62,10 @@ if me == 'pw-cli': if k not in vals or os.environ.get('FAKE_PW_IGNORE') == '1': continue # the filter ignores unknown controls vals[k] = float(v) - with open(state, 'w') as f: + tmp = f'{state}.{os.getpid()}' + with open(tmp, 'w') as f: json.dump(vals, f) + os.replace(tmp, state) log(args[3]) sys.exit(0) if me == 'wpctl': diff --git a/packages/omamobile-audio/version b/packages/omamobile-audio/version index 7d8568351..d1d899fa3 100644 --- a/packages/omamobile-audio/version +++ b/packages/omamobile-audio/version @@ -1 +1 @@ -0.5.4 +0.5.5