Python time.sleep() raises exception in multi-threaded app

Bug #191509 reported by Sergey Chistovich
16
Affects Status Importance Assigned to Milestone
python2.5 (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

Binary package hint: python2.5-minimal

Ubuntu 7.10
Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)

Sometimes, time.sleep() in python raises "IOError: [Errno 22] Invalid argument" in multi-threaded app. This exception is similar to what is raised when sleep()'s argument is non-positive, but it isn't the case. I believe that it happens (rarely) when in another thread an exception is raised (even if it's catched later). It happens both when I call sleep() directly from my code or indirectly from a standard python library (for example threading.Condition class uses sleep, and the bug reproduces with it, too).

Here goes the script with which I reproduce this bug (about two or threee times in ten runs of the script). With the same script at Debian 3.1, python 2.4.1, I can't reproduce the bug (and I believe that it really doesn't happen there).

#!/usr/bin/python2.5
import threading, time, random, sys

stopped = False

def do_sleep():
    while not stopped:
        time.sleep(random.random()/1000 + 0.001)

class MyException(Exception):
    pass

def do_raise():
    while not stopped:
        try:
            time.sleep(0.01)
            raise MyException
        except:
            pass

sleepers = [threading.Thread(target = do_sleep) for i in range(100)]
raisers = [threading.Thread(target = do_raise) for i in range(100)]

for s in sleepers: s.start()
for s in raisers: s.start()

for i in range(100):
    print ',',
    sys.stdout.flush()
    time.sleep(1)
stopped = True

for s in sleepers: s.join()
for s in raisers: s.join()
print '.'

Revision history for this message
Sergey Chistovich (theigel) wrote :

Yes, and exception (for the script above) looks like this:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,Exception in thread Thread-81:
Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 460, in __bootstrap
    self.run()
  File "/usr/lib/python2.5/threading.py", line 440, in run
    self.__target(*self.__args, **self.__kwargs)
  File "./threadtest.py", line 8, in do_sleep
    time.sleep(random.random()/1000 + 0.001)
IOError: [Errno 22] Invalid argument

Revision history for this message
Sergey Chistovich (theigel) wrote :

After some investigation, it seems it only reproduces on 64bit kernel.

2.6.20-100-server-amd64 #2 SMP Sat Jun 2 10:53:09 UTC 2007 x86_64 GNU/Linux

(running on a virtual box)

Changed in python2.5:
status: New → Confirmed
Revision history for this message
Matthias Klose (doko) wrote :

checked in jaunty with 2.5.4 and 2.6.1, not seeing this traceback anymore.

Changed in python2.5 (Ubuntu):
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.