Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check hasNext when when accessing sessionIds from UserDestinationResult #34333

Closed

Conversation

brandenclark
Copy link
Contributor

@brandenclark brandenclark commented Jan 29, 2025

Found a bug in UserDestinationMessageHandler.SendHelper#send that occurs when the UserDestinationResult was created by the pre-6.1.x constructor. Bug was introduced with 3277b0d

Ideally this fix will target 6.1+ and should be compatible. Not sure if there's a special way to call that out as a first-time contributor.

Changes

  • Add a test that reproduces the error and subsequently covers the fix
  • Check if the iterator has a next value before accessing it and use the default condition if it does not
    • Using the default seems to be the original intent and was just missing this initial check

Bug Details

The UserDestinationMessageHandler unsafely retrieves values from an iterator of sessionId strings to lookup ordered messages.

This happens when the UserDestinationResult used to route the message was created with the pre-6.1.x constructor this retrieval throws a NoSuchElementException.

This is because the older constructor passes a null to the newer constructor for the sessionIds constructor arg. The class attempts to safe handle this null arg by creating an EmptySet that eventually provides an [EmptyIterator],(https://github.com/openjdk/jdk/blob/98a93e115137a305aed6b7dbf1d4a7d5906fe77c/src/java.base/share/classes/java/util/Collections.java#L4559) which then throws for Set#next.

This bug seems to only happen for custom implementations of UserDestinationResolver as the DefaultUserDestinationResolver always provides a populated set to the new constructor.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Signed-off-by: Branden Clark <brandenrayclark@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 29, 2025
@sbrannen sbrannen added the in: messaging Issues in messaging modules (jms, messaging) label Jan 29, 2025
@sbrannen sbrannen requested a review from rstoyanchev January 29, 2025 15:09

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Signed-off-by: Branden Clark <brandenrayclark@gmail.com>
@rstoyanchev rstoyanchev self-assigned this Feb 4, 2025
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 4, 2025
@rstoyanchev rstoyanchev added this to the 6.2.3 milestone Feb 4, 2025
@rstoyanchev rstoyanchev added the for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x label Feb 4, 2025
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

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

The proposed updates make sense. Thanks for the pull request.

As UserDestinationResult is typically created by the framework, and we no longer use the pre-6.1 constructor, we should probably deprecate it. Could you clarify what or who is calling that constructor?

@brandenclark
Copy link
Contributor Author

brandenclark commented Feb 7, 2025

We have a custom UserDestinationResolver instead of using the framework provided one, which should be the only way to trigger this.

I'm pretty novice in this space, but it seems like the pre-6.1 constructor is still useful when needing to customize the UserDestinationResolver and explicitly not requiring ordered messaging templates. Also feels strange to deprecate what was the default behavior before 6.1 and require deep knowledge of the subsystem to safely continuing to do that. Especially since the behavior is still supported in the UserDestinationMessageHandler and not being removed entirely.

@rstoyanchev rstoyanchev changed the title Check hasNext when looking for ordered UserDestinationResult sessionIds Check hasNext when when accessing sessionIds from UserDestinationResult Feb 10, 2025
rstoyanchev pushed a commit that referenced this pull request Feb 10, 2025
See gh-34333

Signed-off-by: Branden Clark <brandenrayclark@gmail.com>
@rstoyanchev
Copy link
Contributor

Thanks for the feedback. I've left both constructors.

rstoyanchev pushed a commit that referenced this pull request Feb 10, 2025
Closes gh-34333

Signed-off-by: Branden Clark <brandenrayclark@gmail.com>
@github-actions github-actions bot added the status: backported An issue that has been backported to maintenance branches label Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x in: messaging Issues in messaging modules (jms, messaging) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants