Skip to content

[BUG] PythonTool can kill the whole process #65

Description

@bkj

If the code includes sys.exit or raise SystemExit, it won't get caught here:
https://github.com/jataware/archytas/blob/main/archytas/python.py#L54
and it'll kill the whole process.

ChatGPT says the solution is

        try:
            exec(script, self.locals)
        except BaseException as e:  # catches SystemExit too
            if isinstance(e, SystemExit):
                code = e.code if e.code is not None else 0
                exception_info = {
                    "type": "SystemExit",
                    "message": f"exit({code})",
                    "traceback": traceback.format_exc(),
                    "code": code,
                }
            else:
                exception_info = {
                    "type": type(e).__name__,
                    "message": str(e),
                    "traceback": traceback.format_exc(),
                }

ChatGPT also says that this won't catch os._exit, which would need to be monkey-patched like

import os, types

def _blocked__exit(code=0):
  raise SystemExit(code)  # or a custom exception you handle

# Before exec:
safe_os = types.SimpleNamespace(**{k: getattr(os, k) for k in dir(os) if not k.startswith("_")})
safe_os._exit = _blocked__exit
self.locals.setdefault("os", safe_os)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions