Skip to content

Commit

Permalink
fix: stale frappe.local (backport frappe#20695) (frappe#20700)
Browse files Browse the repository at this point in the history
* fix: stale `frappe.local` (frappe#20695)

* fix: stale `frappe.local`

Co-Authored-By: Aditya Hase <aditya@adityahase.com>

* fix: force re-init in request

To ensure that any one bad request can not completely cause recurring
loop of broken requests due to bad locals, we just force-init locals on
every request.

---------

Co-authored-by: Aditya Hase <aditya@adityahase.com>
(cherry picked from commit 1e9ede4)

# Conflicts:
#	frappe/__init__.py
#	frappe/app.py

* chore: conflicts

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Apr 14, 2023
1 parent f96615a commit bfd9dd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions frappe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ def set_user_lang(user, user_language=None):
# end: static analysis hack


def init(site, sites_path=None, new_site=False):
def init(site, sites_path=".", new_site=False, force=False):
"""Initialize frappe for the current site. Reset thread locals `frappe.local`"""
if getattr(local, "initialised", None):
if getattr(local, "initialised", None) and not force:
return

if not sites_path:
Expand Down
22 changes: 18 additions & 4 deletions frappe/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ def application(request):
rollback = after_request(rollback)

finally:
# Important note:
# this function *must* always return a response, hence any exception thrown outside of
# try..catch block like this finally block needs to be handled appropriately.

if request.method in ("POST", "PUT") and frappe.db and rollback:
frappe.db.rollback()

if getattr(frappe.local, "initialised", False):
for after_request_task in frappe.get_hooks("after_request"):
frappe.call(after_request_task, response=response, request=request)
try:
run_after_request_hooks(request, response)
except Exception as e:
# We can not handle exceptions safely here.
frappe.logger().error("Failed to run after request hook", exc_info=True)

log_request(request, response)
process_response(response)
Expand All @@ -104,12 +110,20 @@ def application(request):
return response


def run_after_request_hooks(request, response):
if not getattr(frappe.local, "initialised", False):
return

for after_request_task in frappe.get_hooks("after_request"):
frappe.call(after_request_task, response=response, request=request)


def init_request(request):
frappe.local.request = request
frappe.local.is_ajax = frappe.get_request_header("X-Requested-With") == "XMLHttpRequest"

site = _site or request.headers.get("X-Frappe-Site-Name") or get_site_name(request.host)
frappe.init(site=site, sites_path=_sites_path)
frappe.init(site=site, sites_path=_sites_path, force=True)

if not (frappe.local.conf and frappe.local.conf.db_name):
# site does not exist
Expand Down

0 comments on commit bfd9dd0

Please sign in to comment.