473,385 Members | 1,333 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,385 software developers and data experts.

Bug in threading.Thread.join() ?

I'm still trying to understand the behaviour that I'm
seeing but I'm already pretty sure that it's either
a bug, or something that would be considered a bug if
it didn't perhaps avoid even worse behaviour.

Inside the join() method of threading.Thread objects,
a Condition named self.__block is acquired, and then
the wait logic is executed. After the wait() finishes,
self.__block is released and the method returns.

If you hit Ctrl-C while the join's wait() is occurring,
you'll raise a KeyboardInterrupt and bypass the
release() call. (I'm observing this on Win XP with
Python 2.4 but have no reason to think it wouldn't
work the same on other platforms, given the docs
on signals and such.) If you do this, the thread
you were waiting for will never be able to complete
its cleanup because __bootstrap() calls __stop()
and that tries to acquire the same Condition object,
which has never been released. (I suspect this will
happen only if its the MainThread that is doing
the join() call since KeyboardInterrupts only occur
in the main thread.)

A simple try/finally in join() appears to solve the
problem, but I'm unsure that this is a good idea,
partly because I'm a little surprised nobody else has
found this problem before and I lack confidence that
I've really found a bug.

Anyone have thoughts on this? I'll file a bug
report shortly unless someone can point out the
error in my reasoning or a reason why this must be
the way it is.

-Peter
Jul 18 '05 #1
3 3193
[Peter Hansen]
I'm still trying to understand the behaviour that I'm
seeing but I'm already pretty sure that it's either
a bug, or something that would be considered a bug if
it didn't perhaps avoid even worse behaviour.

Inside the join() method of threading.Thread objects,
a Condition named self.__block is acquired, and then
the wait logic is executed. After the wait() finishes,
self.__block is released and the method returns.

If you hit Ctrl-C while the join's wait() is occurring,
you'll raise a KeyboardInterrupt and bypass the
release() call.

(I'm observing this on Win XP with
Python 2.4 but have no reason to think it wouldn't
work the same on other platforms, given the docs
on signals and such.)
Then you're doing something other than what you described. Here on
WinXP SP2 w/ Python 2.4c2:
import threading
class T(threading.Thread): .... def run(self):
.... import time
.... while True:
.... time.sleep(1)
.... t =T()
t.start()
t.join()


I can hit Ctrl+C all day at this point, and nothing (visible) happens.
That's because it's sitting in self.__block.wait(), which is in turn
sitting in waiter.acquire(), and it's simply not possible for Ctrl+C
to interrupt a mutex acquire.
If you do this, the thread you were waiting for will never be able
to complete its cleanup because __bootstrap() calls __stop()
and that tries to acquire the same Condition object,
which has never been released. (I suspect this will
happen only if its the MainThread that is doing
the join() call since KeyboardInterrupts only occur
in the main thread.)

A simple try/finally in join() appears to solve the
problem, but I'm unsure that this is a good idea,
partly because I'm a little surprised nobody else has
found this problem before and I lack confidence that
I've really found a bug.

Anyone have thoughts on this?


As above, I don't know what you're doing. Maybe you're doing a join()
with a timeout too? In that case, I doubt anyone gave any thought to
what happens if you muck with KeyboardInterrupt too.
Jul 18 '05 #2
Tim Peters wrote:
[Peter Hansen]
If you hit Ctrl-C while the join's wait() is occurring,
you'll raise a KeyboardInterrupt and bypass the
release() call.
Then you're doing something other than what you described. Here on
WinXP SP2 w/ Python 2.4c2: [snip] I can hit Ctrl+C all day at this point, and nothing (visible) happens.
That's because it's sitting in self.__block.wait(), which is in turn
sitting in waiter.acquire(), and it's simply not possible for Ctrl+C
to interrupt a mutex acquire.

As above, I don't know what you're doing. Maybe you're doing a join()
with a timeout too? In that case, I doubt anyone gave any thought to
what happens if you muck with KeyboardInterrupt too.


Yes, definitely doing this with a timeout value on the join().
Changing your example to do that (join(5), say) pretty
much demonstrates the trouble... in this case a traceback
for KeyboardInterrupt is printed, but the program does not
terminate because of the thread stuck at the __stop()'s
__acquire().

I'll take your last sentence as a form of blessing to go
file a bug report, unless you don't think that's a good idea.

Thanks, Tim!

-Peter
Jul 18 '05 #3
Peter Hansen wrote:
I'll take your last sentence as a form of blessing to go
file a bug report...


Filed as
http://sourceforge.net/tracker/index...70&atid=105470

I guess it makes more sense for any further discussion to
go on there...

(Coincidentally, another bug report was filed just a few days
ago for a closely related item. As Tim mentioned, the wait()
without a timeout can never be interrupted by Ctrl-C, and
somebody else reported that as a bug (1167930) only about three
days before I encountered this one.)

-Peter
Jul 18 '05 #4

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

Similar topics

2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
3
by: ron | last post by:
If I use the System.Threading.ThreadPool.QueueUserWorkItem to spawn a new threaded process, will this process be executed regardless of whether or not the current process ends prior to completion...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
8
by: MattB | last post by:
Hello I am starting a new thread in a button click event. This thread calls an method which sends emails, I don't want the page to wait for the emails to finish going out as it slows the user...
9
by: AdrianJMartin | last post by:
Hi all, I have a need for a STA thread from asp.net. I can create the thread and it runs fine. But when it is finished, the thread still 'hangs' arround. Visible only to the debugger..... I get...
9
by: akrapus | last post by:
Hi, I am trying to understand how to use threading in Python. I get threading as a concept, but not the implementation. In order to start threading, do you call it as a separate function,...
2
by: Daniel | last post by:
I have a class similar to this: class MyThread(threading.Thread): def __init__(self): self.terminated = False def run(self): while not self.terminated:
0
by: Edwin.Madari | last post by:
1. check out the Caveats for thread module: http://docs.python.org/lib/module-thread.html Threads interact strangely with interrupts: the KeyboardInterrupt exceptionwill be received by an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.