Repository navigation
Description
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 \
--verbose3. 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=statelessKeycloak 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:
- Initialize a persistent database using Keycloak 26.6.x.
- Ensure the database contains the master realm and preferably one additional realm.
- Stop the old server cleanly.
- Start the candidate version against that database with embedded caches and
--features=stateless. - Assert that the log contains
Migrating older model to 26.7.0. - Assert that realm migration completes.
- Assert that the log contains
Scheduled cluster event poller. - Assert that server initialization completes.
- Assert that no null
NodeInfoor null marshaller exception occurs. - Update a realm after startup and verify that cache invalidation succeeds, covering both
NodeInfoand marshaller use.
The existing fresh startup Preview coverage in FeaturesDistTest.java does not cover an upgrade from an older persistent database.
Activity
keycloak-github-bot commentedon Jul 25, 2026
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 remaining items
Fix stateless cluster and db migrations race
Fix stateless cluster and db migrations race