473,563 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading issue: [Error 9]: bad file descriptor

Has anyone else run into random IOErrors ([Error 9] bad file descriptor) in
multi-threaded Python apps?

I've searched the newsgroups, and the only references I can find to that
seem to be sporadically related to threading, but nobody seems to have a
"fix" or even a clear understanding of what the issue is.

The app I'm running into this with (once in a while... not really repeatable
predictably) is using 3 threads, and I'm using locks around all shared
objects. The error seems to be happening mostly when I'm trying to delete a
temporary file that was originally created by a different thread.

ANy suggestions? Thanks,

Kevin.
Jul 18 '05 #1
4 16202
In article <Ka************ ***********@new s1.calgary.shaw .ca>,
Kevin <ot***@cazabon. com> wrote:

Has anyone else run into random IOErrors ([Error 9] bad file
descriptor) in multi-threaded Python apps?

The app I'm running into this with (once in a while... not really
repeatable predictably) is using 3 threads, and I'm using locks around
all shared objects. The error seems to be happening mostly when I'm
trying to delete a temporary file that was originally created by a
different thread.


Sounds like the problem isn't locking per se, but lack of
synchronization . The simple answer: never use an external object in
multiple threads.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"Not everything in life has a clue in front of it...." --JMS
Jul 18 '05 #2
On Sun, 6 Jul 2003, Kevin wrote:
Has anyone else run into random IOErrors ([Error 9] bad file descriptor) in
multi-threaded Python apps?


Not indicating which of the platforms with Python supported threads you're
experiencing this on doesn't give much scope for others to help...

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullsey e.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.or g.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #3
On Sun, 2003-07-06 at 22:37, Kevin wrote:
My oversight, thanks!

I'm testing on Windows 2000 Professional, but have had other users report
the same problem on other Windows flavours (NT, 98).


I experienced a similar problem a few years ago (on Windows NT Server).
A long-running threaded application would sporadically raise an
exception when writing to a log file. Never figured out the cause, but
"solved" it by catching the exception and reopening the file (IIRC).

Regards,

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555
Jul 18 '05 #4
"Kevin" <ot***@cazabon. com> writes:
My oversight, thanks!

I'm testing on Windows 2000 Professional, but have had other users report
the same problem on other Windows flavours (NT, 98).


I can only speak for NT, but I have seen timing issues in the past
where there is some additional latency imposed by the system between
when I release all references to a file and when it can be removed
from the filesystem by the same process. This has affected me in C
code as well as Python. Perhaps you're running into the same thing,
although for me it normally showed up as a permission denied error.

Sometimes the higher level Python I/O exceptions can be a bit vague
(by their nature, a lot of underlying Win32 error codes eventually get
translated into a far fewer number of C RTL error codes, which in turn
bubble up as Python exceptions). One thing you could try, depending
on the operation in question, is to replace the Python operation (such
as os.remove) with a matching win32all module operation (such as
win32file.Delet eFile), which should raise a more specific exception.

In my past experiences, the most practical, albeit inelegant,
workaround was to retry a failed removal operation after several
seconds. Definitely a kluge, but I was never able to isolate any more
well-defined approach.

This of course assumes that you don't really have a threading race
condition under which you still do hold active handles to the file
that you are trying to remove. The fact that you're getting back an
invalid handle error certainly makes it seem that you make still be
looking at a race condition or inter-thread data corruption within
your application.

Depending on the structure of the application, if you can wrap (or if
you have already) all access to your file handles through a common
class you should be able to instrument it in order to at least get
some sort of trace as to how they are accessed, and perhaps catch one
of the failures. But as you probably know, debugging this sort of
thing can be nasty, particularly if it's truly sporadic in nature.

-- David
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1755
by: Ajay | last post by:
hi! my application is throwing a bad socket error raise error(EBADF, 'Bad file descriptor') socket.error: (9, 'Bad file descriptor') basically i have the following interchange application service <server1> <client1>
9
29213
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get the error message:
13
2700
by: Varun | last post by:
Hi Friends, Department of Information Technology, Madras Institute of Technology, Anna University, India is conducting a technical symposium, Samhita. As a part of samhita, an Online Programming Contest is scheduled on Sunday, 27 Feb 2005. This is the first Online Programming Contest in India to support Python !!!!. Other languages...
4
2064
by: Nicolas Fleury | last post by:
Hi, Is there any way to get the file descriptor on Unix or handle on Windows associated internally with a threading.Event object? So that it can be used in a call to select or WaitForMultipleObjects. Thx and regards, Nicolas
4
5984
by: lynology | last post by:
I need help trying to figure why this piece of code gives me a "Bad File descriptor error" everytime I try to run it and invoke fflush. This piece of code simple outputs a char string to the output stream. What ends up happening instead is that when outputting the first character string to the channel CG_cdukeypad_CHA.Scrpad, its gives the...
9
10874
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent the wheel, right? Everything so far is working well with the Active Directory. The problem I am having is with adding File Permissions to a...
7
6851
by: news | last post by:
Recently our mail from our e-commerce site has been rejected by AOL due to an IP block because someone was using our PHP scripts to send spam. Well, I got that fixed. But our legitimate auto-generated e-mails are getting "deferred" by AOL now with an error: Deferred: Bad file descriptor I can't find anything on their support site about...
1
3189
by: hg | last post by:
Hi, I have two classes, one listens and accepts connections, gets the socket, reads the first byte from the opened socket then launches a thread (threading) and gives it the socket identifier. The first time the thread attempts to read from the given socket, socket.py raises a bad file descriptor in _dummy. I tried using plain threads...
3
1841
by: ayoung | last post by:
I'm trying to do a 'simple' program that reads formatted text from a file, then prints to the screen, then reads text from the keyboard, and writes to a file. However, it is not reading the text from the file, or printing the contents to the screen. it is however writing the text to the textfile. Any ideas whats going wrong? #include...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.