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:
+11
-1
@@ -845,7 +845,7 @@ def check_identity(chk):
|
|||||||
|
|
||||||
def write_json(path, obj):
|
def write_json(path, obj):
|
||||||
"""Write a small JSON file in one go (a reader never sees half of it)."""
|
"""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:
|
with open(tmp, "w") as f:
|
||||||
json.dump(obj, f, indent=1, sort_keys=True)
|
json.dump(obj, f, indent=1, sort_keys=True)
|
||||||
os.replace(tmp, path)
|
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)
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
|
||||||
proc.stdin.write(script)
|
proc.stdin.write(script)
|
||||||
proc.stdin.close()
|
proc.stdin.close()
|
||||||
|
empty_since = None
|
||||||
for line in proc.stdout:
|
for line in proc.stdout:
|
||||||
data = parse_remote_midi(line)
|
data = parse_remote_midi(line)
|
||||||
if data is not None:
|
if data is not None:
|
||||||
snap.remote_midi[host], snap.remote_midi_time[host] = data, time.time()
|
snap.remote_midi[host], snap.remote_midi_time[host] = data, time.time()
|
||||||
remember_peers(snap, host, data)
|
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():
|
if stop.is_set():
|
||||||
break
|
break
|
||||||
except (OSError, ValueError) as e:
|
except (OSError, ValueError) as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user