Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8289610: Degrade Thread.stop #10230

Closed
wants to merge 12 commits into from
Closed

Conversation

AlanBateman
Copy link
Contributor

@AlanBateman AlanBateman commented Sep 9, 2022

Degrade Thread.stop to throw UOE unconditionally, deprecate ThreadDeath for removal, and remove the remaining special handling of ThreadDeath from the JDK.

Thread.stop is inherently unsafe and has been deprecated since JDK 1.2 (1998) with a link to a supplementary page that explains the rationale. Some of the API surface has already been degraded or removed: Thread.stop(Throwable) was degraded to throw UOE in Java 8 and removed in Java 11, and ThreadGroup.stop was degraded to throw UOE in Java 19. As of Java 19, the no-arg Thread.stop continues to work as before for platform threads but throws UOE for virtual threads. The next step in the glacial pace removal is the degrading of the no-arg Thread.stop method to throw UOE for all threads.

To keep things manageable, the change proposed here leaves JVM_StopThread in place. A separate issue will remove it and do other cleanup/removal in the VM. We have another JBS issue for the updates to the JLS and JVMS where asynchronous exceptions are defined. There is also some remaining work on a test class used by 6 jshell tests - if they aren't done in time then we will temporarily exclude them.

The change here has no impact on the debugger APIs (JVM TI StopThread, JDWP ThreadReference/Stop, and JDI ThreadReference.stop). Debuggers can continue to cause threads to throw an asynchronous exception, as might be done when simulating code throwing an exception at some point in the code.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires a CSR request to be approved

Issues

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/10230/head:pull/10230
$ git checkout pull/10230

Update a local copy of the PR:
$ git checkout pull/10230
$ git pull https://git.openjdk.org/jdk pull/10230/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10230

View PR using the GUI difftool:
$ git pr show -t 10230

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10230.diff

@AlanBateman
Copy link
Contributor Author

/csr

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 9, 2022

👋 Welcome back alanb! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Sep 9, 2022
@openjdk
Copy link

openjdk bot commented Sep 9, 2022

@AlanBateman has indicated that a compatibility and specification (CSR) request is needed for this pull request.

@AlanBateman please create a CSR request for issue JDK-8289610 with the correct fix version. This pull request cannot be integrated until the CSR request is approved.

@openjdk
Copy link

openjdk bot commented Sep 9, 2022

@AlanBateman The following labels will be automatically applied to this pull request:

  • client
  • core-libs
  • kulla
  • serviceability

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added serviceability serviceability-dev@openjdk.org client client-libs-dev@openjdk.org core-libs core-libs-dev@openjdk.org kulla kulla-dev@openjdk.org labels Sep 9, 2022
Comment on lines 28 to 42
/**
* An instance of {@code ThreadDeath} is thrown in the victim thread
* when the (deprecated) {@link Thread#stop()} method is invoked.
* This error is no longer thrown.
*
* <p>An application should catch instances of this class only if it
* must clean up after being terminated asynchronously. If
* {@code ThreadDeath} is caught by a method, it is important that it
* be rethrown so that the thread actually dies.
*
* <p>The {@linkplain ThreadGroup#uncaughtException top-level error
* handler} does not print out a message if {@code ThreadDeath} is
* never caught.
*
* <p>The class {@code ThreadDeath} is specifically a subclass of
* {@code Error} rather than {@code Exception}, even though it is a
* "normal occurrence", because many applications catch all
* occurrences of {@code Exception} and then discard the exception.
* @deprecated {@link Thread#stop()} was originally specified to "stop" a
* thread by causing it to throw a {@code ThreadDeath}. It was inherently
* unsafe and deprecated in an early JDK release. The ability to "stop" a
* thread with {@code Thread.stop} has been removed and the {@code
* Thread.stop} method changed to throw an exception. Consequently,
* {@code ThreadDeath} is also deprecated and may be deprecated
* for removal in a future release.
*
* @since 1.0
*/

@Deprecated(since="20")
public class ThreadDeath extends Error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlanBateman AlanBateman marked this pull request as ready for review September 13, 2022 13:12
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 13, 2022
@mlbridge
Copy link

mlbridge bot commented Sep 13, 2022

Webrevs

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core-libs parts look fine.

* Thread.stop causes it to unlock all of the monitors that it
* has locked (as a natural consequence of the unchecked
* @throws UnsupportedOperationException
* always
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fold with previous line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fold with previous line.

Done.

Copy link
Contributor

@plummercj plummercj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serviceability changes look good

@seanjmullan
Copy link
Member

What about also removing and undocumenting RuntimePermission("stopThread") as part of this change?

@fisk
Copy link
Contributor

fisk commented Sep 13, 2022

There is a bunch of VM code for this too. Should we clean that up separately?

@AlanBateman
Copy link
Contributor Author

There is a bunch of VM code for this too. Should we clean that up separately?

Yes chatted with @dholmes-ora about this recently and he suggested separating out so there is a follow-on JBS issue created for that. Aside from JVM_StopThread, I suspect code that deals with pending exceptions before a thread start can go away as the JVM TI StopThread can't cause an async exception on an unstarted thread. I'm hoping Thread.stillborn can go away too.

@fisk
Copy link
Contributor

fisk commented Sep 13, 2022

There is a bunch of VM code for this too. Should we clean that up separately?

Yes chatted with @dholmes-ora about this recently and he suggested separating out so there is a follow-on JBS issue created for that. Aside from JVM_StopThread, I suspect code that deals with pending exceptions before a thread start can go away as the JVM TI StopThread can't cause an async exception on an unstarted thread. I'm hoping Thread.stillborn can go away too.

Sounds good.

@AlanBateman
Copy link
Contributor Author

What about also removing and undocumenting RuntimePermission("stopThread") as part of this change?

Ah yes, the table RuntimePermission can be updated as part of this (and the corresponding constant in sun/security/util/SecurityConstants).

@mlchung
Copy link
Member

mlchung commented Sep 13, 2022

The change looks good. There are other tests that reference ThreadDeath for example test/lib/jdk/test/lib/process/ProcessTools.java, test/jdk/java/util/Timer/KillThread.java , test/jdk/java/net/httpclient/reactivestreams-tck/org/reactivestreams/tck/flow/support/NonFatal.java etc. Is it expected to update them in a follow-on issue?

@AlanBateman
Copy link
Contributor Author

The change looks good. There are other tests that reference ThreadDeath for example test/lib/jdk/test/lib/process/ProcessTools.java, test/jdk/java/util/Timer/KillThread.java , test/jdk/java/net/httpclient/reactivestreams-tck/org/reactivestreams/tck/flow/support/NonFatal.java etc. Is it expected to update them in a follow-on issue?

I've been trying to keep the test cleanup separate if possible, only because the changes become unwieldy. Aside from jshell, the tests that used Thread.stop have already been changed or removed in advance of this PR. The tests that have catch blocks for ThreadDeath will work as before as there is no Thread.stop. There are one or two tests that will need special attention - the reactivestreams-tck tests that you listed is 3rd party code and we might decide to do nothing there.

@jaikiran
Copy link
Member

The changes to the core-libs look fine to me. There are some test security policy files (for example IsKeepingAlive.policy) and the DynamicPolicy which use the stopThread RuntimePermission which no longer exists. Should those policy files and artifacts be updated to remove reference to this permission too?

@@ -2666,8 +2666,6 @@ private void invokeConfigurationListeners() {
for (Runnable c : listeners.values().toArray(new Runnable[0])) {
try {
c.run();
} catch (ThreadDeath death) {
throw death;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, before this change, some (odd/weird) application code which was registered as a listener could throw a ThreadDeath and we would then abort any other listeners from being run. With this change that would no longer be that case. However, I think that is fine, since the API doc of LogManager.addConfigurationListener() already states:

* It is recommended that listeners do not throw errors or exceptions.
*
* If a listener terminates with an uncaught error or exception then
* the first exception will be propagated to the caller of
* {@link #readConfiguration()} (or {@link #readConfiguration(java.io.InputStream)})
* after all listeners have been invoked.

@AlanBateman
Copy link
Contributor Author

AlanBateman commented Sep 14, 2022

The changes to the core-libs look fine to me. There are some test security policy files (for example IsKeepingAlive.policy) and the DynamicPolicy which use the stopThread RuntimePermission which no longer exists. Should those policy files and artifacts be updated to remove reference to this permission too?

Mandy asked about test cleanup too. I'm hoping that we will do the test cleanup separately, only to avoid the changes being too unwieldly.

@jaikiran
Copy link
Member

The changes to the core-libs look fine to me. There are some test security policy files (for example IsKeepingAlive.policy) and the DynamicPolicy which use the stopThread RuntimePermission which no longer exists. Should those policy files and artifacts be updated to remove reference to this permission too?

Mandy asked the test cleanup too. I'm hoping that we will do the test cleanup separately, only to avoid the changes being too unwieldly.

Sorry, I missed your previous response to that similar question. Doing those changes separately sounds fine to me.

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

* @deprecated This method was originally specified to "stop" a thread
* by causing it to throw a {@link ThreadDeath}. It was inherently
* unsafe. Stopping a thread caused it to unlock all of the
* monitors that it had locked (as a natural consequence of the
* {@code ThreadDeath} exception propagating up the stack). If
* any of the objects previously protected by these monitors were in
* an inconsistent state, the damaged objects become visible to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/become/became/
(because you are writing in past tense now).

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Alan,

Good to see this finally become non-functional but as long as the debugger/JVMTI can trigger throwing an async ThreadDeath then I have to question the potential change in behaviour introduced by removing all the catch (ThreadDeath td) logic in various places.

Comment on lines -193 to -195
// evaluate possible precedence of flushException over closeException
if ((flushException instanceof ThreadDeath) &&
!(closeException instanceof ThreadDeath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the ThreadDeath originates from the debugger then this is now a change in behaviour.

Copy link
Contributor Author

@AlanBateman AlanBateman Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to see this finally become non-functional but as long as the debugger/JVMTI can trigger throwing an async ThreadDeath then I have to question the potential change in behaviour introduced by removing all the catch (ThreadDeath td) logic in various places.

I've gone through this a few times too and concluded it would be better to remove these untestable code paths. The debugger scenario is more like what used to be Thread.stop(Throwable). It can be used to throw any exception or error, e.g. ask the debugger to throw IllegalArgumentException or SQLException when I'm at this breakpoint so I can see how the code behaves. Yes, the user could ask the debugger to throw java.lang.ThreadDeath when prompted but it's not really interesting now because code won't get this error outside of a debugger. We could leave the special handing but we have no way to test is and we'll need to the remove the special handling when TD is removed. As a general point, the special casing of TD is a bit inconsistent and more likely to be seen in older code rather newer code. A static analysis of 24M classes found very few usages in 3rd party libraries.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Alan on this. First, use and handling of ThreadDeath is quite rare; see my analysis of the corpus search results in the bug report. Second, while this does change the behavior in the debugger case, I'm hard-pressed to see how anybody is relying on such behavior. And maybe somebody somewhere is indeed relying on this behavior, but it doesn't seem to me this behavior is guaranteed by any specifications.

A more likely way that programs could observe changes in ThreadDeath handling occurs when programs throw ThreadDeath explicitly, that is, not using Thread.stop. This does seem to occur infrequently "in the wild" (we'll fix the jshell case). The point of throwing ThreadDeath explicitly is to take advantage of special-case handling of ThreadDeath that might occur higher in the call stack. Such special-casing was at one time an accepted exception handing idiom, but it's essentially disused. I don't think the JDK needs to continue to support this old idiom.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I will defer to your views here. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I will defer to your views here. Thanks.

Thank you, I'll get the CSR moving again and we'll try to get this done.

if (t instanceof ThreadDeath td) {
throw td;
}
// ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again change of behaviour if TD originates from debugger.

Comment on lines +29 to +30
* An instance of {@code ThreadDeath} was originally specified to be thrown
* by a victim thread when "stopped" with {@link Thread#stop()}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have always mentioned the possibility of TD coming from a debugger as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have always mentioned the possibility of TD coming from a debugger as well?

The debugger can cause any error or exception to be thrown so I don't think we should put anything in the TD spec about this.

Comment on lines +699 to +700
} else {
System.err.print("Exception in thread \"" + t.getName() + "\" ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again change in behaviour.

Comment on lines +36 to +37
<p>Because it was inherently unsafe. Stopping a thread caused it to
unlock all the monitors that it had locked. (The monitors were
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an aside but this rationale was always a significant under-statement as async-exceptions are inherently unsafe even if no concurrency or monitors are involved.

@@ -167,11 +167,6 @@ public final void run() {
break;
}
}
} catch (ThreadDeath td) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again change in behaviour

@@ -202,10 +202,6 @@ void pumpOneEventForFilters(int id) {

eq.dispatchEvent(event);
}
catch (ThreadDeath death) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again change in behaviour.

* exceptions does not print out a message or otherwise notify the
* application if the uncaught exception is an instance of
* {@code ThreadDeath}.
* Throws {@code UnsupportedOperationException}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using an implSpec tag here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using an implSpec tag here.

implSpec is usually for default methods or overrideable methods. In this case, Thread.stop is final so code that extends Thread can't override and change the behavior. So it's not clear to me that implSpec would be useful as it would duplicate the description in the generated javadoc.

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Sep 22, 2022
@openjdk
Copy link

openjdk bot commented Sep 22, 2022

@AlanBateman This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8289610: Degrade Thread.stop

Reviewed-by: rriggs, cjplummer, jpai, mchung, prr, mullan

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 65 new commits pushed to the master branch:

  • d5bee4a: 8294086: RISC-V: Cleanup InstructionMark usages in the backend
  • 47f233a: 8292202: modules_do is called without Module_lock
  • 742bc04: 8294100: RISC-V: Move rt_call and xxx_move from SharedRuntime to MacroAssembler
  • 2283c32: 8294149: JMH 1.34 and later requires jopt-simple 5.0.4
  • 9f90eb0: 8294062: Improve parsing performance of j.l.c.MethodTypeDesc
  • c6be2cd: 8293156: Dcmd VM.classloaders fails to print the full hierarchy
  • 711e252: 8294039: Remove "Classpath" exception from java/awt tests
  • 27b8e2f: 8294038: Remove "Classpath" exception from javax/swing tests
  • e195897: 8294068: Unconditional and eager load of nio library since JDK-8264744
  • 84d7ff6: 8288129: Shenandoah: Skynet test crashed with iu + aggressive
  • ... and 55 more: https://git.openjdk.org/jdk/compare/01e7b8819918906082e315870e667b15910cee99...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 22, 2022
@AlanBateman
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Sep 23, 2022

Going to push as commit acd5bcf.
Since your change was applied there have been 85 commits pushed to the master branch:

  • 05c8cab: 8293532: Use lighter jmod compression levels in build config
  • eec992c: 8292602: ZGC: C2 late barrier analysis uses invalid dominator information
  • f6d78cd: 8293657: sun/management/jmxremote/bootstrap/RmiBootstrapTest.java#id1 failed with "SSLHandshakeException: Remote host terminated the handshake"
  • a4dc035: 8290910: Wrong memory state is picked in SuperWord::co_locate_pack()
  • f3ba332: 8294183: AArch64: Wrong macro check in SharedRuntime::generate_deopt_blob
  • df53fa7: 8292328: AccessibleActionsTest.java test instruction for show popup on JLabel did not specify shift key
  • 5285035: 8294075: gtest/AsyncLogGtest crashes with SEGV
  • 696287d: 8294037: Using alias template to unify hashtables in AsyncLogWriter
  • 48cc156: 8293331: Refactor FileDispatcherImpl into operating system-specific components
  • f751e60: 8294197: Zero: JVM_handle_linux_signal should not assume deopt NOPs
  • ... and 75 more: https://git.openjdk.org/jdk/compare/01e7b8819918906082e315870e667b15910cee99...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 23, 2022
@openjdk openjdk bot closed this Sep 23, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 23, 2022
@openjdk
Copy link

openjdk bot commented Sep 23, 2022

@AlanBateman Pushed as commit acd5bcf.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client client-libs-dev@openjdk.org core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated kulla kulla-dev@openjdk.org serviceability serviceability-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.