crash_guard
¶
Keeps the terminal usable when Reloaded dies without a chance to clean up after itself, e.g. a crash in native code, and saves where a crash happened.
The terminal UI switches the terminal to its own screen, turns mouse reporting on, and reads keys raw. A process that is killed outright, or that exits from inside native code (a library that gives up with a plain exit), never switches any of that back. Nothing inside the dead process can fix that, and nothing outside it can do it quickly enough: a shell started from a script sees the script exit normally, so it keeps the terminal as it finds it, raw, and starts its line editor there. The line editor takes a terminal with echo off to mean that echo is wanted off, so it shows nothing that is typed, and it keeps that state for every later prompt. A helper process woken the instant Reloaded died still lost that race to the shell.
So the command runs Reloaded as a child process and waits for it. The parent is part of the job the shell is waiting for, so it can put the terminal right before the shell gets it back. The child tells the parent, through a pipe, when the terminal UI session starts and when it has finished cleaning up, which is how the parent knows whether the terminal needs putting right.
It also turns on Python's fault handler, so a crash writes the Python stack of every thread to a file, which is what is needed to find out where it happened.
RESET_TERMINAL
module-attribute
¶
RESET_TERMINAL = "\x1b[?1049l\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1005l\x1b[?1006l\x1b[?1015l\x1b[?1004l\x1b[?2004l\x1b[?2026l\x1b[<9u\x1b[?1l\x1b>\x1b[?7h\x1b[?25h\x1b[0m"
What to send a terminal to undo everything the terminal UI turns on.
SUPERVISED
module-attribute
¶
Set in the environment of the process that does the work, so it does not supervise itself.
PROGRESS_FD
module-attribute
¶
Set in the environment of the process that does the work, to the number of the pipe it reports its progress through.
current_tty
¶
current_tty() -> str | None
The path of the terminal Reloaded is running in, or None when there is no terminal to look after (output is piped, or this is not a Unix system).
Source code in src/mujoco_mojo/utils/layers/crash_guard.py
restore_terminal
¶
Puts the terminal at tty_path back the way it was: its input settings as saved (from termios.tcgetattr), and its screen modes reset. Prints message afterwards, if there is one.
Source code in src/mujoco_mojo/utils/layers/crash_guard.py
crash_message
¶
What to tell the user after Reloaded stopped without cleaning up, including where the stacks went if a crash left any. status is the child's, negative for a signal.
Source code in src/mujoco_mojo/utils/layers/crash_guard.py
run_supervised
¶
Runs the command that is running now again, as a child process, waits for it, and gives the status to exit with. If the child was killed, or stopped in the middle of the terminal UI session without finishing it, the terminal is put right first.
Gives None when that is not wanted, which means the caller should just carry on and do the work: this already is the child, or there is no terminal to look after.