From ee0a47f16688080457edd8bf01ae0d746d21a79e Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Tue, 5 Apr 2022 17:04:54 +0900 Subject: [PATCH] cli: check for closed stdout/stderr --- dvc/cli/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dvc/cli/__init__.py b/dvc/cli/__init__.py index 78dd13819..3910ebf73 100644 --- a/dvc/cli/__init__.py +++ b/dvc/cli/__init__.py @@ -1,6 +1,7 @@ """This module provides an entrypoint to the dvc cli and parsing utils.""" import logging +import sys # Workaround for CPython bug. See [1] and [2] for more info. # [1] https://github.com/aws/aws-cli/blob/1.16.277/awscli/clidriver.py#L55 @@ -48,6 +49,16 @@ def main(argv=None): # noqa: C901 from dvc.exceptions import DvcException, NotDvcRepoError from dvc.logger import FOOTER, disable_other_loggers + # NOTE: stderr/stdout may be closed if we are running from dvc.daemon. + # On Linux we directly call cli.main after double forking and closing + # the copied parent's standard file descriptors. If we make any logging + # calls in this state it will cause an exception due to writing to a closed + # file descriptor. + if sys.stderr.closed: # pylint: disable=using-constant-test + logging.disable() + elif sys.stdout.closed: # pylint: disable=using-constant-test + logging.disable(logging.INFO) + args = None disable_other_loggers() @@ -68,7 +79,7 @@ def main(argv=None): # noqa: C901 logger.trace(args) - if not args.quiet: + if not sys.stdout.closed and not args.quiet: from dvc.ui import ui ui.enable()