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

Use a regular flash object if session store is disabled #42230

Merged
merged 1 commit into from
May 19, 2021

Conversation

ricardotk002
Copy link
Contributor

@ricardotk002 ricardotk002 commented May 15, 2021

Summary

After #42167, all apps (except api only ones) have access to the flash
module. If the session store is disabled, then an empty flash object
is used.

The old null flash was removed because of the maintenance costs.

cc/ @byroot

@rails-bot rails-bot bot added the actionpack label May 15, 2021
@pixeltrix
Copy link
Contributor

@byroot we had the discussion about whether writing into a null session should raise or not which is a 50/50 call, but I think we should at minimum log it at the debug level to give developers a chance of seeing why their flash messages aren't appearing.

@byroot
Copy link
Member

byroot commented May 15, 2021

we had the discussion about whether writing into a null session should raise or not

Yes, and everyone agreed about writing to a disabled session being a failing call.

But in my opinion Flash is not the session, it just happens to be backed by it, and semantically it degrade fairly well.

I think we should at minimum log it at the debug level

Yes that's a good idea.


def replace(h); end

def keys; end
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that it's correct to have all these methods return nil like this. Sure it will now longer break if for instance you call flash.keys, but returning nil is totally unexpected and will likely break the caller.

NullObjects are notoriously hard to get right. I'm starting to wonder if returning a regular Flash object wouldn't be better. You'd just get a new one on every request.flash call. I doubt anyone has request.flash as a hotspot so I don't think a few extra allocation are big concern.

This will also be a good place to put the log suggested by @pixeltrix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[...] returning nil is totally unexpected and will likely break the caller.

Agree, I'll try to replicate the responses too. I also think that logs would be helpful.

I'm starting to wonder if returning a regular Flash object wouldn't be better.

Well yes, the main issue in #42167 was that the Flash middleware wasn't present when some other modules (eg. ETagWithFlash) depended on it. The null object was added for the sake of correctness I would say, as even though it is present, it basically no-ops. A regular flash object works too, with the difference that it indeed would behave as if the flash module were working just fine.

Let me know if this null object still makes sense, so that I work on both items 😄.

Copy link
Member

Choose a reason for hiding this comment

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

Let me know if this null object still makes sense

Well it makes sense, but maintenance wise I think it's much more work than just returning an empty Flash. So I'd go with the later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I've removed the null object, now the request should have access to a regular (empty) flash object, even when the session store is disabled.

Btw, I was checking your branch the other day and tried to add the logging you mentioned: ricardotk002@587d599c4c

Something I noticed was that the Rails session always wraps the previous session into a regular enabled one, even if that previous one comes disabled (for instance, this is what I did for the tests). I'm not sure that'd be a common case but perhaps something to keep in mind:

ricardotk002@587d599c4c#diff-40af0c0e76bdb69f4240deed5d844e4564115d291f8f73b0c4ebf523a1ed16bdR19-R23

@byroot
Copy link
Member

byroot commented May 15, 2021

we had the discussion about whether writing into a null session should raise or not

Ref: #42231

@ricardotk002 ricardotk002 changed the title Add missing methods to ActionDispatch::Flash::NullFlash Use an regular flash object if session store is disabled May 18, 2021
@byroot
Copy link
Member

byroot commented May 18, 2021

Could you rebase? I just merged #42231, so now you can easily check for session.enabled?

@ricardotk002
Copy link
Contributor Author

@byroot Sorry, I'm confused. Should I re-add the null-object with full API? I removed it as per your comment #42230 (comment) and calls to session.enabled? are no longer required.

@byroot
Copy link
Member

byroot commented May 18, 2021

No I wasn't asking to re-add the null object, just to check wether this PR works well with the session change, and eventually to make sure we don't try to persist the Flash object in the session if it is disabled.

After rails#42167, all apps (except api only ones) have access to the flash
module. If the session store is disabled, then an empty flash object
is used.

This patch also prevents the flash from being committed to the session
if this is not enabled.
@ricardotk002 ricardotk002 changed the title Use an regular flash object if session store is disabled Use a regular flash object if session store is disabled May 19, 2021
@ricardotk002
Copy link
Contributor Author

@byroot I applied the changes you suggested. Everything seems to be working correctly. Thanks for reviewing.

@byroot byroot merged commit c047e21 into rails:main May 19, 2021
@ricardotk002 ricardotk002 deleted the correct-null-flash-api branch May 28, 2021 04:36
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.

None yet

3 participants