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

throw an exception on id.get if session is not available #57902

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamkrillin
Copy link

@iamkrillin iamkrillin commented Sep 16, 2024

Throw exception on id.get

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Description

Throw an exception in DistributedSession.Id.Get if the session is not available

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware label Sep 16, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 16, 2024
Copy link
Contributor

Thanks for your PR, @iamkrillin. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@amcasey
Copy link
Member

amcasey commented Sep 17, 2024

It looks like the unavailability of a session is already signalled by an empty Id. Is that not sufficient?

@iamkrillin
Copy link
Author

The problem I was running into was two fold:

  1. The documentation says the Id returned is unique, which strictly speaking, if there is a possibility for it to return "" is not always true, but the bigger problem was..
  2. When using sqlserverdistributedcache, if there is a problem with the setup (server not available, table missing, etc). The error gets eaten then logged to the console (which is not ideal when using IIS hosting)

These two things combined lead to a scenario where things looked to be working, but were not working. When I was working on this PR I though it was better to throw an exception since that would make it very clear that things were not working, but returning null would also work

Either of these would be preferable to eating a error, then returning a value IMO

@amcasey
Copy link
Member

amcasey commented Sep 17, 2024

I'm not actually familiar with this type, but the current behavior certainly appears to be intentional. If we were going to move to a model where it throws, wouldn't it make more sense to just add a throw in Loads catch block?

Similarly, it would have been easy to give each failed session its own guid, so I assume they were grouped together with the id "" on purpose. (Whether that purpose is valid, I have no idea, though it's a common enough pattern.)

@iamkrillin
Copy link
Author

That's a fair enough point, I did consider adding a throw into the catch in Load, I ultimately decided against it since Load is called in many places and I wanted my change to be as small as possible.

Copy link
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 24, 2024
@amcasey
Copy link
Member

amcasey commented Oct 4, 2024

I was hoping an expert would chime in, but I'm not sure there is one for this type. 😆

Barring that, I think I'd prefer to just set it to null, rather than string.Empty in Load, which should cause it to use a fresh guid. Having said that, I can also see the value in just leaving it. If there is a session ID and it just couldn't be loaded, assigning a fresh one is a little strange. It looks like the expectation of the API is that the other properties will be disregarded if IsAvailable is false.

If you need more explicit error behavior in your app, do you have the option of either checking IsAvailable or switching to LoadAsync, which appears to throw when you want it to?

@iamkrillin
Copy link
Author

I changed my application to check IsAvailable before opening this pull request, but figured if this behavior impacted me, perhaps it is impacting others. My Application was checking for null before as an error condition so that makes me think at some point in the past perhaps this API returned null in that scenario. My application has been around since .net 3 and is being updated to .net core so it may have been long ago.

@amcasey
Copy link
Member

amcasey commented Oct 7, 2024

if this behavior impacted me, perhaps it is impacting others

Thanks for taking time to contribute! 😄

it may have been long ago

It certainly seems possible that the sentinel used to be null and changed to string.Empty sometime in the last decade. If we were designing the API now, I think there would be a strong argument in favor of making it throw. In fact, I'm guessing that did actually happen with LoadAsync was added.

In my personal opinion, the best way forward would be to update the doc comments to explain that IsAvailable should be called before Id, etc. I would also consider having Load set it to null to cause a new ID to be generated, though I think the value of that is diminished pretty substantially if the check-IsAvailable-first convention is made explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants