question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot close os.pipe

See original GitHub issue

Describe the bug File descriptor returned by os.pipe() cannot be closed:

_, w = os.pipe()
os.close(w)

OSError: [Errno 9] Bad file descriptor in the fake filesystem: '12'

How To Reproduce Using pytest:

import os
def test_1(fs):
    read, write = os.pipe()
    os.close(write)

Save above as test_foobar.py and run:

$ pytest -vvvv test_foobar.py 
======================================================================================= test session starts =======================================================================================
platform linux -- Python 3.6.5, pytest-4.2.1, py-1.8.0, pluggy-0.9.0 -- /service/nox/test-3-6/bin/python3.6
cachedir: .pytest_cache
rootdir: /service, inifile:
plugins: requests-mock-1.5.2, cov-2.6.1, pyfakefs-3.5.8
collected 1 item                                                                                                                                                                                  

test_foobar.py::test_1 FAILED                                                                                                                                                               [100%]

============================================================================================ FAILURES =============================================================================================
_____________________________________________________________________________________________ test_1 ______________________________________________________________________________________________

fs = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x7fa25d6431d0>

    def test_1(fs):
        read, write = os.pipe()
>       os.close(write)

/service/test_foobar.py:4: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/service/nox/test-3-6/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py:3700: in close
    file_handle = self.filesystem.get_open_file(file_des)
/service/nox/test-3-6/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py:1355: in get_open_file
    self.raise_os_error(errno.EBADF, str(file_des))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x7fa25d6431d0>, errno = 9, filename = '10', winerror = None

    def raise_os_error(self, errno, filename=None, winerror=None):
        """Raises OSError.
        The error message is constructed from the given error code and shall
        start with the error string issued in the real system.
        Note: this is not true under Windows if winerror is given - in this
        case a localized message specific to winerror will be shown in the
        real file system.
    
        Args:
            errno: A numeric error code from the C variable errno.
            filename: The name of the affected file, if any.
            winerror: Windows only - the specific Windows error code.
        """
        message = self._error_message(errno)
        if (winerror is not None and sys.platform == 'win32' and
                self.is_windows_fs):
            if IS_PY2:
                raise WindowsError(winerror, message, filename)
            raise OSError(errno, message, filename, winerror)
>       raise OSError(errno, message, filename)
E       OSError: [Errno 9] Bad file descriptor in the fake filesystem: '10'

/service/nox/test-3-6/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py:976: OSError
==================================================================================== 1 failed in 0.71 seconds =====================================================================================

Please provide a unit test or a minimal code snippet that reproduces the problem.

Your enviroment

Linux-4.20.13-200.fc29.x86_64-x86_64-with
Python 3.6.5 (default, Aug 22 2018, 14:30:18) 
pyfakefs 3.5.8

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
samlcommented, Apr 22, 2019

Currently with master, running the same test case that involves multiprocessing hangs indefinitely (or until pytest timeout). I’ll close this and patch multiprocessing.

0reactions
mrbean-bremencommented, May 2, 2019

@tomhey - I added a small chapter for this kind of problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flushing a pipe (os.pipe) before closing - python
You can't close the pipes until you're sure the process is done writing (because you'll lose data), and you can't wait for the...
Read more >
Closing different ends in a pipe - Unix & Linux Stack Exchange
The answer to questions 1 and 2 is in the pipe man page (section "Examples"): After the fork, each process closes the file...
Read more >
os — Miscellaneous operating system interfaces — Python ...
Close file descriptor fd. This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open()...
Read more >
when should the pipe be closed? : r/learnpython - Reddit
Basically the idea is to close both sides of the pipe eventually, even if there's an error state. Also, where possible, use a...
Read more >
Close Files And Pipes (The GNU Awk User's Guide)
Close Files And Pipes (The GNU Awk User's Guide) ... Often this means the command cannot really do its work until the pipe...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found