Bad file descriptor ошибка

I am using a scientific software including a Python script that is calling os.system() which is used to run another scientific program. While the subprocess is running, Python at some point prints the following:

close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor

I believe that this message is printed at the same time as os.system() returns.

My questions now are:

Which conditions can lead to this type of IOError? What does it exactly mean? What does it mean for the subprocess that has been invoked by os.system()?

Aminah Nuraini's user avatar

asked Oct 7, 2011 at 10:54

Dr. Jan-Philip Gehrcke's user avatar

2

You get this error message if a Python file was closed from «the outside», i.e. not from the file object’s close() method:

>>> f = open(".bashrc")
>>> os.close(f.fileno())
>>> del f
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor

The line del f deletes the last reference to the file object, causing its destructor file.__del__ to be called. The internal state of the file object indicates the file is still open since f.close() was never called, so the destructor tries to close the file. The OS subsequently throws an error because of the attempt to close a file that’s not open.

Since the implementation of os.system() does not create any Python file objects, it does not seem likely that the system() call is the origin of the error. Maybe you could show a bit more code?

answered Oct 7, 2011 at 11:13

Sven Marnach's user avatar

Sven MarnachSven Marnach

568k117 gold badges934 silver badges835 bronze badges

11

You can get this error if you use wrong mode when opening the file. For example:

with open(output, 'wb') as output_file:
    print(output_file.read())

In that code, I want to read the file, but I use mode wb instead of r or r+

vvvvv's user avatar

vvvvv

23.9k19 gold badges48 silver badges75 bronze badges

answered Nov 30, 2015 at 18:38

Aminah Nuraini's user avatar

Aminah NurainiAminah Nuraini

17.9k8 gold badges88 silver badges107 bronze badges

You can get this error if your Ransomware Protection is enabled on your working directory. Windows does not allow any third party applications to make changes to files if the directory is protected by Ransomware Protection which comes with windows. You can rectify this by going to «Windows Security -> Virus and Threat Protection -> Manage Ransomware Protection -> Allow an app through controlled folder access»

Then add «Python[version].exe» by clicking Add an allowed app.

Wai Ha Lee's user avatar

Wai Ha Lee

8,55380 gold badges57 silver badges92 bronze badges

answered Aug 1, 2022 at 11:28

Jerin BS's user avatar

5

If you are a developer working with Python, you may have come across the «OSError: [Errno 9] Bad File Descriptor» error. This error can occur when you are working with files or sockets and can be frustrating to debug. In this guide, we will provide a comprehensive solution to troubleshoot this error.

Understanding the Error

Before we dive into the troubleshooting steps, let’s understand what this error means. The «OSError: [Errno 9] Bad File Descriptor» error is raised when a file descriptor that’s not open or is closed is used. A file descriptor is a unique number that identifies a file or socket. When you perform operations such as reading, writing, or closing a file or socket, the file descriptor associated with it is used.

Troubleshooting Steps

Follow these steps to troubleshoot the «OSError: [Errno 9] Bad File Descriptor» error:

Step 1: Check the File Descriptor

The first step is to check the file descriptor associated with the file or socket that’s causing the error. You can do this by printing the file descriptor before and after performing any operations on it. Here’s an example:

import os

# Open a file
file = open("file.txt", "r")

# Print the file descriptor
print(file.fileno())

# Perform operations on the file

# Close the file
file.close()

# Print the file descriptor again
print(file.fileno())

If the file descriptor is different before and after performing operations on it, it means that the file or socket has been closed or is not open. You need to make sure that the file or socket is open before performing any operations on it.

Step 2: Check for Exceptions

The next step is to check if any exceptions are being raised while performing operations on the file or socket. You can use a try-except block to catch any exceptions. Here’s an example:

try:
    # Perform operations on the file
except Exception as e:
    print("Exception:", e)

If an exception is being raised, it may be causing the «OSError: [Errno 9] Bad File Descriptor» error. You need to fix the exception before proceeding.

Step 3: Use a Context Manager

Using a context manager can ensure that the file or socket is closed properly after performing operations on it. Here’s an example using a context manager:

with open("file.txt", "r") as file:
    # Perform operations on the file

By using a context manager, you don’t have to worry about closing the file or socket manually.

Step 4: Use a Different File Descriptor

If none of the above steps work, you can try using a different file descriptor. You can use the os.dup() function to create a duplicate file descriptor. Here’s an example:

import os

# Open a file
file = open("file.txt", "r")

# Create a duplicate file descriptor
new_fd = os.dup(file.fileno())

# Perform operations on the file using the new file descriptor
# ...

# Close the file
file.close()

FAQ

Q1: What causes the «OSError: [Errno 9] Bad File Descriptor» error?

A1: This error is caused when a file descriptor that’s not open or is closed is used.

Q2: How do I check the file descriptor associated with a file or socket?

A2: You can print the fileno() method of the file or socket object.

Q3: What is a context manager?

A3: A context manager is an object that defines the methods __enter__() and __exit__(). It’s used with the with statement to ensure that resources are properly managed.

Q4: How do I create a duplicate file descriptor?

A4: You can use the os.dup() function to create a duplicate file descriptor.

Q5: Can I use the same file descriptor for multiple files or sockets?

A5: No, each file or socket has a unique file descriptor associated with it.

Conclusion

The «OSError: [Errno 9] Bad File Descriptor» error can be frustrating to debug, but by following the troubleshooting steps outlined in this guide, you should be able to resolve the issue. Remember to always check the file descriptor, catch exceptions, use a context manager, and try using a different file descriptor if all else fails.

@EdinyangaOttoho

OS Version: Windows 10
Python version: Version 3.6

Attached is a screenshot of the error

err

@abhinandanpawar

update python to 3.6 to 3.7 and try to run the same
output:
Microsoft Windows [Version 10.0.19041.450]
(c) 2020 Microsoft Corporation. All rights reserved.

C:Userswarrior>pip install pyinstaller
Collecting pyinstaller
Downloading pyinstaller-4.0.tar.gz (3.5 MB)
|████████████████████████████████| 3.5 MB 731 kB/s
Installing build dependencies … done
Getting requirements to build wheel … done
Preparing wheel metadata … done
Requirement already satisfied: setuptools in c:userswarriorappdatalocalprogramspythonpython37libsite-packages (from pyinstaller) (47.1.0)
Collecting pyinstaller-hooks-contrib>=2020.6
Downloading pyinstaller_hooks_contrib-2020.7-py2.py3-none-any.whl (152 kB)
|████████████████████████████████| 152 kB 544 kB/s
Collecting pywin32-ctypes>=0.2.0; sys_platform == «win32»
Downloading pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
Collecting altgraph
Downloading altgraph-0.17-py2.py3-none-any.whl (21 kB)
Collecting pefile>=2017.8.1; sys_platform == «win32»
Downloading pefile-2019.4.18.tar.gz (62 kB)
|████████████████████████████████| 62 kB 171 kB/s
Collecting future
Downloading future-0.18.2.tar.gz (829 kB)
|████████████████████████████████| 829 kB 386 kB/s
Using legacy ‘setup.py install’ for pefile, since package ‘wheel’ is not installed.
Using legacy ‘setup.py install’ for future, since package ‘wheel’ is not installed.
Building wheels for collected packages: pyinstaller
Building wheel for pyinstaller (PEP 517) … done
Created wheel for pyinstaller: filename=pyinstaller-4.0-py3-none-any.whl size=2789243 sha256=541fcc65d1fd514323e13c7e8b2bb9fe7f6ec02c31b17af50016b913ed96ba07
Stored in directory: c:userswarriorappdatalocalpipcachewheels3c2658a2ea56951fcffedcf013660fc2e820df7c9f341744b67e0fb
Successfully built pyinstaller
Installing collected packages: pyinstaller-hooks-contrib, pywin32-ctypes, altgraph, future, pefile, pyinstaller
Running setup.py install for future … done
Running setup.py install for pefile … done
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.0 pyinstaller-hooks-contrib-2020.7 pywin32-ctypes-0.2.0

C:Userswarrior>python —version
Python 3.7.8

@Rainbow-Musoke

Well am running on python 3.8.3 through anaconda. pip is installed but can not get to install any other package. it brings up the bad descriptor error 9
i have tried literally all the possible online solutions but nothing has worked yet. My laptop only has drive C an acer w10
help please

@EdinyangaOttoho

I am facing this issue too
No online hint has helped in all forums
I resorted to installing packages manually, but some still come up with the OS Error 09: Bad File Descriptor

@pfmoore

When you say «I resorted to installing packages manually» what precisely did you do? Unpack the wheel and manually copy the files into place? Or something else?

And when you say «some still come up with the OS Error 09: Bad File Descriptor», again, what do you mean? That packages you «installed manually» give that error? When? When you import them?

This issue seems like it’s affecting multiple people, but there’s so little information on how it occurs, or how to reproduce it, that there’s nothing much we can do until someone supplies more information.

@pcprashant88

if i am trying to install any module in pychrm then i get bad discriptor error. please give me a solution on this

@CHANDRASEKHAR613

pip install pyinstaller

for this still come up with the OS Error 09: Bad File Descriptor

@pcprashant88

@iProgram22

I installed python on my D drive and it fixed the error completely,

so try changing the location for the installation of python to some other drive and see if it works

@malcolmdurosaye

I experienced same error for two weeks but couldn’t find solution.
So, I tried to partition my hard drive (C:) and created a new volume (D:). I then installed python in the D: path and boom!. It works perfectly right now.

@shkemu

First think, check the script folder is empty, if folder is empty ensure the pip first use this command «python -m ensurepip» then check that folder it have some pip .exe files then will try «PIP install *» .or other wise change the directory.

Challenge

When using Linux repository with mounted NFS as a backup storage, the backup job may fail with «failed to lock file» errors:

 [14.03.2013 13:57:13] < 65541> ERR |Bad file descriptor
[14.03.2013 13:57:13] < 65541> >> |Failed to lock file [/storage/backups/job.vbk].

Cause

Some specific storages don’t support flock() file locking with their NFS implementation so the workaround is to disable flock() on the mount with nolock parameter. If it doesn’t help, then please change nfsvers=4 to nfsvers=3, it should help.
This issue is particularly known for DataDomain storage.

Solution

Use the following command to mount NFS:
mount -t nfs -o nolock,hard,intr,nfsvers=3,tcp,bg xxx.xxx.xxx.xxx:/aa/bb /dd/storage/backup

To submit feedback regarding this article, please click this link: Send Article Feedback
To report a typo on this page, highlight the typo with your mouse and press CTRL + Enter.

Questions

Is it because the .oa files are corrupted, or the disk is failing? Is this a common issue when using tar? Is there any way to repair this error?

It isn’t really relevant whether this issue is typical or not (I’ve not encountered it before), I’d start by attempting to tar up a single file and see if can’t isolate the issue a bit more, and also repeat it.

$ tar -cpSWf somefile.tar my/sub/dir/file1.oa

Also as an aside, you can save yourself a step and tar and compress all at once:

$ tar zcpSWf somefile.tar.gz ...

I would also look to take out the SW switches temporarily to see if that has any impact on your ability to tar these problematic files too.

If these errors are a warning that there are bad sectors on the HDD you might need to run an fsck or use a tool such as HDAT2 to attempt to repair any damaged sectors. This repair work may still leave the .oa file in a corrupted state, however.

Понравилась статья? Поделить с друзьями:
  • Backgroundtaskhost exe системная ошибка
  • Backgroundtaskhost exe ошибка приложения
  • Back ups pro 1200 ошибка f02
  • Back ups es 525 ошибки
  • Back 4 blood ошибка unknown file version