JDK-8306434 : add support of virtual threads to JVMTI StopThread
  • Type: CSR
  • Component: hotspot
  • Sub-Component: jvmti
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 21
  • Submitted: 2023-04-19
  • Updated: 2023-05-22
  • Resolved: 2023-04-20
Related Reports
Blocks :  
CSR :  
Relates :  
Description
Summary
-------

The JVMTI `StopThread` function is updated to support virtual threads.

Problem
-------

Currently the JVMTI `StopThread` function may be used to send an asynchronous
exception to a platform thread. It needs to be extended to support virtual threads as well.

Solution
--------

The JVMTI spec for `StopThread` is updated so that it can be used to send an asynchronous
exception to a virtual thread in some situations.

The error code `JVMTI_ERROR_UNSUPPORTED_OPERATION` is removed. Two new error codes are added instead: `JVMTI_ERROR_THREAD_NOT_SUSPENDED` and `JVMTI_ERROR_OPAQUE_FRAME`.

Specification
-------------

The JVMTI spec includes the following changes below:

The `StopThread` function description is updated to remove the word "platform" from the statement:

    Send the specified asynchronous exception to the specified platform thread.

The description of the `StopThread` 'thread' parameter is updated to replace the statement:

    The thread may not be a virtual thread. Otherwise, the error
    code JVMTI_ERROR_UNSUPPORTED_OPERATION will be returned.

with:

    The StopThread function may be used to send an asynchronous
    exception to a virtual thread when it is suspended at an event.
    An implementation may support sending an asynchronous exception
    to a suspended virtual thread in other cases.

The following `StopThread` error code description is removed:

    JVMTI_ERROR_UNSUPPORTED_OPERATION: is a virtual thread.

The following two `StopThread` error code descriptions are added:

    JVMTI_ERROR_THREAD_NOT_SUSPENDED:
    The thread is a virtual thread and was not suspended and
    was not the current thread.

    JVMTI_ERROR_OPAQUE_FRAME:
    The thread is a suspended virtual thread and the implementation 
    was unable to throw an asynchronous exception from the current frame.

A general description of the `JVMTI_ERROR_OPAQUE_FRAME` error was:

    Information about the frame is not available (e.g. for native frames).

It is extended to be:

    Information about the frame is not available (e.g. for native frames),
    or the function cannot be performed on the thread's current frame.


The full jvmti.xml diff is:

    diff --git a/src/hotspot/share/prims/jvmti.xml b/src/hotspot/share/prims/jvmti.xml
    index 456876fbcb9..cbb0cb6c84b 100644
    --- a/src/hotspot/share/prims/jvmti.xml
    +++ b/src/hotspot/share/prims/jvmti.xml
    @@ -1892,7 +1892,7 @@ jvmtiEnv *jvmti;
         <function id="StopThread" num="7">
           <synopsis>Stop Thread</synopsis>
           <description>
    -        Send the specified asynchronous exception to the specified platform thread.
    +        Send the specified asynchronous exception to the specified thread.
           </description>
           <origin>jvmdi</origin>
           <capabilities>
    @@ -1903,8 +1903,10 @@ jvmtiEnv *jvmti;
               <jthread impl="noconvert"/>
                 <description>
                   The thread to stop.
    -              The <code>thread</code> may not be a virtual thread. Otherwise, the error code
    -              <errorlink id="JVMTI_ERROR_UNSUPPORTED_OPERATION"></errorlink> will be returned.
    +              The <functionlink id="StopThread"></functionlink> function may be used to send
    +              an asynchronous exception to a virtual thread when it is suspended at an event.
    +              An implementation may support sending an asynchronous exception to a suspended
    +              virtual thread in other cases.
                  </description>
             </param>
             <param id="exception">
    @@ -1915,8 +1917,12 @@ jvmtiEnv *jvmti;
             </param>
           </parameters>
           <errors>
    -        <error id="JVMTI_ERROR_UNSUPPORTED_OPERATION">
    -          <paramlink id="thread"/> is a virtual thread.
    +        <error id="JVMTI_ERROR_THREAD_NOT_SUSPENDED">
    +          Thread is a virtual thread and was not suspended and was not the current thread.
    +        </error>
    +        <error id="JVMTI_ERROR_OPAQUE_FRAME">
    +          The thread is a suspended virtual thread and the implementation was unable
    +          to throw an asynchronous exception from the current frame.
             </error>
           </errors>
         </function>
    @@ -11974,7 +11980,8 @@ myInit() {
           There are no Java programming language or JNI stack frames at the specified depth.
         </errorid>
         <errorid id="JVMTI_ERROR_OPAQUE_FRAME" num="32">
    -      Information about the frame is not available (e.g. for native frames).
    +      Information about the frame is not available (e.g. for native frames),
    +      or the function cannot be performed on the thread's current frame.
         </errorid>
         <errorid id="JVMTI_ERROR_DUPLICATE" num="40">
           Item already set.




Comments
Thank you, Joe!
20-04-2023

Moving to Approved.
20-04-2023

Added a link to the related debugger CSR.
20-04-2023

Thanks, David!
20-04-2023

This wording seems fine to me. Thanks. Minor editing and formatting changes applied.
20-04-2023

Finalized now,
20-04-2023

Added some debugger related corrections to the "Compatibility Risk" section as Chris suggested.
19-04-2023

We had a small discussion with Chris. I've made needed corrections. Now, it is: "Thread is a virtual thread and was not suspended and was not the current thread." Also, corrected this statement to be consistent: "The thread is a suspended virtual thread and the implementation was unable to throw an asynchronous exception from this frame."
19-04-2023

I see Chris has changed the description of JVMTI_ERROR_THREAD_NOT_SUSPENDED. Using "Thread is" is better than "Thread was" but does mean changing the other existing usages (several functions) to be consistent.
19-04-2023

JVMTI StopThread was low priority for Java 19/20 so it was just short term that it returned the "not supported" error. The proposal here to require that a virtual thread be suspended at an event, and the errors returned, look fine. As the CSR noted, there will be follow-up adjustments to JDWP and JDI on this feature.
19-04-2023