Fixed race condition associated with closing an activity before a thread
is finished.