<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Hi Chris, <br>
    <br>
    I tried it on jdk 23 and it works the same way :(<br>
    It is clear that calling Thread.interrupted() in debugger should
    modify the interrupted state, but it does not...<br>
    It looks like the debugger is taking thread interrupted state from
    somewhere else, and also somehow calling different methods...<br>
    Check the attached video for more details.<br>
    <br>
    Thanks!<br>
    Egor<br>
    <br>
    <div class="moz-cite-prefix">On 20.08.2024 18:27, Chris Plummer
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:28938cd9-aaf4-46e5-8857-3ddf40131baa@oracle.com">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <p>The presence of the breakpoint likely results in the debug
        agent executing code that does a JVMTI RawMonitorWait. However,
        there is code in place in the debug agent to repost the
        interrupt if it happens before or during the wait. See the code
        and comment in debugMonitorWait() and its call to
        handleInterrupt(), which calls
        threadControl_setPendingInterrupt(). It seems this code is
        failing somehow.</p>
      <p><br>
      </p>
      <p>There were changes during JDK 23 in this area for virtual
        thread support, and the changes possibly could impact platform
        threads also. See <a class="issue-link"
          data-issue-key="JDK-8324868"
          href="https://bugs.openjdk.org/browse/JDK-8324868"
          id="key-val" rel="5120243" moz-do-not-send="true">JDK-8324868</a>
        and <a class="issue-link" data-issue-key="JDK-8325187"
          href="https://bugs.openjdk.org/browse/JDK-8325187"
          id="key-val" rel="5120651" moz-do-not-send="true">JDK-8325187</a>.
        It would be worth trying with the latest sources to see if the
        issue is still reproducible.</p>
      <p><br>
      </p>
      <p>Regarding calling Thread.currentThread() from the watch panel,
        the use of the JDI method invocation support can change the
        state of the program being debugged. So if you do an invoke on
        the interrupted thread, it would not surprise me if the
        interrupt got cleared as a result, and I think this is to be
        expected.<br>
      </p>
      <p><br>
      </p>
      <p>Chris</p>
      <p><br>
      </p>
      <div class="moz-cite-prefix">On 8/20/24 6:47 AM, Gillespie, Oli
        wrote:<br>
      </div>
      <blockquote type="cite"
cite="mid:01000191700af6b8-7ceb0a93-3a3b-44b5-b37d-d52010a0eaa2-000000@email.amazonses.com">
        <style type="text/css" style="display:none;">P {margin-top:0;margin-bottom:0;}</style>
        <div id="divtagdefaultwrapper"
style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;"
          dir="ltr">
          <p>There are lots of paths that clear the interrupted flag. In
            your example, I hit at least this one when calling
            Thread.currentThread from the watch panel:<br>
            <br>
            ```<br>
          </p>
          <div>    at
            java.base/java.lang.Thread.interrupted(Thread.java:1030)<br>
                at
            java.base/jdk.internal.loader.Resource.getBytes(Resource.java:96)<br>
                at
java.base/jdk.internal.loader.URLClassPath$JarLoader$2.getBytes(URLClassPath.java:895)<br>
                at
java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:859)<br>
                at
java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)<br>
                at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)<br>
                at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)<br>
                at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)<br>
                at
            java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)<br>
                at java.base/java.lang.Class.forName0(Native Method)<br>
                at java.base/java.lang.Class.forName(Class.java:467)</div>
          <br>
          ```<br>
          <br>
          but the key for your example seems to be <a
href="https://github.com/openjdk/jdk17u/blob/master/src/hotspot/share/prims/jvmtiRawMonitor.cpp"
            class="OWAAutoLink moz-txt-link-freetext"
            moz-do-not-send="true">
https://github.com/openjdk/jdk17u/blob/master/src/hotspot/share/prims/jvmtiRawMonitor.cpp</a>.
          If you replace is_interrupted(true) with is_interrupted(false)
          in the 3 calls from that file, your example works as you
          expect. That might help your investigations.<br>
          <br>
          Oli<br>
        </div>
        <hr style="display:inline-block;width:98%" tabindex="-1">
        <div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt"
            face="Calibri, sans-serif" color="#000000"><b>From:</b>
            serviceability-dev <a class="moz-txt-link-rfc2396E"
              href="mailto:serviceability-dev-retn@openjdk.org"
              moz-do-not-send="true"><serviceability-dev-retn@openjdk.org></a>
            on behalf of Egor Ushakov <a class="moz-txt-link-rfc2396E"
              href="mailto:egor.ushakov@jetbrains.com"
              moz-do-not-send="true"><egor.ushakov@jetbrains.com></a><br>
            <b>Sent:</b> 20 August 2024 13:32:36<br>
            <b>To:</b> serviceability-dev<br>
            <b>Subject:</b> [EXTERNAL] Thread.interrupted() is always
            false in debugger</font>
          <div> </div>
        </div>
        <div>
          <div class="WordSection1">
            <table class="MsoTableGrid"
              style="border-collapse:collapse;border:none"
              cellspacing="0" cellpadding="0" border="1">
              <tbody>
                <tr style="height:15.25pt">
                  <td
style="width:842.35pt;border:solid #ED7D31 1.5pt;padding:0in 5.4pt 0in 5.4pt;height:15.25pt"
                    width="711" valign="top">
                    <p><strong><span style="background:#FFFF99">CAUTION</span></strong><span
                        style="background:#FFFF99">: This email
                        originated from outside of the organization. Do
                        not click links or open attachments unless you
                        can confirm the sender and know the content is
                        safe.</span><o:p></o:p></p>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
          <br>
          <div>Hi everyone!<br>
            <br>
            we have a long standing issue <a
              class="moz-txt-link-freetext"
              href="https://youtrack.jetbrains.com/issue/IDEA-169706"
              moz-do-not-send="true">
              https://youtrack.jetbrains.com/issue/IDEA-169706</a><br>
            Maybe someone could clarify why it happens and if there's a
            possible workaround.<br>
            Here's the simplified test case:<br>
            <div style="background-color:#ffffff;color:#080808">
              <pre
style="font-family:'Source Code Pro',monospace;font-size:12,0pt;"><span
              style="color:#0033b3;">public class </span><span
              style="color:#000000;">Interruption </span>{
    <span style="color:#0033b3;">public static void </span><span
              style="color:#00627a;">main</span>(<span
              style="color:#000000;">String</span>[] <span
              style="color:#000000;">args</span>) {
        <span style="color:#000000;">Thread thread </span>= <span
              style="color:#000000;">Thread</span>.<span
              style="font-style:italic;">currentThread</span>();
        <span style="color:#000000;">thread</span>.interrupt();

        <span style="color:#0033b3;">if </span>(<span
              style="color:#000000;">Thread</span>.<span
              style="font-style:italic;">interrupted</span>()) { // <----- stop on a breakpoint here 
            <span style="color:#000000;">System</span>.<span
              style="color:#871094;font-style:italic;">out</span>.println(<span
              style="color:#067d17;">"Interrupted"</span>);
        } <span style="color:#0033b3;">else </span>{
            <span style="color:#000000;">System</span>.<span
              style="color:#871094;font-style:italic;">out</span>.println(<span
              style="color:#067d17;">"Not interrupted"</span>);
        }
    }
}

Obviously the program prints:
<span style="color:#067d17;">Interrupted

</span><font color="#000000">But if stopped on a breakpoint in the debugger, evaluation of </font>
<span style="color:#000000;">Thread</span>.<span
              style="font-style:italic;">interrupted</span>() always returns false.
More of that <i>interrupted </i>field value in the thread object is also reported as false:
<img alt="" src="cid:part1.9GuYg32c.gGVcBYDP@jetbrains.com" class="">

If interrupted method is debugged step by step it is obvious that interrupted value is true, 
but the debugger continue to show it as false.

Any ideas why it could happen and how to fix it?

Thanks!
Egor
</pre>
            </div>
          </div>
        </div>
        <br>
        <br>
        <br>
        Amazon Development Centre (London) Ltd.Registered in England and
        Wales with registration number 04543232 with its registered
        office at 1 Principal Place, Worship Street, London EC2A 2FA,
        United Kingdom.<br>
        <br>
        <br>
      </blockquote>
    </blockquote>
    <br>
  </body>
</html>