# Buggy code
def _pantsd_logging(self):
    """A context manager that runs with pantsd logging.

    Asserts that stdio (represented by file handles 0, 1, 2) is closed to ensure that
    we can safely reuse those fd numbers.
    """

    # Ensure that stdio is closed so that we can safely reuse those file descriptors.
    for fd in (0, 1, 2):
        try:
            os.fdopen(fd)
            raise AssertionError(
                "pantsd logging cannot initialize while stdio is open: {}".format(fd)
            )
        except OSError:
            pass

    # Redirect stdio to /dev/null for the rest of the run, to reserve those file descriptors
    # for further forks.
    with stdio_as(stdin_fd=-1, stdout_fd=-1, stderr_fd=-1):
        # Reinitialize logging for the daemon context.
        result = setup_logging(
            self._log_level, log_dir=self._log_dir, log_name=self.LOG_NAME
        )

        # Do a python-level redirect of stdout/stderr, which will not disturb `0,1,2`.
        # TODO: Consider giving these pipes/actual fds, in order to make them "deep" replacements
        # for `1,2`, and allow them to be used via `stdio_as`.
        sys.stdout = _LoggerStream(logging.getLogger(), logging.INFO, result.log_stream)
        sys.stderr = _LoggerStream(logging.getLogger(), logging.WARN, result.log_stream)

        self._logger.debug("logging initialized")
        yield result.log_stream


# Traceback (ValueError)
F0117 15:10:04.500231 12664 process_manager.py:463] Traceback (most recent call last):
File "/Users/redacted/.pex/install/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl.ac6c80da047e6ed09228262b774906b65ebf6918/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl/pants/pantsd/process_manager.py", line 461, in daemonize
self.post_fork_child(**post_fork_child_opts or {})
File "/Users/redacted/.pex/install/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl.ac6c80da047e6ed09228262b774906b65ebf6918/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl/pants/bin/daemon_pants_runner.py", line 172, in post_fork_child
self._exiter.set_except_hook(sys.stderr)
File "/Users/redacted/.pex/install/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl.ac6c80da047e6ed09228262b774906b65ebf6918/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl/pants/base/exiter.py", line 123, in set_except_hook
self._setup_faulthandler(trace_stream or sys.stderr)
File "/Users/redacted/.pex/install/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl.ac6c80da047e6ed09228262b774906b65ebf6918/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl/pants/base/exiter.py", line 117, in _setup_faulthandler
faulthandler.enable(trace_stream)
File "/Users/redacted/.pex/install/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl.ac6c80da047e6ed09228262b774906b65ebf6918/pantsbuild.pants-1.4.0.dev20+3346017507-cp27-none-macosx_10_8_x86_64.whl/pants/pantsd/pants_daemon.py", line 61, in fileno
return self._stream.fileno()
ValueError: I/O operation on closed file

# Fixed code
def _pantsd_logging(self):
    """A context manager that runs with pantsd logging.

    Asserts that stdio (represented by file handles 0, 1, 2) is closed to ensure that
    we can safely reuse those fd numbers.
    """

    # Ensure that stdio is closed so that we can safely reuse those file descriptors.
    for fd in (0, 1, 2):
        try:
            os.fdopen(fd)
            raise AssertionError(
                "pantsd logging cannot initialize while stdio is open: {}".format(fd)
            )
        except OSError:
            pass

    # Redirect stdio to /dev/null for the rest of the run, to reserve those file descriptors
    # for further forks.
    with stdio_as(stdin_fd=-1, stdout_fd=-1, stderr_fd=-1):
        # Reinitialize logging for the daemon context.
        result = setup_logging(
            self._log_level, log_dir=self._log_dir, log_name=self.LOG_NAME
        )

        # Do a python-level redirect of stdout/stderr, which will not disturb `0,1,2`.
        # TODO: Consider giving these pipes/actual fds, in order to make them "deep" replacements
        # for `1,2`, and allow them to be used via `stdio_as`.
        sys.stdout = _LoggerStream(
            logging.getLogger(), logging.INFO, result.log_handler
        )
        sys.stderr = _LoggerStream(
            logging.getLogger(), logging.WARN, result.log_handler
        )

        self._logger.debug("logging initialized")
        yield result.log_handler.stream