okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main dbb2d168c (picture-reader fix, GGML_NO_BACKTRACE) into f-own-int2503b206b

main
1.8 KiB33 linesraw
1"""The product never starts a debugger on a user's machine.
2
3When ggml aborts (a failed GGML_ASSERT, an uncaught C++ exception in a program that links ggml) it prints a backtrace first: on Linux it forks
4`gdb --batch -ex attach <pid>` (then lldb, then backtrace_symbols), on macOS it prints frames and three WARNING lines, on Windows nothing.  A
5debugger that attaches to a process the server is about to end writes its own errors into the log ("Couldn't get registers: No such process")
6and, because it inherits the reader's stdout, into the pipe the server reads the reader's READY line from.  `GGML_NO_BACKTRACE` (any value)
7switches it all off.  Every child Strata starts gets it, unless a developer asks for the backtrace with STRATA_BACKTRACE=1 (then nothing is
8set here, and the environment is left as the developer made it).
9"""
10from __future__ import annotations
11
12import os
13
14NO_BACKTRACE = "GGML_NO_BACKTRACE"
15DEVELOPER_SWITCH = "STRATA_BACKTRACE"
16
17
18def developer_wants_backtrace(env=None) -> bool:
19    """STRATA_BACKTRACE is set to anything but empty / 0 / no / off / false, in `env` or in this process's environment."""
20    for source in (env, os.environ):
21        value = str((source or {}).get(DEVELOPER_SWITCH, "")).strip().lower()
22        if value and value not in ("0", "no", "off", "false"):
23            return True
24    return False
25
26
27def quiet_crash_env(env: dict | None = None) -> dict:
28    """`env` (this process's own environment when None) with GGML_NO_BACKTRACE=1 set unless it is already there or the developer switch is on.
29    Changes the dict it is given and returns it, so it fits where an environment is built: `env = quiet_crash_env(dict(os.environ))`."""
30    target = os.environ if env is None else env
31    if NO_BACKTRACE not in target and not developer_wants_backtrace(target):
32        target[NO_BACKTRACE] = "1"
33    return target