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

[master] Avoid extraneous call to SessionRespository.findById(...) in SessionRepositoryRequestWrapper.commitSession() #1732

Closed
wants to merge 1 commit into from

Conversation

pferraro
Copy link

Resolves #1731
Clearing the requested session cache after we've written the session ID to the response avoid the need for a redundant lookup of the session.

@pivotal-issuemaster
Copy link

@pferraro Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@pferraro Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 21, 2020
@pferraro pferraro force-pushed the master branch 2 times, most recently from b8c56f6 to 92cb240 Compare November 21, 2020 15:58
@pferraro pferraro changed the title Avoid extraneous call to SessionRespository.findById(...) in SessionRepositoryRequestWrapper.commitSession() [master] Avoid extraneous call to SessionRespository.findById(...) in SessionRepositoryRequestWrapper.commitSession() Nov 22, 2020
@eleftherias eleftherias self-assigned this Nov 27, 2020
Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @pferraro!

Could you add Closes gh-1731 to the commit message so that it will automatically close the issue you created?

@eleftherias eleftherias added in: core type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 27, 2020
@pferraro
Copy link
Author

@eleftherias Done.

@vpavic
Copy link
Contributor

vpavic commented Dec 4, 2020

The lookup might seem redundant when one considers only what happens within the scope of this method (or even JVM), but have in mind that one of the core challenges Spring Session solves is session management in a cluster.

With that in mind, this needs to be looked at having in mind the scenarios with concurrent requests. The session id validity check that guards the invocation of HttpSessionIdResolver#setSessionId needs to know the up-to-date state, otherwise I think we could expose ourself to a number of subtle issues in scenarios involving concurrent requests, especially since things have been this way since the early days of Spring Session. Hence the unit test that clearly describes that 3 invocations are expected.

An important bit of context here is that SessionRepository#save has historically been implemented in such a way to be as performant as possible, specifically in this case it's relevant that invoking #save on a session that was already deleted from the store by some other request will result in a no-op. This is done to prevent reads within the implementation of #save. I believe that different implementations of SessionRepository aren't consistent here but at the same time there isn't any specific contract here to adhere to. That's why I opened #1277 a while ago.

So the change proposed here IMO shouldn't be done without considering a more substantial review of the whole arrangement.

@pferraro
Copy link
Author

pferraro commented Dec 4, 2020

The lookup might seem redundant when one considers only what happens within the scope of this method (or even JVM), but have in mind that one of the core challenges Spring Session solves is session management in a cluster.

Concurrent requests to a given Session by multiple cluster members are forbidden by section 7.7.2 of the Servlet specification:

Within an application marked as distributable, all requests that are part of a session must be handled by one JVM at a time.

Thus, we only need to be concerned with concurrent requests for a given Session within a single JVM.

With that in mind, this needs to be looked at having in mind the scenarios with concurrent requests. The session id validity check that guards the invocation of HttpSessionIdResolver#setSessionId needs to know the up-to-date state...

Assuming that the state referenced by a Session returned by SessionRepository.findById(...) for a given request is, by definition, up-to-date until the corresponding Session#save completes, can we not simply reorder this logic such that we perform the session id validity check before Session#save?

I believe that different implementations of SessionRepository aren't consistent here but at the same time there isn't any specific contract here to adhere to. That's why I opened #1277 a while ago.

It seems to me that a SessionRepository implementation that persists an invalid session is incorrect, no?

…epositoryRequestWrapper.commitSession() due to premature cache clearing.

Closes spring-projectsgh-1731
@vpavic
Copy link
Contributor

vpavic commented Dec 4, 2020

Thus, we only need to be concerned with concurrent requests for a given Session within a single JVM.

I'm afraid the excerpt you're quoting isn't really applicable to modern environments, as nowadays we typically have a round robin based load-balancers that distribute requests evenly between the nodes in a cluster. Spring Session is exactly here to address that scenario so that users don't have to resort to anti-patterns like sticky sessions (which is something Servlet spec seems to suggest there).

can we not simply reorder this logic such that we perform the session id validity check before Session#save?

Logically, the concern I'm expressing here would still be present, because #save precedes #setSessionId. Historically, we've seen many issues reported due to concurrency scenarios and that's why I'm hesitant to make any changes that could lead to subtle issues. We need to review the overall approach here and that looks like a 3.0 target to me.

It seems to me that a SessionRepository implementation that persists an invalid session is incorrect, no?

Yes, I agree with you there and that's exactly why I opened #1277.

@pferraro
Copy link
Author

pferraro commented Dec 8, 2020

Logically, the concern I'm expressing here would still be present, because #save precedes #setSessionId. Historically, we've seen many issues reported due to concurrency scenarios and that's why I'm hesitant to make any changes that could lead to subtle issues. We need to review the overall approach here and that looks like a 3.0 target to me.

Can you describe a scenario where the proposed change is problematic - that is not already problematic using the current logic?

@lnxmad
Copy link

lnxmad commented Jul 20, 2021

Any update on this?

@quaff
Copy link
Contributor

quaff commented Sep 3, 2021

@pferraro @lnxmad Could you try #1909?

@rwinch
Copy link
Member

rwinch commented Oct 18, 2022

Closing in favor of gh-1909

@rwinch rwinch closed this Oct 18, 2022
@rwinch rwinch added the status: duplicate A duplicate of another issue label Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
8 participants