Skip to content

Commit

Permalink
Handle SIGPIPE cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvbrt committed Mar 22, 2024
1 parent bc9bfc7 commit 1b9f225
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions k4FWCore/scripts/k4run
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,26 @@ def add_arguments(parser, app_mgr):
)


if __name__ == "__main__":
class LoggingHandler(logging.StreamHandler):
def handleError(self, record):
# Re-raise SIGPIPE generated exception
excp = sys.exc_info()[1] # from Python 3.11 can be replaced with
# sys.exception()
if isinstance(excp, BrokenPipeError):
raise(excp)

super().handleError(record)


def main():
# ensure that we (and the subprocesses) use the C standard localization
os.environ["LC_ALL"] = "C"

logger = logging.getLogger()
logger.setLevel(logging.INFO)
# formatter = logging.Formatter('[%(asctime)s %(levelname)s] %(message)s', datefmt='%Y-%b-%d %H:%M:%S')
formatter = logging.Formatter("[k4run] %(message)s")
handler = logging.StreamHandler(stream=sys.stdout)
handler = LoggingHandler(stream=sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)

Expand Down Expand Up @@ -251,3 +262,14 @@ if __name__ == "__main__":
if ApplicationMgr().EvtMax == -1 and retcode == 4:
retcode = 0
sys.exit(retcode)


if __name__ == "__main__":
try:
main()
except BrokenPipeError:
# Handle SIGPIPE generated exception
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
os.dup2(devnull, sys.stderr.fileno())
sys.exit(1)

0 comments on commit 1b9f225

Please sign in to comment.