Skip to content

Commit 1bfd8b2

Browse files
authored
Merge pull request #245 from oslokommune/fix-sigpipe
Improve SIGPIPE handling
2 parents 1680497 + 0814adc commit 1bfd8b2

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* New option `--verbose` for the `datasets ls` command which lists every
1111
relevant metadata field for the listed datasets.
1212
* New output format option for printing CSV: `--format=csv`.
13+
* Improved SIGPIPE handling so that `okdata` works better with tools
14+
such as `head`.
1315

1416
## 4.4.0 - 2025-05-14
1517

okdata/cli/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import sys
34

45
from keycloak.exceptions import KeycloakGetError, KeycloakPostError
@@ -33,6 +34,9 @@ def main():
3334
try:
3435
instance.login()
3536
instance.handle()
37+
# Flush output here to force SIGPIPE to be triggered while inside
38+
# this try block.
39+
sys.stdout.flush()
3640
except RequestException as e:
3741
if hasattr(e.response, "json"):
3842
instance.print_error_response(e.response.json())
@@ -55,6 +59,11 @@ def main():
5559
)
5660
except (EOFError, KeyboardInterrupt):
5761
instance.print("\nAbort.")
62+
except BrokenPipeError:
63+
# https://docs.python.org/3/library/signal.html#note-on-sigpipe
64+
devnull = os.open(os.devnull, os.O_WRONLY)
65+
os.dup2(devnull, sys.stdout.fileno())
66+
sys.exit(1)
5867
except Exception as e:
5968
instance.print(
6069
"An exception occurred",

0 commit comments

Comments
 (0)