Skip to content

feat: add Operator.keepAlive to keep the JVM running until the operator is stopped - #3669

Open
csviri wants to merge 1 commit into
nextfrom
feat/operator-keep-alive
Open

csviri wants to merge 1 commit into
nextfrom
feat/operator-keep-alive

Conversation

@csviri

@csviri csviri commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds Operator.keepAlive(), which starts a non-daemon thread that keeps the JVM running until the operator is stopped.

The operator normally keeps the JVM alive through the non-daemon threads of its thread pools. When nothing else does, for example when all the threads of the operator are daemon threads as is the case with virtual threads, the JVM exits as soon as the main method returns after starting the operator. This makes keeping the JVM alive an explicit, on demand choice instead of a side effect of the executors.

public static void main(String[] args) {
    Operator operator = new Operator();
    operator.register(new MyReconciler());
    operator.installShutdownHook();
    operator.start();
    operator.keepAlive();
}

Extracted from the virtual thread support PR (#3664), where the same thread used to be started by the ExecutorServiceManager.

Behavior

  • Can be called before or after start().
  • Calling it again while the thread is running has no effect.
  • stop() ends the thread, including when the operator was never started, so it works together with installShutdownHook(). keepAlive() can be called again after a stop.
  • No thread is started unless keepAlive() is called.

Documentation

New "Keeping the JVM Alive" section in operations/configuration.md.

Tests

OperatorKeepAliveTest:

  • the thread is a non-daemon thread and ends on stop()
  • a repeated call does not start another thread
  • it can be started again after a stop
  • no thread is started unless requested

…or is stopped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@openshift-ci openshift-ci Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 29, 2026
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a650c4e7-04f5-4676-836b-f3a78c084ecc

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Add Carrot credits or activate Agent usage billing to use Autofix

@csviri
csviri marked this pull request as ready for review October 30, 2026 13:31
Copilot AI balanced review requested due to automatic review settings October 30, 2026 13:32
@openshift-ci openshift-ci Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 30, 2026
@openshift-ci
openshift-ci Bot requested review from metacosm and xstefank October 30, 2026 13:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Interrupting the last non-daemon thread before shutdown completes can let the JVM exit early, abandoning controller shutdown, reconciliation draining, or client cleanup.

1 open issue
What changed in this PR

Adds opt-in JVM lifetime management through Operator.keepAlive(), separating it from executor behavior and supporting operators that use only daemon threads.

Changes:

  • Adds a non-daemon keep-alive thread with stop and restart handling.
  • Adds tests for thread lifecycle, repeated calls, and opt-in behavior.
  • Documents the API with a startup example.
File Description
operator-framework-core/​src/​test/​java/​io/​javaoperatorsdk/​operator/​OperatorKeepAliveTest.java Tests keep-alive thread behavior and lifecycle.
operator-framework-core/​src/​main/​java/​io/​javaoperatorsdk/​operator/​Operator.java Adds keepAlive() and integrates thread termination with stop().
docs/​content/​en/​docs/​documentation/​operations/​configuration.md Explains when and how to keep the JVM alive.

Review effort: Balanced

*/
@Override
public void stop() throws OperatorException {
stopKeepAlive();

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants