Skip to content

Run plugin asgi_wrapper middleware inside the startup-arming layer - #2891

Open
asg017 wants to merge 4 commits into
asg017/first-request-4-shutdown-hookfrom
asg017/first-request-5-wrapper-reorder
Open

Run plugin asgi_wrapper middleware inside the startup-arming layer#2891
asg017 wants to merge 4 commits into
asg017/first-request-4-shutdown-hookfrom
asg017/first-request-5-wrapper-reorder

Conversation

@asg017

@asg017 asg017 commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Some ASGI middlware fixes. To be honest, not 100% sure how this is relevant or what it fixes, but Claude was insistent. Seems to handle some corner cases of plugin hooks that need to be ran after startup finishes?

🤖 Claude-generated PR description

Fifth PR in the startup/lifecycle stack, on top of #2890.

What this does

Reorders the ASGI middleware assembly in Datasette.app() so plugin asgi_wrapper middleware runs inside the startup-arming layer (AsgiRunOnFirstRequest) but still outside AsgiLifespan. Wrappers get a single, simple contract: every http/websocket scope they see arrives after startup has fully completed — on both the lifespan path and the first-request fallback.

Why

Previously the wrapper loop was outermost, which had two consequences:

  • a wrapper could see request scopes before startup ran (e.g. before another plugin's startup hook had run internal-database migrations);
  • a short-circuiting wrapper — an auth plugin returning 401/403, a CORS plugin answering a preflight — that never called the inner app deferred startup indefinitely for those requests.

lifespan scopes are unaffected: AsgiRunOnFirstRequest ignores them, so plugins that inspect or wrap lifespan events see identical message flow.

Changes

Tests

Three new tests in tests/test_lifespan.py: a wrapper asserting startup has completed before any http scope (fails on the pre-reorder app()), a short-circuiting 403 wrapper that no longer prevents startup, and a recording wrapper pinning that lifespan scopes still reach wrappers.

Stack created with GitHub Stacks CLIGive Feedback 💬

@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (9fba1d3) to head (c2f7735).

Files with missing lines Patch % Lines
datasette/app.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           asg017/first-request-4-shutdown-hook   #2891   +/-   ##
====================================================================
  Coverage                                  0.00%   0.00%           
====================================================================
  Files                                        74      74           
  Lines                                     12461   12461           
====================================================================
  Misses                                    12461   12461           

☔ 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.

@asg017
asg017 force-pushed the asg017/first-request-5-wrapper-reorder branch from 7a7fa81 to 5ea77b3 Compare August 31, 2026 21:25
@simonw
simonw force-pushed the asg017/first-request-5-wrapper-reorder branch from 5ea77b3 to f9ab3d8 Compare September 1, 2026 16:32
@simonw
simonw force-pushed the asg017/first-request-5-wrapper-reorder branch from f9ab3d8 to 74c898a Compare September 1, 2026 16:39
@asg017
asg017 marked this pull request as ready for review September 2, 2026 16:42
asg017 and others added 4 commits September 2, 2026 09:49
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
Rolled down from the stack's docs-only tip PR: the lifecycle section now
states that startup completes before plugin asgi_wrapper middleware sees
any request, and the RELEASE_NOTES_DRAFT_05.md scratch file is gone -
its content lands in the changelog in the tasks-endpoint PR at the top
of the stack.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
@asg017
asg017 force-pushed the asg017/first-request-5-wrapper-reorder branch from 89ad91d to c2f7735 Compare September 2, 2026 16:49
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