--- old/src/share/vm/runtime/thread.cpp Tue May 3 04:08:17 2016 +++ new/src/share/vm/runtime/thread.cpp Tue May 3 04:08:16 2016 @@ -1326,6 +1326,15 @@ _watcher_thread = NULL; Terminator_lock->notify(); } + + // Deletion is done synchronously by the thread terminating the VM + // so that the WatcherThread deletion doesn't race with that thread as it + // tears down VM resources. That means 'this' may already have been + // deallocated so we can't reference it. However we must do some cleanup + // ourselves before allowing the native thread to terminate + + ThreadLocalStorage::set_thread(NULL); + } void WatcherThread::start() { @@ -1344,6 +1353,7 @@ } void WatcherThread::stop() { + WatcherThread* watcher = NULL; { // Follow normal safepoint aware lock enter protocol since the // WatcherThread is stopped by another JavaThread. @@ -1350,7 +1360,7 @@ MutexLocker ml(PeriodicTask_lock); _should_terminate = true; - WatcherThread* watcher = watcher_thread(); + watcher = watcher_thread(); if (watcher != NULL) { // unpark the WatcherThread so it can see that it should terminate watcher->unpark(); @@ -1373,6 +1383,10 @@ Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0, Mutex::_as_suspend_equivalent_flag); } + + // can't get here till the WatcherThread has indicated it is terminating + if (watcher != NULL) + delete watcher; } void WatcherThread::unpark() {