By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

What happens to a thread with an unhandled exception?

P: n/a
Hi all

I am doing something which works, but I have a gut feel that it cannot
be relied upon. Can someone confirm this one way or the other.

I have a multi-threaded server, which responds to client logins and
sets up a thread for each active session. A thread can stay active for
a long time.

I use a subclass of threading.Thread to handle the session, to which I
have added a number of attributes and methods, plus a reference back
to the main thread. When a user logs in, I call a 'setup' method in
the thread, which among other things updates a dictionary in the main
thread to record the fact that they are logged in. When they log out,
I call a 'cleanup' method in the thread, which among other things
removes their id from the dictionary of active logins.

It sometimes happens that an unhandled exception occurs in a thread.
The main thread continues, other threads are unaffected, and users can
continue to log in. However, the user that was active at the time
cannot log back in as their cleanup method was never called.

There are a number of ways to handle this, but I found one which seems
quite effective. In the dictionary of active logins, I store a
reference to the thread which is managing the session. If the user
tries to log back in, I can detect that the thread is in a suspended
state, and I use the reference to invoke the thread's cleanup method.
This removes the entry from the dictionary of active logins and
enables the user to log in again successfully.

My worry is that the thread with the unhandled exception may
eventually get garbage-collected, in which case the cleanup method
will no longer be accessible. Could this happen, or does the thread
stay in memory until termination of the main program?

Thanks for any pointers.

Frank Millman

Jun 29 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Jun 29, 2:51 pm, Frank Millman <f...@chagford.comwrote:
Hi all

I am doing something which works, but I have a gut feel that it cannot
be relied upon. Can someone confirm this one way or the other.
[...]
My worry is that the thread with the unhandled exception may
eventually get garbage-collected, in which case the cleanup method
will no longer be accessible. Could this happen, or does the thread
stay in memory until termination of the main program?
I just noticed Diez's reply to a related question earlier today, where
he suggested wrapping the thread's run() method in a try/finally
construct. It works perfectly in my situation - if there is an
unhandled exception, my cleanup method is called before the exception
is raised. Thanks, Diez (even though you did not realise you were
helping me).

Frank

Jun 29 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.