Skip to content

Commit b70dc37

Browse files
authored
fix: Handle broken pipe error (#36)
1 parent a5e6fde commit b70dc37

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/sphinxnotes/snippet/cli.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# startup of the CLI tool.
1414
from __future__ import annotations
1515
import sys
16+
import os
17+
from os import path
1618
import argparse
1719
from typing import List, Iterable, Tuple
18-
from os import path
1920
from textwrap import dedent
2021
from shutil import get_terminal_size
2122
import posixpath
@@ -344,4 +345,13 @@ def _on_command_integration(args: argparse.Namespace):
344345

345346

346347
if __name__ == '__main__':
347-
sys.exit(main())
348+
# Prevent "[Errno 32] Broken pipe" error.
349+
# https://docs.python.org/3/library/signal.html#note-on-sigpipe
350+
try:
351+
sys.exit(main())
352+
except BrokenPipeError:
353+
# Python flushes standard streams on exit; redirect remaining output
354+
# to devnull to avoid another BrokenPipeError at shutdown.
355+
devnull = os.open(os.devnull, os.O_WRONLY)
356+
os.dup2(devnull, sys.stdout.fileno())
357+
sys.exit(1) # Python exits with error code 1 on EPIPE

0 commit comments

Comments
 (0)