What happens to a thread with an unhandled exception?

F

Frank Millman

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
 
F

Frank Millman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top