Skip to content

fix: Handle broken pipe error #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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