@@ -179,8 +179,8 @@ setThreadLocalStorage(jthread thread, ThreadNode *node)
179179
180180 error = JVMTI_FUNC_PTR (gdata -> jvmti ,SetThreadLocalStorage )
181181 (gdata -> jvmti , thread , (void * )node );
182- if ( error == JVMTI_ERROR_THREAD_NOT_ALIVE ) {
183- /* Just return, thread hasn't started yet */
182+ if ( error == JVMTI_ERROR_THREAD_NOT_ALIVE && node == NULL ) {
183+ /* Just return. This can happen when clearing the TLS. */
184184 return ;
185185 } else if ( error != JVMTI_ERROR_NONE ) {
186186 /* The jthread object must be valid, so this must be a fatal error */
@@ -244,25 +244,17 @@ findThread(ThreadList *list, jthread thread)
244244 /* Get thread local storage for quick thread -> node access */
245245 node = getThreadLocalStorage (thread );
246246
247- /* In some rare cases we might get NULL, so we check the list manually for
248- * any threads that we could match.
249- */
250247 if ( node == NULL ) {
251- JNIEnv * env ;
252-
253- env = getEnv ();
254- if ( list != NULL ) {
255- node = nonTlsSearch (env , list , thread );
256- } else {
257- node = nonTlsSearch (env , & runningThreads , thread );
258- if ( node == NULL ) {
259- node = nonTlsSearch (env , & otherThreads , thread );
260- }
261- }
262- if ( node != NULL ) {
263- /* Here we make another attempt to set TLS, it's ok if this fails */
264- setThreadLocalStorage (thread , (void * )node );
248+ /*
249+ * If the thread was not yet started when the ThreadNode was created, then it
250+ * got added to the otherThreads list and its thread local storage was not set.
251+ * Search for it in the otherThreads list.
252+ */
253+ if ( list == NULL || list == & otherThreads ) {
254+ node = nonTlsSearch (getEnv (), & otherThreads , thread );
265255 }
256+ /* A thread with no TLS should never be in the runningThreads list. */
257+ JDI_ASSERT (!nonTlsSearch (getEnv (), & runningThreads , thread ));
266258 }
267259
268260 /* If a list is supplied, only return ones in this list */
@@ -380,10 +372,14 @@ insertThread(JNIEnv *env, ThreadList *list, jthread thread)
380372#endif
381373
382374 /* Set thread local storage for quick thread -> node access.
383- * Some threads may not be in a state that allows setting of TLS,
384- * which is ok, see findThread, it deals with threads without TLS set.
375+ * Threads that are not yet started do not allow setting of TLS. These
376+ * threads go on the otherThreads list and have their TLS set
377+ * when moved to the runningThreads list. findThread() knows to look
378+ * on otherThreads when the TLS lookup fails.
385379 */
386- setThreadLocalStorage (node -> thread , (void * )node );
380+ if (list != & otherThreads ) {
381+ setThreadLocalStorage (node -> thread , (void * )node );
382+ }
387383 }
388384
389385 return node ;
@@ -2111,6 +2107,8 @@ threadControl_onEventHandlerEntry(jbyte sessionID, EventInfo *evinfo, jobject cu
21112107 node = findThread (& otherThreads , thread );
21122108 if (node != NULL ) {
21132109 moveNode (& otherThreads , & runningThreads , node );
2110+ /* Now that we know the thread has started, we can set its TLS.*/
2111+ setThreadLocalStorage (thread , (void * )node );
21142112 } else {
21152113 /*
21162114 * Get a thread node for the reporting thread. For thread start
0 commit comments