473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 16193
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.