8000 User session limit generates fatal error · Issue #48040 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

User session limit generates fatal error #48040

Description

@martinkrcka

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

No response

Describe the bug

UserSessionLimitsAuthenticator implicitly assumes that whenever the realm session limit is exceeded, all client session limits are also exceeded.

Introduced by #38016

Version

26.5.6

Regression

  • The issue is a regression

Expected behavior

UserSessionLimitsAuthenticator should correctly handle cases where a user exceeds the realm session limit without exceeding all client session limits, and must do so without causing a runtime error.

As a possible solution, when the realm limit is exceeded but some client limits are not, the system could purge the oldest user sessions across all clients, rather than restricting the cleanup to sessions of the current client.

Actual behavior

When a user exceeds the realm session limit but has not exceeded the session limit for a specific client, attempting to log in to that client results in a runtime error.

How to Reproduce?

Steps to reproduce:

  1. Create a realm with two clients, A and B.
  2. Configure an authentication flow with identical session limits applied at both the realm and client levels.
  3. Log in repeatedly to client A until both the realm session limit and client A’s session limit are exceeded for a specific user.
  4. Attempt to log in to client B using the same user.

Actual result:
The login attempt fails with an internal server error.

Anything else?

Relevant code (UserSessionLimitsAuthenticator.java)

            if (newUserSession && exceedsLimit(userSessionCountForRealm, userRealmLimit)) {
                logger.infof("Too many session in this realm for the current user. Session count: %s", userSessionCountForRealm);
                String eventDetails = String.format("Realm session limit exceeded. Realm: %s, Realm limit: %s. Session count: %s, User id: %s",
                        context.getRealm().getName(), userRealmLimit, userSessionCountForRealm, context.getUser().getId());
                var removedClientSessions = handleLimitExceeded(context, userSessionsForClient, eventDetails, userClientLimit);

When the realm limit is exceeded, the implementation attempts to purge sessions based on the current client’s limit first.

    private long getNumberOfSessionsThatNeedToBeLoggedOut(long count, long limit) {
        return count - (limit - 1);
    }

This method is used to determine how many sessions should be removed for the current client. However, it returns a negative value when the client limit has not been exceeded.

       List<UserSessionModel> userSessionsToBeRemoved = userSessions
            .stream()
            .sorted(Comparator.comparingInt(UserSessionModel::getLastSessionRefresh))
            .limit(numberOfSessionsThatNeedToBeLoggedOut)
            .toList();

A negative number as argument to limit() produces an runtime exception which leads to the internal server error.

Activity

  1. added theissue type on Apr 14, 2026
  2. changed the title [-]User session limit generates fatal error . Introduced by #38016[/-] [+]User session limit generates fatal error[/+] on Apr 14, 2026
  3. added
    area/authenticationAuth flows, authenticators, required actions, OTP, password policies, brute force
    on Apr 14, 2026
  4. keycloak-github-bot commented on Apr 14, 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. 12 remaining items

  6. added a commit that references this issue on Apr 15, 2026
    365d620
  7. linked a pull request that will close this issue[26.6] Realm session limit is exceeded #48105on Apr 15, 2026
  8. added a commit that references this issue on Apr 15, 2026
    c66069c
  9. added a commit that references this issue on Jul 20, 2026
    0421e7a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Fields

No fields configured for bug.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0