The report server
has a watchdog.
Every minute
it looks for report threads
that have run too long,
and kills them.
if (elapsed > limit) {
worker.stop();
}
Some reports
hang on a bad query.
Some loop forever
on bad data.
The watchdog
has kept the server alive
since 2014.
Then the upgrade.
Java 17
to Java 21.
Tests green.
The first stuck report
did not die.
The watchdog
threw instead:
java.lang.UnsupportedOperationException
The exception
was logged
inside the watchdog's own loop,
once a minute,
and the stuck thread
kept its connection
and its memory.
By the afternoon
there were thirty of them.
Here is what changed.
Thread.stop
was deprecated
in Java 1.2,
in 1998.
It was never safe.
It throws ThreadDeath
wherever the thread happens to be,
and releases every lock
it holds,
mid-update.
Java 18 marked it
for removal.
Java 20
turned it into a method
that only throws.
It still compiles,
with a warning
nobody reads.
It still links.
It just
does nothing useful,
loudly.
The tests passed
because nothing in them
ever got stuck.
Here is what to do.
Find every call first.
The JDK ships a tool for it:
jdeprscan --for-removal --release 21 app.jar
Then replace force
with interruption.
future.cancel(true)
interrupts the thread.
Blocking calls
throw InterruptedException.
Long loops
must check
Thread.currentThread().isInterrupted()
and leave.
Give the query
its own timeout
on the statement,
so the database ends it
instead of you.
For code you cannot change,
a library that loops
and never checks,
run it in a separate process
with a deadline,
and destroy the process.
Processes can be killed.
The operating system
takes back
the memory,
the connections
and the locks.
Threads, in Java,
no longer can be.
A method that only throws
looks like a feature
right up until
the day you need it.
Search for the ones
your emergencies depend on
before you upgrade.
– Serguey Asael Shinder