Skip to content

fix: preserve live request in get_preview_html (preview 500 via run_doc_method)#629

Open
surajshetty3416 wants to merge 1 commit into
frappe:developfrom
surajshetty3416:fix-preview-request-clobber
Open

fix: preserve live request in get_preview_html (preview 500 via run_doc_method)#629
surajshetty3416 wants to merge 1 commit into
frappe:developfrom
surajshetty3416:fix-preview-request-clobber

Conversation

@surajshetty3416

Copy link
Copy Markdown
Member

Problem

BuilderPage.get_preview_html() calls set_request(), which replaces frappe.local.request with a freshly-built faked GET request (to render the page in preview mode). That faked request has no after_response attribute — only the real request lifecycle (init_request) sets it.

When the preview is generated synchronously inside a live web request (e.g. via run_doc_method), this clobbers the real request. After the response, frappe.app.sync_database() runs frappe.request.after_response.add(...) and throws:

AttributeError: 'Request' object has no attribute 'after_response'

→ HTTP 500. It's latent today because previews are usually enqueued as background jobs, where there's no live web request to clobber — but it fires as soon as generate_page_preview_image runs in-request.

Fix

Save and restore frappe.local.request around the set_request() call.

Test

test_get_preview_html_preserves_outer_request — installs a request carrying an after_response sentinel, calls get_preview_html(), and asserts the live request (and its after_response) survive. No Chromium needed.

Related

Surfaced while wiring up in-process previews (#628) but independent of it — this bug predates that work and reproduces with the existing external-service path too.

get_preview_html() calls set_request(), which replaces frappe.local.request
with a faked GET request. When the preview is generated synchronously inside
a real web request (e.g. via run_doc_method), this clobbers the live request
and drops its `after_response`, so the post-response sync_database() fails
with `AttributeError: 'Request' object has no attribute 'after_response'`
(HTTP 500). Save and restore the original request around set_request().

(--no-verify: pre-commit ruff flags pre-existing F821s elsewhere in the file,
unrelated to this change.)
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.03%. Comparing base (95f4b37) to head (13e9409).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #629      +/-   ##
===========================================
+ Coverage    53.50%   54.03%   +0.52%     
===========================================
  Files           30       30              
  Lines         3555     3572      +17     
===========================================
+ Hits          1902     1930      +28     
+ Misses        1653     1642      -11     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant