Skip to content

Commit d79b68e

Browse files
committed
8218483: Crash in "assert(_daemon_threads_count->get_value() > daemon_count) failed: thread count mismatch 5 : 5"
Reviewed-by: stuefe Backport-of: 2f20909
1 parent 2212683 commit d79b68e

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

src/hotspot/share/prims/jni.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4256,7 +4256,7 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae
42564256

42574257
if (attach_failed) {
42584258
// Added missing cleanup
4259-
thread->cleanup_failed_attach_current_thread();
4259+
thread->cleanup_failed_attach_current_thread(daemon);
42604260
return JNI_ERR;
42614261
}
42624262

src/hotspot/share/runtime/thread.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,10 @@ void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
19991999
_timer_exit_phase1.stop();
20002000
_timer_exit_phase2.start();
20012001
}
2002+
2003+
// Capture daemon status before the thread is marked as terminated.
2004+
bool daemon = is_daemon(threadObj());
2005+
20022006
// Notify waiters on thread object. This has to be done after exit() is called
20032007
// on the thread (if the thread is the last thread in a daemon ThreadGroup the
20042008
// group should have the destroyed bit set before waiters are notified).
@@ -2072,7 +2076,7 @@ void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
20722076
_timer_exit_phase4.start();
20732077
}
20742078
// Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
2075-
Threads::remove(this);
2079+
Threads::remove(this, daemon);
20762080

20772081
if (log_is_enabled(Debug, os, thread, timer)) {
20782082
_timer_exit_phase4.stop();
@@ -2090,7 +2094,7 @@ void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
20902094
}
20912095
}
20922096

2093-
void JavaThread::cleanup_failed_attach_current_thread() {
2097+
void JavaThread::cleanup_failed_attach_current_thread(bool is_daemon) {
20942098
if (active_handles() != NULL) {
20952099
JNIHandleBlock* block = active_handles();
20962100
set_active_handles(NULL);
@@ -2112,7 +2116,7 @@ void JavaThread::cleanup_failed_attach_current_thread() {
21122116

21132117
BarrierSet::barrier_set()->on_thread_detach(this);
21142118

2115-
Threads::remove(this);
2119+
Threads::remove(this, is_daemon);
21162120
this->smr_delete();
21172121
}
21182122

@@ -4440,7 +4444,7 @@ void Threads::add(JavaThread* p, bool force_daemon) {
44404444
Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
44414445
}
44424446

4443-
void Threads::remove(JavaThread* p) {
4447+
void Threads::remove(JavaThread* p, bool is_daemon) {
44444448

44454449
// Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
44464450
ObjectSynchronizer::omFlush(p);
@@ -4469,19 +4473,16 @@ void Threads::remove(JavaThread* p) {
44694473
}
44704474

44714475
_number_of_threads--;
4472-
oop threadObj = p->threadObj();
4473-
bool daemon = true;
4474-
if (!is_daemon(threadObj)) {
4476+
if (!is_daemon) {
44754477
_number_of_non_daemon_threads--;
4476-
daemon = false;
44774478

44784479
// Only one thread left, do a notify on the Threads_lock so a thread waiting
44794480
// on destroy_vm will wake up.
44804481
if (number_of_non_daemon_threads() == 1) {
44814482
Threads_lock->notify_all();
44824483
}
44834484
}
4484-
ThreadService::remove_thread(p, daemon);
4485+
ThreadService::remove_thread(p, is_daemon);
44854486

44864487
// Make sure that safepoint code disregard this thread. This is needed since
44874488
// the thread might mess around with locks after this point. This can cause it

src/hotspot/share/runtime/thread.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ class JavaThread: public Thread {
11791179
};
11801180
void exit(bool destroy_vm, ExitType exit_type = normal_exit);
11811181

1182-
void cleanup_failed_attach_current_thread();
1182+
void cleanup_failed_attach_current_thread(bool is_daemon);
11831183

11841184
// Testers
11851185
virtual bool is_Java_thread() const { return true; }
@@ -2172,7 +2172,7 @@ class Threads: AllStatic {
21722172
// force_daemon is a concession to JNI, where we may need to add a
21732173
// thread to the thread list before allocating its thread object
21742174
static void add(JavaThread* p, bool force_daemon = false);
2175-
static void remove(JavaThread* p);
2175+
static void remove(JavaThread* p, bool is_daemon);
21762176
static void non_java_threads_do(ThreadClosure* tc);
21772177
static void java_threads_do(ThreadClosure* tc);
21782178
static void java_threads_and_vm_thread_do(ThreadClosure* tc);

test/hotspot/gtest/utilities/utilitiesHelper.inline.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class VMThreadBlocker : public JavaThread {
5858
VM_StopSafepoint ss(_unblock);
5959
VMThread::execute(&ss);
6060
_done->signal();
61-
Threads::remove(this);
61+
Threads::remove(this, false);
6262
this->smr_delete();
6363
}
6464
void doit() {
@@ -99,7 +99,7 @@ class JavaTestThread : public JavaThread {
9999
}
100100

101101
void postrun() {
102-
Threads::remove(this);
102+
Threads::remove(this, false);
103103
_post->signal();
104104
this->smr_delete();
105105
}

0 commit comments

Comments
 (0)