473,500 Members | 1,464 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 16198
In article <Ka***********************@news1.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**@pythoncraft.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*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.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.DeleteFile), 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
1751
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 ...
9
29202
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...
13
2689
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...
4
2057
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...
4
5974
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...
9
10857
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...
7
6836
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...
1
3181
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. ...
3
1836
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...
0
7147
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7204
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,...
0
7241
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5502
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,...
1
4928
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...
0
4618
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...
0
3114
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
318
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...

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.