Remote MIDI reader restarts after a minute of an empty session list; one temp file per JSON writer

A reader that reconnects while the other Mac is still at the login window never sees the user's
MIDI sessions and reports none for ever (four false NO SESSION alarms after the Tracks and
ProPresenter Macs rebooted on Sunday morning). Two threads writing fixes-running.json shared one
temp file and one rename failed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-10-18 05:59:30 -04:00
co-authored by Claude Fable 5.1
parent 98739bd041
commit 4f2a5f17a2
+11 -1
View File
@@ -845,7 +845,7 @@ def check_identity(chk):
def write_json(path, obj):
"""Write a small JSON file in one go (a reader never sees half of it)."""
tmp = path + ".tmp"
tmp = "%s.%d-%x.tmp" % (path, os.getpid(), threading.get_ident() & 0xFFFF) # one per writer: two threads never trip over one file
with open(tmp, "w") as f:
json.dump(obj, f, indent=1, sort_keys=True)
os.replace(tmp, path)
@@ -2290,11 +2290,21 @@ def stream_remote_midi(snap, cfg, host, stop):
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
proc.stdin.write(script)
proc.stdin.close()
empty_since = None
for line in proc.stdout:
data = parse_remote_midi(line)
if data is not None:
snap.remote_midi[host], snap.remote_midi_time[host] = data, time.time()
remember_peers(snap, host, data)
# A reader started while that Mac was still at the login window never sees the
# user's MIDI sessions: it reports none, for ever. Start again after a minute of nothing.
if data == {}:
empty_since = empty_since or time.time()
if time.time() - empty_since > 60:
log.info("MIDI stream from %s reports no sessions for a minute: reconnecting", host)
break
else:
empty_since = None
if stop.is_set():
break
except (OSError, ValueError) as e: