Problem
ThreadRuntime.stop() stops a function instance only by interrupting its thread, then waits up to 10 s for the thread to exit (THREAD_SHUTDOWN_TIMEOUT_MILLIS). If the stop arrives while JavaInstanceRunnable.setup() is running, code the setup runs can clear that interrupt. Then the setup finishes, subscribes to the input, and the thread blocks in consumer.receive() until stop() gives up after 10 s and closes the runnable.
One place that clears it is Netty's HashedWheelTimer.start(). The first newTimeout() on a timer waits for the worker thread to start, and it swallows the interrupt while it waits:
// io.netty.util.HashedWheelTimer.start() (4.2.19)
while (startTime == 0) {
try {
startTimeInitialized.await();
} catch (InterruptedException ignore) {
// Ignore - it will be ready very soon.
}
}
The setup can be the first code to use the client's timer. Creating the output producer starts its stats recorder (ProducerStatsRecorderImpl.init() → pulsarClient.timer().newTimeout(...)). When the producer is created on the instance thread, that call clears the interrupt inside createAsync(), before create() waits on the future. The user's function, connectors and the state store all run on that thread too, and any of them can do the same.
With the process runtime, the worker's ProcessRuntime.stop() sends SIGTERM and waits for the instance JVM to exit. The JVM's shutdown hook calls ThreadRuntime.stop(), so the worker thread that stops the instance (the assignment tailer or the scheduler) is blocked for those 10 s. That delays every other function's start or stop on that worker.
Example
A local run of PulsarFunctionsJavaProcessTest updated a function's code and deleted it right after. The new instance JVM (test-exclamation-fn-vchflziz-0) got the SIGTERM during its setup:
02:58:53.467 worker sends SIGTERM (instance thread is in PulsarSink.open, looking up the output schema)
02:58:53.598 [instance thread] ProducerStatsRecorderImpl - Pulsar client config ... <- first timer use, interrupt cleared
02:58:53.675 [pulsar-client-io] ConsumerImpl - Subscribed to topic ...
02:59:03.474 [Thread-0] ThreadRuntime - The function instance thread is still alive after timeout.
Giving up waiting and moving forward to close function {timeoutMs=10000}
02:59:03.479 [pulsar-client-io] ConsumerImpl - Closed consumer ...
In that run, the worker scheduled the next function late, and the test's 10 s await in getFunctionStatsEmpty failed ("null within 10 seconds").
Change
-
ThreadRuntime.stop() now calls JavaInstanceRunnable.requestStop(fnThread). That method sets a volatile flag and interrupts the thread. The instance loop checks the flag before every read, so the thread exits even if the interrupt was cleared:
public void requestStop(Thread instanceThread) {
synchronized (stopLock) {
stopRequested = true;
instanceThread.interrupt();
}
}
...
while (!stopRequested) {
currentRecord = readInput();
...
-
The loop can now also end on the flag with the stop's interrupt still pending. This happens when a stop arrives while the function is processing a record. So the instance clears the interrupt in finally, on every exit path, before it closes. It used to do this only in the catch block. On an interrupted thread, ProducerCache.close() stops waiting for the output producers to flush and close. The clear takes the same lock as requestStop(), so the interrupt can't arrive after it:
} finally {
synchronized (stopLock) {
Thread.interrupted();
}
log.info("Closing instance");
close();
}
The interrupt still wakes a thread that is blocked in a read or in the setup. The flag covers the case where something cleared the interrupt before the next read. A stop that arrives during the setup still lets the setup finish, which can take a few hundred ms and create the input subscription. Then the instance closes, as it does today when the interrupt makes receive() fail.
Testing
- New
ThreadRuntimeTest.testStopDuringSetupThatClearsTheInterrupt. Its source's open() waits for the stop's interrupt and clears it, like HashedWheelTimer.start(), and the test then checks that ThreadRuntime.stop() returns within 5 s and the thread has exited. Without the fix, every attempt logs "still alive after timeout" and stop() takes about 10 s. With the fix, stop() returns in a few ms.
- New
ThreadRuntimeTest.testStopDuringProcessingClosesWithoutTheInterrupt. A sync function is busy and leaves the interrupt set when stop() arrives, and the test checks that the instance thread closes the sink without the interrupt. Without the finally change, it fails with "the instance thread closed the sink while interrupted".
JavaInstanceRunnableTest and the other org.apache.pulsar.functions.runtime.thread tests pass.
- A standalone check against netty-common 4.2.19: after
Thread.currentThread().interrupt(), the first newTimeout() on a new HashedWheelTimer returns with the thread no longer interrupted. A second call keeps the interrupt.
Found while fixing #27551, which is independent of this change.