Skip to content

Upgrade to 26.7.0 fails with preview features as the stateless cluster provider captures a null NodeInfo before postInit #51154

Closed

Description

@slaskawi

Before reporting an issue

  • I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.

Area

infinispan

Describe the bug

Upgrading an existing Keycloak database from 26.6.4 to 26.7.0 fails during startup when the stateless Preview feature is enabled.

During the 26.7.0 database and realm model migration, Keycloak creates a DatabaseAwareClusterProvider before DatabaseAwareClusterProviderFactory.postInit() has run. DatabaseAwareClusterProviderFactory.create() passes the factory fields nodeInfo and protoStreamMarshaller into the provider constructor. Both fields are still null at that point because they are assigned only from postInit().

The provider stores these values in final instance fields. Consequently, initialization of the Infinispan connection later does not repair the already created provider. When realm migration commits its cache invalidations, DatabaseAwareClusterProvider.persistToDatabase() dereferences the captured null value: nodeInfo.clusterName()

Startup then fails with:

  Cannot invoke "org.keycloak.connections.infinispan.NodeInfo.clusterName()" because "this.nodeInfo" is null

Version

26.7.0

Regression

  • The issue is a regression

Expected behavior

Keycloak should complete the database and realm model migration from 26.6.4 to 26.7.0 when Preview features are enabled.

Actual behavior

Keycloak initializes the underlying embedded Infinispan provider successfully. It then fails while committing cache invalidations generated by the 26.7.0 realm migration.

The following sequence is reconstructed from the independent local reproduction. Generated identifiers are represented by placeholders:

INFO  [org.keycloak.common.Profile]
Preview features enabled: [..., stateless:v1, ...]

INFO  [org.keycloak.quarkus.runtime.storage.database.liquibase.QuarkusJpaUpdaterProvider]
Updating database. Using changelog META-INF/jpa-changelog-master.xml

INFO  [org.keycloak.storage.datastore.DefaultMigrationManager]
Migrating older model to 26.7.0

INFO  [org.infinispan.CONTAINER]
ISPN000556: Starting user marshaller 'org.infinispan.commons.marshall.ImmutableProtoStreamMarshaller'

INFO  [org.keycloak.connections.infinispan.DefaultInfinispanConnectionProviderFactory]
Node name: <generated-node-name>, Site name: null, Cluster name: ISPN

INFO  [org.keycloak.migration.migrators.RealmMigration]
migrated realm <realm-name> to 26.7.0

ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler]
ERROR: Failed to start server

ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler]
ERROR: Cannot invoke "org.keycloak.connections.infinispan.NodeInfo.clusterName()" because "this.nodeInfo" is null

The successful Node name message does not contradict the exception.

It proves that the session's InfinispanConnectionProvider has initialized its NodeInfo. The null value is the separate copy captured earlier by DatabaseAwareClusterProvider from the uninitialized fields of DatabaseAwareClusterProviderFactory.

The relevant stack from a local reproduction using --verbose is:

java.lang.NullPointerException: Cannot invoke "org.keycloak.connections.infinispan.NodeInfo.clusterName()" because "this.nodeInfo" is null
    at org.keycloak.cluster.infinispan.DatabaseAwareClusterProvider.persistToDatabase(DatabaseAwareClusterProvider.java:130)
    at org.keycloak.cluster.infinispan.DatabaseAwareClusterProvider.notify(DatabaseAwareClusterProvider.java:126)
    at org.keycloak.models.cache.infinispan.CacheManager.sendInvalidationEvents(CacheManager.java:220)
    at org.keycloak.models.cache.infinispan.RealmCacheSession.runInvalidations(RealmCacheSession.java:357)
    at org.keycloak.models.cache.infinispan.RealmCacheSession$2.commit(RealmCacheSession.java:415)
    ...
    at org.keycloak.quarkus.runtime.storage.database.jpa.QuarkusJpaConnectionProviderFactory.postInit(QuarkusJpaConnectionProviderFactory.java:144)
    at org.keycloak.services.DefaultKeycloakSessionFactory.initializeProviders(DefaultKeycloakSessionFactory.java:140)

Although Quarkus previously logs that Keycloak is listening, initialization is not complete. The Keycloak Initialized health check remains DOWN, and Keycloak subsequently shuts down.

How to Reproduce?

The following reproduces the failure using only a persistent H2 database and one Keycloak node.

1. Initialize a persistent database using Keycloak 26.6.4

Use the official Keycloak 26.6.4 distribution:

export KC_DB_URL='jdbc:h2:file:/tmp/keycloak-preview-reproducer/keycloakdb;NON_KEYWORDS=VALUE'

KC_BOOTSTRAP_ADMIN_USERNAME='reproducer-admin' \
KC_BOOTSTRAP_ADMIN_PASSWORD='reproducer-only-password' \
./keycloak-26.6.4/bin/kc.sh start-dev \
    --db=dev-file \
    --db-url="$KC_DB_URL"

Wait until the 26.6.4 server has completed initialization, then stop it cleanly.

The master realm alone is sufficient. Creating an additional test realm is optional.

2. Start Keycloak 26.7.0 against the same database

./keycloak-26.7.0/bin/kc.sh start-dev \
    --db=dev-file \
    --db-url="$KC_DB_URL" \
    --features=preview \
    --verbose

3. Observe the migration and failure

The log includes:

Preview features enabled: ... stateless:v1 ...
Updating database. Using changelog META-INF/jpa-changelog-master.xml
Migrating older model to 26.7.0
ISPN000556: Starting user marshaller 'org.infinispan.commons.marshall.ImmutableProtoStreamMarshaller'
Node name: <generated-node-name>, Site name: null, Cluster name: ISPN
migrated realm <realm-name> to 26.7.0
ERROR: Failed to start server
ERROR: Cannot invoke "org.keycloak.connections.infinispan.NodeInfo.clusterName()" because "this.nodeInfo" is null

4. Verify the triggering feature

Start 26.7.0 again with stateless excluded:

./keycloak-26.7.0/bin/kc.sh start-dev \
    --db=dev-file \
    --db-url="$KC_DB_URL" \
    --features=preview \
    --features-disabled=stateless

Keycloak completes initialization.

This confirms that stateless is the triggering feature.

Anything else?

Suggested fix

DatabaseAwareClusterProviderFactory.create() should obtain NodeInfo and the marshaller directly from the current session's InfinispanConnectionProvider.

It should not copy lifecycle dependent state from factory fields populated by postInit().

For example:

@Override
public ClusterProvider create(KeycloakSession session) {
    var delegate = super.create(session);
    var ispnConnections = session.getProvider(InfinispanConnectionProvider.class);

    return new DatabaseAwareClusterProvider(
            delegate,
            session,
            ispnConnections.getNodeInfo(),
            ispnConnections.getMarshaller(),
            awaitTimeout);
}

postInit() can use local values for configuring the poller:

@Override
public void postInit(KeycloakSessionFactory factory) {
    KeycloakModelUtils.runJobInTransaction(factory, session -> {
        var ispnConnections = session.getProvider(InfinispanConnectionProvider.class);
        var nodeInfo = ispnConnections.getNodeInfo();
        var protoStreamMarshaller = ispnConnections.getMarshaller();

        var pollerTask = new DatabaseClusterEventPollerTask(
                nodeInfo.clusterName(),
                protoStreamMarshaller);
        var runner = new ScheduledTaskRunner(factory, pollerTask);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runner.run();
            }
        }, pollInterval.toMillis(), pollInterval.toMillis());

        logger.infof(
                "Scheduled cluster event poller with interval %s for cluster '%s'",
                pollInterval,
                nodeInfo.clusterName());
    });
}

The factory fields can then be removed:

private volatile NodeInfo nodeInfo;
private volatile Marshaller protoStreamMarshaller;

Both values must be fixed together. Correcting only nodeInfo would leave the provider with a null marshaller and move the failure to:

marshaller.objectToByteBuffer(wrappedEvent)

A null guard in persistToDatabase() would not be an appropriate fix because it would silently discard cross cluster invalidation events. Adding provider ordering alone would also be fragile because Keycloak permits create() calls from another factory's postInit().

Optionally, the DatabaseAwareClusterProvider constructor could use Objects.requireNonNull() for both values so that any future lifecycle regression fails immediately at provider creation rather than during cache invalidation.

Suggested regression test

The regression test must exercise an upgrade. A fresh 26.7.0 startup with Preview enabled does not reproduce the provider ordering that caused this failure.

Recommended test sequence:

  1. Initialize a persistent database using Keycloak 26.6.x.
  2. Ensure the database contains the master realm and preferably one additional realm.
  3. Stop the old server cleanly.
  4. Start the candidate version against that database with embedded caches and --features=stateless.
  5. Assert that the log contains Migrating older model to 26.7.0.
  6. Assert that realm migration completes.
  7. Assert that the log contains Scheduled cluster event poller.
  8. Assert that server initialization completes.
  9. Assert that no null NodeInfo or null marshaller exception occurs.
  10. Update a realm after startup and verify that cache invalidation succeeds, covering both NodeInfo and marshaller use.

The existing fresh startup Preview coverage in FeaturesDistTest.java does not cover an upgrade from an older persistent database.

Activity

  1. added
    kind/bug
    on Jul 24, 2026
  2. added theissue type on Jul 24, 2026
  3. added
    area/infinispan
    on Jul 24, 2026
  4. keycloak-github-bot commented on Jul 25, 2026

    @keycloak-github-bot

    Due to the amount of issues reported by the community we are not able to prioritise resolving this issue at the moment.

    If you are affected by this issue, upvote it by adding a 👍 to the description. We would also welcome a contribution to fix the issue.

  5. 5 remaining items

  6. added a commit that references this issue on Aug 2, 2026
    f0e9bef
  7. added a commit that references this issue on Aug 6, 2026
    3de2f91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions