Skip to content

Commit

Permalink
fix: Handle broken pipe error (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ authored Oct 19, 2024
1 parent a5e6fde commit b70dc37
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/sphinxnotes/snippet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# startup of the CLI tool.
from __future__ import annotations
import sys
import os
from os import path
import argparse
from typing import List, Iterable, Tuple
from os import path
from textwrap import dedent
from shutil import get_terminal_size
import posixpath
Expand Down Expand Up @@ -344,4 +345,13 @@ def _on_command_integration(args: argparse.Namespace):


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

0 comments on commit b70dc37

Please sign in to comment.