GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][fn] Exit promptly when a function instance is stopped during its setup by merlimat · Pull Request #27559 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
merlimat added 2 commits November 9, 2026 20:00
@merlimat
[fix][fn] Exit promptly when a function instance is stopped during it…
4f4c73a
…s setup

ThreadRuntime.stop() stopped the instance thread only by interrupting it
and then waited up to 10 s for it. Code that runs in the setup can clear
that interrupt: Netty's HashedWheelTimer.start() swallows it when the
setup schedules the client's first timeout (the output producer's stats
recorder). The thread then subscribed and blocked in receive() until
stop() gave up. With the process runtime, this blocks the worker thread
that stops the instance for those 10 s.

JavaInstanceRunnable.requestStop() now sets a flag that the instance loop
checks before every read, and ThreadRuntime.stop() calls it before it
interrupts the thread.
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/functions-in…
0cb9419
…stance-stop-during-setup
codelipenghui
codelipenghui requested changes Nov 10, 2026
View reviewed changes

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The PR fixes a real problem and the approach is sound. A stop that lands during setup no longer leaves the instance blocked in read() until stop() gives up after 10 s. I confirmed this: with the 'requestStop()' call removed, the new ThreadRuntimeTest fails with "stop() took 10021 ms"; with it in place, it passes.

The fix also adds a second way out of the run loop, and that path skips the catch block. That block is where the interrupt is cleared "so that closing can be handled gracefully". So when stop() arrives while the thread is processing a record, which is common for a busy sync function, close() now runs with the interrupt still set. My focused test shows this at the PR head, and shows the interrupt was cleared before close() on the baseline.

On an interrupted thread, ProducerCache.close() gives up waiting at once and calls shutdownNow(). It returns before the output producers are flushed and closed. Clearing the interrupt in the finally block before close() fixes it; I tested that change and both tests pass. I recommend that one-line change plus a test for this path before merging.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...ctions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@merlimat
[fix][fn] Clear the stop's interrupt before closing the instance
8513bb4
A stop that arrives while the instance thread processes a record ends the
loop on the stop request, with the stop's interrupt still pending, so the
catch block that cleared it no longer runs and close() ran on an
interrupted thread: closing the producer cache then gave up waiting for the
output producers to be flushed and closed.

Clear the interrupt in finally instead, under a lock that requestStop()
holds while it sets the request and interrupts the thread, so the interrupt
can't arrive after it was cleared. requestStop() now does the interrupt.
codelipenghui
codelipenghui approved these changes Nov 10, 2026
View reviewed changes
@merlimat
merlimat merged commit 1ad8568 into apache:master Nov 10, 2026
44 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@merlimat
merlimat deleted the mmerli/functions-instance-stop-during-setup branch November 10, 2026 21:55
lhotari pushed a commit that referenced this pull request Nov 11, 2026
@merlimat @lhotari
[fix][fn] Exit promptly when a function instance is stopped during it…
e9d1b16
…s setup (#27559)

(cherry picked from commit 1ad8568)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 11, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][fn] Exit promptly when a function instance is stopped during it…
38d8f2e
…s setup (apache#27559)

(cherry picked from commit 1ad8568)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][fn] Exit promptly when a function instance is stopped during it…
e8d5b3d
…s setup (apache#27559)

(cherry picked from commit 1ad8568)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@merlimat @codelipenghui @lhotari
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.