8000 when the realm session limit is exceeded and behavior is set to Termi… by martinkrcka · Pull Request #48064 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

when the realm session limit is exceeded and behavior is set to Termi… - #48064

Merged
ahus1 merged 2 commits into
keycloak:mainfrom
martinkrcka:main
Apr 15, 2026
Merged

ahus1 merged 2 commits into
keycloak:mainfrom
martinkrcka:main

Conversation

@martinkrcka
@martinkrcka martinkrcka commented Apr 15, 2026 •
Copy link
Copy Markdown
Contributor

when the realm session limit is exceeded and behavior is set to Terminate oldest session, the authenticator calls
handleLimitExceeded(userSessionsForClient, userClientLimit) first.

if the user has fewer existing sessions for the current client than userClientLimit - 1, getNumberOfSessionsThatNeedToBeLoggedOut() returns a negative value and Stream.limit(negative) throws IllegalArgumentException, producing a 500 on every login to that client.

This change ensures that getNumberOfSessionsThatNeedToBeLoggedOut returns 0 if the calculated value is negative. The dependent execution steps remain unchanged.
A result of 0 sessions to be purged from the current client triggers the removal of the oldest sessions across all clients.

also added a regression test in UserSessionLimitsTest that reproduces the scenario: realm limit=2, client limit=3, two logins on client-a to saturate the realm, then login on client-b (0 existing client sessions).

Fixes #48040

@ahus1
ahus1 commented Apr 15, 2026
Copy link
Copy Markdown
Member

@martinkrcka - please add a sign-off to your commit as outlined in the CONTRIBUTING.md file.

@martinkrcka
Copy link
Copy Markdown
Contributor Author

@martinkrcka - please add a sign-off to your commit as outlined in the CONTRIBUTING.md file.

Done :-)

@mabartos
Copy link
Copy Markdown
Member

@martinkrcka Still not done :/ The commit needs to contain the "Signed-off-by: xxx"... you can add the -s flag when creating the commit.

@martinkrcka
Copy link
Copy Markdown
Contributor Author

@martinkrcka Still not done :/ The commit needs to contain the "Signed-off-by: xxx"... you can add the -s flag when creating the commit.

Finally it should be done!

@mabartos
Copy link
Copy Markdown
Member

@martinkrcka Thanks! As this PR is clearly built on top of @mateenali66's PR, you should add this note to the commit description:

Co-authored-by: Mateen Anjum <mateenali66@gmail.com>

As I think this approach with Math.max() seems to be more defensive, so +1 to proceed with this one.

@mabartos
Copy link
Copy Markdown
Member

@martinkrcka It'd be also good to return-fast, when the count is 0, so similar to what @mateenali66 did here:

if (numberOfSessionsThatNeedToBeLoggedOut == 0) {
    logger.debug("No additional sessions that need to be logged out");
    return Collections.emptyList();
}

…nate oldest session, the authenticator calls

handleLimitExceeded(userSessionsForClient, userClientLimit) first.

if the user has fewer existing sessions for the current client than userClientLimit - 1, getNumberOfSessionsThatNeedToBeLoggedOut() returns a negative value and Stream.limit(negative) throws
IllegalArgumentException, producing a 500 on every login to that client.

This change ensures that getNumberOfSessionsThatNeedToBeLoggedOut returns 0 if the calculated value is negative.
The dependent execution steps remain unchanged.
A result of 0 sessions to be purged from the current client triggers the removal of the oldest sessions across all clients.

also added a regression test in UserSessionLimitsTest that reproduces the scenario: realm limit=2, client limit=3, two logins on client-a to saturate the realm, then login on client-b (0 existing client sessions).

Fixes keycloak#48040

Co-authored-by: Mateen Anjum <mateenali66@gmail.com>
Signed-off-by: Martin Krcka <martin.krcka@adesso.de>
Signed-off-by: Martin Bartoš <mabartos@redhat.com>
@mabartos mabartos left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@martinkrcka I've added the last missing piece.

@martinkrcka @mateenali66 Thanks for the contribution!

@keycloak/core-authn Anyone want to review it as well?

@rmartinc rmartinc 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.

Thanks @martinkrcka and @mabartos! LGTM too!

@ahus1 ahus1 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving based on previous reviews.

@ahus1
ahus1 merged commit 953cd26 into keycloak:main Apr 15, 2026
84 checks passed
mabartos added a commit to mabartos/keycloak that referenced this pull request Apr 15, 2026
keycloak#48064)

* when the realm session limit is exceeded and behavior is set to Terminate oldest session, the authenticator calls
handleLimitExceeded(userSessionsForClient, userClientLimit) first.

if the user has fewer existing sessions for the current client than userClientLimit - 1, getNumberOfSessionsThatNeedToBeLoggedOut() returns a negative value and Stream.limit(negative) throws
IllegalArgumentException, producing a 500 on every login to that client.

This change ensures that getNumberOfSessionsThatNeedToBeLoggedOut returns 0 if the calculated value is negative.
The dependent execution steps remain unchanged.
A result of 0 sessions to be purged from the current client triggers the removal of the oldest sessions across all clients.

also added a regression test in UserSessionLimitsTest that reproduces the scenario: realm limit=2, client limit=3, two logins on client-a to saturate the realm, then login on client-b (0 existing client sessions).

Fixes keycloak#48040

---------

Signed-off-by: Martin Krcka <martin.krcka@adesso.de>
Signed-off-by: Martin Bartoš <mabartos@redhat.com>
Co-authored-by: Mateen Anjum <mateenali66@gmail.com>
Co-authored-by: Martin Bartoš <mabartos@redhat.com>
(cherry picked from commit 953cd26)
@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
keycloak#48064)

* when the realm session limit is exceeded and behavior is set to Terminate oldest session, the authenticator calls
handleLimitExceeded(userSessionsForClient, userClientLimit) first.

if the user has fewer existing sessions for the current client than userClientLimit - 1, getNumberOfSessionsThatNeedToBeLoggedOut() returns a negative value and Stream.limit(negative) throws
IllegalArgumentException, producing a 500 on every login to that client.

This change ensures that getNumberOfSessionsThatNeedToBeLoggedOut returns 0 if the calculated value is negative.
The dependent execution steps remain unchanged.
A result of 0 sessions to be purged from the current client triggers the removal of the oldest sessions across all clients.

also added a regression test in UserSessionLimitsTest that reproduces the scenario: realm limit=2, client limit=3, two logins on client-a to saturate the realm, then login on client-b (0 existing client sessions).

Fixes keycloak#48040

---------

Signed-off-by: Martin Krcka <martin.krcka@adesso.de>
Signed-off-by: Martin Bartoš <mabartos@redhat.com>
Co-authored-by: Mateen Anjum <mateenali66@gmail.com>
Co-authored-by: Martin Bartoš <mabartos@redhat.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.

User session limit generates fatal error

4 participants

0