Just catching the BrokenPipeError is not sufficient. There might still be data in the stdout buffer, which then causes the following error when Python shuts down and flushes it:
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe
To handle this, redirect stdout to /dev/null in the exception handler before exiting: https://docs.python.org/3.11/library/signal.html#note-on-sigpipe