8000 Warn when running in a container without being PID 1 by EspenRoth · Pull Request #48479 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Warn when running in a container without being PID 1 - #48479

Merged
shawkins merged 3 commits into
keycloak:mainfrom
EspenRoth:warn-non-pid1-container
May 27, 2026
Merged

shawkins merged 3 commits into
keycloak:mainfrom
EspenRoth:warn-non-pid1-container

Conversation

@EspenRoth
@EspenRoth EspenRoth commented Apr 25, 2026 •
Copy link
Copy Markdown
Contributor

Summary

When KC_RUN_IN_CONTAINER=true but the Keycloak process is not PID 1, graceful shutdown may fail silently because signals such as SIGTERM are not forwarded correctly. This can lead to cache inconsistencies or data loss.

This change adds a startup warning in kc.sh to alert users to use exec in their entrypoint scripts, with a link to the existing container documentation that already describes the correct approach. The warning is only emitted for server commands (start, start-dev) and is skipped for non-server commands such as import, export, and bootstrap-admin.

Changes

  • quarkus/dist/src/main/content/bin/kc.sh — added PID 1 check with warning message printed at startup when running in a container without being PID 1; command detection refined to identify the first non-flag argument so that flags like -v or -cf preceding the subcommand are handled correctly
  • quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/JavaOptsScriptTest.java — added three tests covering the presence and absence of the warning

Test plan

  • testContainerNonPid1Warning — asserts warning appears when KC_RUN_IN_CONTAINER=true and command is start
  • testNoContainerNonPid1Warning — asserts warning is absent when env var is not set
  • testContainerNonPid1WarningAbsentForNonServerCommands — asserts warning is absent for non-server commands (e.g. export)
  • All existing JavaOptsScriptTest tests pass
  • spotless:check passes

Notes

Claude Code was used to assist in writing the code, tests, and PR description for this contribution. All changes have been reviewed and are fully understood by the author.

Closes #48059

Comment thread quarkus/dist/src/main/content/bin/kc.sh Outdated
@@ -161,6 +161,11 @@ if [ "$PRINT_ENV" = "true" ]; then
echo "Using JAVA_RUN_OPTS: $JAVA_RUN_OPTS"
fi

# Warn if running in a container without being PID 1, as signals may not be forwarded correctly and graceful shutdown may not work
if [ "$KC_RUN_IN_CONTAINER" = "true" ] && [ "$$" != "1" ]; then
echo "WARNING: Keycloak is running inside a container, but is not PID 1. Graceful shutdown may not work. Use 'exec' in your entrypoint script to ensure signals are forwarded correctly. See https://www.keycloak.org/server/containers for more details."
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.

The only additional consideration here is that this warning shouldn't matter for non-server mode commands (import, export, boostrap-admin). Is it worth accounting for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for taking a look! Yeah I did not consider that. Will modify to exclude those scenarios

Comment thread quarkus/dist/src/main/content/bin/kc.sh Outdated
@@ -68,6 +68,7 @@ do
case "$1" in
-D*) SERVER_OPTS="$SERVER_OPTS ${OPT}";;
*) CONFIG_ARGS="$CONFIG_ARGS ${OPT}"
[ -z "$KC_CMD" ] && KC_CMD="$1"
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.

Unfortunately this assumption won't generally hold. You are allowed to put arguments like -v or -cf ahead of command.

We could refine this to be the first argument without a leading -, or to avoid any additional arugment parsing in kc.sh we could instead set the initiating pid as an environment variable or system property and let the java logic emit the warning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@shawkins I think I've addressed this concern if you could take another look?

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.

Let me kick off the ci run. I'll try to think of any additional corner cases.

@EspenRoth
EspenRoth force-pushed the warn-non-pid1-container branch from 110d400 to d1b6524 Compare April 27, 2026 19:34
@shawkins
shawkins requested review from Pepo48, Copilot and vmuzikar April 29, 2026 17:53
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.

Copilot wasn't able to review any files in this pull request.

@vmuzikar
vmuzikar requested a review from Copilot April 30, 2026 16:20
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.

Copilot wasn't able to review any files in this pull request.

@shawkins
Copy link
Copy Markdown
Contributor

@EspenRoth after considering that -cf file.conf start, I went ahead and converted things to be in the java logic instead. I would like to keep as much complexity as possible out of the scripts as possible. Let me know if the changes look good to you.

@Pepo48 should kc.bat be doing something similar, or are we not worried about users who would be rolling their own windows images?

@shawkins
shawkins force-pushed the warn-non-pid1-container branch from 6621b72 to 5f97b59 Compare May 4, 2026 11:07
@Pepo48
Pepo48 commented May 4, 2026
Copy link
Copy Markdown
Contributor

@shawkins short answer: no, I think no adjustments are needed as far as Windows world of things.

Unless I missed something completely, there's no official support for Windows containers. I wouldn't be even surprised if I was the only one who actually tried to use Windows containers in connection with Keycloak (back in the QE days, just for testing purposes. I've never seen any upstream issue/discussion either.

Strictly technically speaking:

  • PID 1 concept doesn't exist on Windows - Windows containers use a completely different process model.
  • Windows uses CTRL_SHUTDOWN_EVENT, not SIGTERM.
  • cmd.exe has no exec.

If someone would go further despite of that then on docker environment (windowsservercore 1803+) it's a non-existent problem - CTRL_SHUTDOWN_EVENT is broadcast to all processes in the container. So even if kc.bat spawns java as a child process, Java receives the shutdown signal directly from the OS.

On Kubernetes, the situation is slightly different from what I found - only the entrypoint process gets the signal, not children. So if someone ran kc.bat as a wrapper script in a K8s Windows pod, the JVM child process would NOT receive CTRL_SHUTDOWN_EVENT. Seemingly a known gap: kubernetes/kubernetes#124664.

@shawkins
shawkins commented May 4, 2026
Copy link
Copy Markdown
Contributor

Thanks for the input @Pepo48, it's safe to say then that we aren't worried about kc.bat.

@shawkins
shawkins self-requested a review May 4, 2026 16:00
shawkins
shawkins previously approved these changes May 4, 2026
@shawkins shawkins 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.

LGTM

Pepo48
Pepo48 previously approved these changes May 4, 2026
@Pepo48 Pepo48 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.

I noticed a typo. Otherwise LGTM

Comment thread quarkus/dist/src/main/content/bin/kc.sh Resolved
@shawkins
shawkins dismissed stale reviews from Pepo48 and themself via 6276f8e May 4, 2026 16:05
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.BruteForceTest#testNoFailureResetForPermanentLockout

Keycloak CI - Base IT (5)

org.opentest4j.AssertionFailedError: Expected error event ==> expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

@vmuzikar
vmuzikar force-pushed the warn-non-pid1-container branch from 6276f8e to c55a80b Compare May 5, 2026 10:56
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.

We should consider also adding a container specific tests that verifies the warning is not present there.

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.

Discussed this offline with @shawkins, and we agreed on not including such test as it would not bring much value and contradicts the efforts in #48613.

@shawkins
shawkins force-pushed the warn-non-pid1-container branch from c55a80b to 6aa7277 Compare May 5, 2026 12:05
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown
10BC8

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.adapter.servlet.SAMLServletAdapterTest#salesPostSigTransientTest

Keycloak CI - Adapter IT Strict Cookies

org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

@shawkins
shawkins force-pushed the warn-non-pid1-container branch from 6aa7277 to de2406f Compare May 5, 2026 21:09
@vmuzikar vmuzikar 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.

LGTM

@vmuzikar
vmuzikar enabled auto-merge (squash) May 6, 2026 14:04
@vmuzikar
vmuzikar force-pushed the warn-non-pid1-container branch from de2406f to b7d3120 Compare May 6, 2026 14:04
EspenRoth and others added 3 commits May 19, 2026 14:42
When KC_RUN_IN_CONTAINER=true but the process is not PID 1, graceful
shutdown may fail silently because signals are not forwarded correctly.
This adds a warning at startup to alert users to use exec in their
entrypoint scripts.

Closes keycloak#48059

Signed-off-by: Espen Roth <eroth1622@gmail.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Co-authored-by: Peter Zaoral <pepo48@gmail.com>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
@vmuzikar
vmuzikar force-pushed the warn-non-pid1-container branch from b7d3120 to 51dd0fa Compare May 19, 2026 12:42
auto-merge was automatically disabled May 19, 2026 12:42

Head branch was pushed to by a user without write access

@shawkins
shawkins merged commit 4f06846 into keycloak:main May 27, 2026
90 of 154 checks passed
@ahus1
ahus1 commented May 27, 2026
Copy link
Copy Markdown
Member

Thank you for everyone involved in this, and @EspenRoth for getting the ball rolling. Keycloak will be safer to use with this.

@stianst stianst mentioned this pull request Jun 1, 2026
sashyo pushed a commit to tide-foundation/keycloak-IGA that referenced this pull request Jul 20, 2026
* Warn when running in a container without being PID 1

When KC_RUN_IN_CONTAINER=true but the process is not PID 1, graceful
shutdown may fail silently because signals are not forwarded correctly.
This adds a warning at startup to alert users to use exec in their
entrypoint scripts.

Closes keycloak#48059

Signed-off-by: Espen Roth <eroth1622@gmail.com>

* moving to java logic to avoid any command detection in the script

Signed-off-by: Steve Hawkins <shawkins@redhat.com>

* Update quarkus/dist/src/main/content/bin/kc.sh

Co-authored-by: Peter Zaoral <pepo48@gmail.com>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>

---------

Signed-off-by: Espen Roth <eroth1622@gmail.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
Co-authored-by: Steve Hawkins <shawkins@redhat.com>
Co-authored-by: Peter Zaoral <pepo48@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Safeguard against wrong PID in containers

6 participants

0