Skip to content

Fix concurrent group access to prevent NullPointerException#10

Open
everettbu wants to merge 1 commit into
feature-group-concurrency-updatefrom
feature-group-concurrency-implementation
Open

Fix concurrent group access to prevent NullPointerException#10
everettbu wants to merge 1 commit into
feature-group-concurrency-updatefrom
feature-group-concurrency-implementation

Conversation

@everettbu

@everettbu everettbu commented Jul 26, 2025

Copy link
Copy Markdown

Test 10

Summary by CodeRabbit

  • Tests

    • Added a new test to verify stability when reading and deleting multiple groups concurrently.
  • Bug Fixes

    • Improved null safety when retrieving subgroup counts for groups.
  • Refactor

    • Added an annotation to clarify method overriding in group-related code.
    • Removed an unused private method from group utilities.

Closes #40368

Signed-off-by: vramik <vramik@redhat.com>
@coderabbitai

coderabbitai Bot commented Jul 26, 2025

Copy link
Copy Markdown

Walkthrough

This change set introduces a null check in the getSubGroupsCount() method of GroupAdapter, adds an @Override annotation to getRealm() in CachedGroup, removes an unused private method from GroupUtils, and adds a new concurrent read/delete test to GroupTest to verify group operations under load.

Changes

Cohort / File(s) Change Summary
GroupAdapter Null Safety Update
model/infinispan/.../GroupAdapter.java
Modified getSubGroupsCount() to retrieve the model with a null check before accessing subgroup count.
Annotation Addition
model/infinispan/.../CachedGroup.java
Added @Override annotation to the getRealm() method.
Utility Method Removal
services/.../GroupUtils.java
Removed the unused private static method groupMatchesSearchOrIsPathElement.
Concurrent Group Test
tests/base/.../GroupTest.java
Added createMultiDeleteMultiReadMulti() test for concurrent group creation, reading, and deletion.

Sequence Diagram(s)

sequenceDiagram
    participant TestThread as Test Thread
    participant MainThread as Main Thread
    participant GroupAPI as Group API

    MainThread->>GroupAPI: Create 100 groups
    MainThread->>TestThread: Start concurrent reading thread
    loop While not all groups deleted
        TestThread->>GroupAPI: Read all groups (brief)
    end
    MainThread->>GroupAPI: Delete groups sequentially
    MainThread->>TestThread: Signal reading thread to stop
    TestThread->>MainThread: Report exceptions (if any)
    MainThread->>MainThread: Assert no exceptions occurred
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A rabbit hopped through code so neat,
Null checks in groups, a tidy feat!
An annotation placed with care,
Old utils gone—no need to spare.
Tests now race with threads that run,
Ensuring groups break never—none!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature-group-concurrency-implementation

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai

coderabbitai Bot commented Jul 30, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@everettbu

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 30, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/base/src/test/java/org/keycloak/tests/admin/group/GroupTest.java (1)

118-158: Excellent concurrent test for validating the NPE fix.

This test effectively validates the concurrent group access improvements by:

  1. Creating multiple groups to establish test data
  2. Running concurrent read operations while deleting groups
  3. Using thread-safe collections (CopyOnWriteArrayList, AtomicBoolean) for coordination
  4. Asserting that no exceptions occur during concurrent operations

The test directly exercises the scenario described in the PR objective and validates that the null safety improvements in GroupAdapter.getSubGroupsCount() prevent NPEs.

Consider adding a small delay in the reader thread loop to better exercise different timing windows:

 while (!deletedAll.get()) {
     try {
+        Thread.sleep(1); // Small delay to vary timing
         // just loading briefs
         managedRealm.admin().groups().groups(null, 0, Integer.MAX_VALUE, true);
     } catch (Exception e) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e92b825 and 332c9b6.

📒 Files selected for processing (4)
  • model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/GroupAdapter.java (1 hunks)
  • model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/entities/CachedGroup.java (1 hunks)
  • services/src/main/java/org/keycloak/utils/GroupUtils.java (0 hunks)
  • tests/base/src/test/java/org/keycloak/tests/admin/group/GroupTest.java (4 hunks)
💤 Files with no reviewable changes (1)
  • services/src/main/java/org/keycloak/utils/GroupUtils.java
🔇 Additional comments (5)
model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/entities/CachedGroup.java (1)

61-61: Good practice: Adding explicit @OverRide annotation.

The addition of the @Override annotation improves code clarity by explicitly marking this method as an interface implementation, and provides compile-time safety to ensure the method signature remains consistent with the InRealm interface.

model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/GroupAdapter.java (1)

274-275: Excellent fix for the NullPointerException issue.

This change directly addresses the PR objective by adding null safety to prevent NPE during concurrent group access. The defensive programming approach of checking model == null before calling getSubGroupsCount() prevents crashes when the underlying group model is null due to concurrent operations or caching issues.

tests/base/src/test/java/org/keycloak/tests/admin/group/GroupTest.java (3)

28-28: New import added for test assertions.

The Matchers import is correctly added to support the new test's assertion on the empty exceptions list.


80-82: Appropriate imports for concurrent testing.

The imports for CopyOnWriteArrayList, AtomicBoolean, and IntStream are well-chosen for implementing thread-safe concurrent testing functionality.


97-97: Good addition of fail import.

The fail import is correctly added to support the test's error handling when group creation fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants