twist_check: survive a closed SSH window and unexpected errors

A closed terminal made print() raise and any unexpected exception skipped the
cleanup, leaving the nozzle low and hot. Both now end in the normal stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-03 22:55:55 +01:00
1 parent 92d86d6bfd
commit 2592030cb4
1 file changed
+18 -6
+18 -6
View File
@@ -91,7 +91,10 @@ class Stopped(Exception):
def log(text): def log(text):
line = "%s %s" % (time.strftime("%H:%M:%S"), text) line = "%s %s" % (time.strftime("%H:%M:%S"), text)
print(line, flush=True) try:
print(line, flush=True)
except OSError:
pass # SSH window closed: the run continues, the log file stays
if LOG: if LOG:
LOG.write(line + "\n") LOG.write(line + "\n")
LOG.flush() LOG.flush()
@@ -635,16 +638,25 @@ def run(mode, host):
failure = ("Twist %s stopped" % mode, [ failure = ("Twist %s stopped" % mode, [
str(e), "Nothing changed in the configuration. " str(e), "Nothing changed in the configuration. "
"Fix this, then start again with TWIST_CALIBRATE."]) "Fix this, then start again with TWIST_CALIBRATE."])
except Exception as e:
# Whatever went wrong, the nozzle must still be lifted and switched off
failure = ("Twist %s stopped" % mode, [
"Unexpected error: %r" % e, "Nothing changed in the configuration."
" See twist_data/twist_run.log."])
signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN)
signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGINT, signal.SIG_IGN)
if started: if started:
cleanup(p) cleanup(p)
if res: if res:
report_console(res) try:
report_html(res, report) if res["block"]:
if res["block"]: with open(os.path.join(DATA_DIR, "twist_suggested.cfg"),
with open(os.path.join(DATA_DIR, "twist_suggested.cfg"), "w") as f: "w") as f:
f.write(res["block"]) f.write(res["block"])
report_html(res, report)
report_console(res)
except Exception as e:
log("Could not write the report: %r" % e)
log(res["title"]) log(res["title"])
return finish(p, res, failure, os.path.basename(report), started) return finish(p, res, failure, os.path.basename(report), started)