Skip to content

Conversation

@nickita-khylkouski
Copy link

Summary

This PR improves TestThread.tearDown() to try interrupt() before falling back to Thread.stop(), as suggested by @cpovirk in #7319.

Changes:

  • Call interrupt() first, allowing interruptible threads (like those in InterruptibleMonitorTest) to clean up gracefully
  • Wait DUE_DILIGENCE_MILLIS (100ms) for the thread to respond to the interrupt
  • Only fall back to reflective stop() if the thread is still alive after the interrupt attempt

Motivation

The existing implementation immediately tries Thread.stop() via reflection, which:

  1. Is deprecated and throws UnsupportedOperationException on Java 20+
  2. Doesn't give interruptible threads a chance to clean up gracefully

Many test threads (particularly in InterruptibleMonitorTest) are designed to respond to interrupts. By trying interrupt() first, we allow these threads to terminate cleanly without relying on the deprecated stop() method.

Behavior by Java Version

Java Version interrupt() stop() fallback Thread cleanup
8-19 ✅ Tried first ✅ Works if needed Always works
20-25 ✅ Tried first ❌ Throws UnsupportedOperationException Works for interruptible threads
26+ ✅ Tried first ❌ Method removed Works for interruptible threads

Testing

Verified locally that existing tests pass:

  • InterruptibleMonitorTest - 16 tests ✅
  • UninterruptibleMonitorTest - 16 tests ✅
  • UninterruptiblesTest - 49 tests ✅

Related Issues

Fixes #7319

This allows interruptible threads (like those in InterruptibleMonitorTest)
to clean up gracefully instead of relying solely on the deprecated
Thread.stop() method.

The implementation:
1. Calls interrupt() first
2. Waits DUE_DILIGENCE_MILLIS for the thread to respond
3. Falls back to reflective stop() only if the thread is still alive

This addresses Issue google#7319 as suggested by cpovirk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 no SLO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lingering threads in multiple tests

2 participants