Skip to content

Parsing arguments using argparse can lead to SIGSTOP #101474

Open
@sliedes

Description

@sliedes

Bug report

I don't know if this is considered a bug in Python or Dash or Flask or if something can be done about this. I'm raising it here first because I find Python's behavior, requiring in some cases a terminal for parsing arguments, surprising.

I am running a Dash application, which I believe uses flask. If I run the application in background ("python app.py &" in my Linux terminal), it stops with a SIGSTOP.

In my main module, I have something like this:

def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    ...
    return parser.parser_args()

def main() -> None:
    args = parse_args()
    dash_app(args).run_server()

if __name__ == "__main__":
    main()

Flask uses some magic to reload the file when it changes. After debugging this a bit, it seems to use werkzeug to run a function in an independent Python interpreter (run_with_reloader).

argparse's HelpFormatter, in turn, calls shutil.get_terminal_size() in its __init__. If the process is backgrounded (and probably the terminal is set to stop on output, but what does that?), the kernel sends the process a SIGTTOU. If I set the environment variable COLUMNS to a number, this doesn't happen because argparse does not call shutil.get_terminal_size().

I'm a bit lost about how, where and why in the process terminal starts reacting to output by SIGTTOU.

Nevertheless, I see that os_get_terminal_size_impl() tries to be very defensively coded, but evidently still causes problems in some cases.

I can try to find a sane reproducer for this later. I realize this probably will have to be digged further, unless someone has a good idea of what exactly happens.

Your environment

  • CPython versions tested on: 3.10.6 (from Conda)
  • Operating system and architecture: Ubuntu 22.04.1 LTS, x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Bugs

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions