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
Fixed #34806 -- Made the cached_db backend resilient to cache backend errors. #17220
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi & thanks for submitting this PR 😊
There's a change required to the way the logger is created. Also we'll need a new test to confirm the behaviour change.
import logging | ||
|
||
logging.error(f"Caching error: {e}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately we can't just log willy-nilly, we have to follow the standard process of getting a named logger and logging to that to allow users of the framework to capture those logs.
I'm not sure myself what the appropriate logger is for this module but nessita or felixx will be able to confirm that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did talk with Mariusz about this, and he confirmed that:
- we should use a new logger for
django.contrib.sessions
:
import logging
logging.getLogger("django.contrib.sessions")
- we should add a release note about the adding of this logger and a documentation entry in
ref/logging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sulabhkatila Any chance you missed the comment above? I don't see those changes reflected in the current diff.
try: | ||
super().save(must_create) | ||
self._cache.set(self.cache_key, self._session, self.get_expiry_age()) | ||
except MemoryError as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claudep Is this the correct exception to catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we can target any specific exception type. In my use case, it was a TooBig error generated by pylibmc. It looks like the error is defined in https://github.com/lericson/pylibmc/blob/97ace274cffd8db/src/_pylibmcmodule.h#L201C12-L201C12 and raised from C code. I'm afraid we have no real choice than catching the broader Exception
base class.
Oh and there may be some documentation updates as well to let folks know "from x.xx version caching errors will result in logs"… felix & nessita will confirm this though 👍 |
I don't have the answers to the logger questions but I'll check with @felixxm in our next catch up, and get back to you (in about a week). In the meantime, any chance that the lint errors are fixed? |
hi @nessita, can you please lead me in the right direction in terms of adding a test case for this? |
@sulabhkatila Nessita's busy, I can help. You've already added a test? 🤔 |
@sulabhkatila A good starting logger may be |
Hi @shangxiao, But it doesn't really work: the test case passes even when we raise the error here instead of passing (how it is done now). I am now suspicious if I am even catching the exception properly as if there isn't where the exception is raised?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting better 🎉
import logging | ||
|
||
logger = logging.getLogger("django.request") | ||
logger.error("Error saving to cache: %s", e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optionally we can do logger.exception(<message>)
which captures the exception:
https://docs.python.org/3/library/logging.html#logging.Logger.exception
This hasn't been used in Django yet so let's wait to see what Mariusz thinks.
Hi @shangxiao, i have made the proposed changes and also tried to update the documentation in this commit. |
Thanks 🏆 The |
Thanks for the ping, looking into this now! |
Yes, I think we do need a release note stating this and the adding of the new logger. |
I am not OK with that :-) Please let's move the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments. Also, please note that the .DS_Store
binary file that was added, needs to be removed, it does not belong in the repo.
Thank you!
ddc8bf0
to
cab6592
Compare
Hi @shangxiao, I have made the changes as discussed by moving the logger, update to the docs in the appropriate file, and removed the .DS_Store binary file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in one of the comments, a new logger needs to be defined and documented, and also the call to save()
inside the try-except block should be moved outside that block.
Thank you for the work so far!
ee66fa4
to
6a65a73
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @sulabhkatila, please read my last comment in full, there are still multiple things pending to implement, such as documenting the new logger and moving the save()
call to outside the try-except block.
The documenting of the new logger needs to occur in multiple doc pages, I listed those in previous comments I think.
Please only when all those are ready, mark this PR as ready for review in the issue tracker as documented in https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/submitting-patches/#patch-review-checklist
ef6d0a2
to
bc7f895
Compare
Hi @nessita , I apologize for the oversight. Regarding the docs, I just added the release note about the behavior in
Is there some other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @sulabhkatila for the updates.
Regarding your question:
Is there any other pages in the docs that I might need to update?
Yes, the logging docs also need updating (see docs/ref/logging.txt). Could you also please rebase onto main
so you incorporate the latest changes from the main
branch?
8982c70
to
aa971d0
Compare
Hey @sulabhkatila, thank you for your continued work. If this branch is ready for a re-review, please double check that the ticket flags are properly set or unset. This are the docs describing those details. |
0363476
to
e4f218b
Compare
@claudep Hi there! I pushed some code and docs tweaks, I think it's ready to be merged. Would you fancy doing a final review to this PR? (no worries if you can't!) |
…ors. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
e4f218b
to
11a072e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for working on this! We'll see in the future if other backend operations need some similar safeguards.
ticket_34806
I have added a MemoryError handling to ensure that it doesn't crash and logged the issue to let the user know.