Skip to content

Prepare applications before server readiness (#642) - #678

Open
leynos wants to merge 2 commits into
issue-641-introduce-preparedapp-and-one-time-route-middleware-preparationfrom
issue-642-prepare-the-application-before-server-readiness
Open

Prepare applications before server readiness (#642)#678
leynos wants to merge 2 commits into
issue-641-introduce-preparedapp-and-one-time-route-middleware-preparationfrom
issue-642-prepare-the-application-before-server-readiness

Conversation

@leynos

@leynos leynos commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Closes #642

Summary

The server now evaluates its startup factory once, prepares one immutable root before installing accept loops, and shares that root with every connection task. Startup failures now surface as typed server errors before readiness can be signalled.

Implementation

  • Prepares the application before workers start and propagates one Arc<PreparedApp> through the runtime, accept loop, and connection spawner.
  • Adds typed factory-build and preparation errors, and exposes from_app for a pre-built application.
  • Moves bundled examples to the server runtime path; documents the once-per-run factory contract and lifecycle state C; adds factory/transform startup benchmarks and regression coverage.

Validation

  • make check-fmt
  • make lint
  • make typecheck
  • make test
  • make markdownlint
  • make nixie

References

Summary by Sourcery

Prepare and share the application before installing server workers so startup failures occur before readiness and connections reuse one immutable root.

New Features:

  • Prepare the application once per server run and share one immutable prepared root across all connection tasks.
  • Add WireframeServer::from_app for serving an already-built application.
  • Expose typed errors for application factory construction and preparation failures before readiness.

Bug Fixes:

  • Prevent server readiness from being signalled when startup factory evaluation or application preparation fails.

Enhancements:

  • Move connection-specific initialization to lifecycle setup state and align examples with the server runtime.
  • Clarify the startup factory contract and application preparation lifecycle in user and migration documentation.

Build:

  • Add a connection-startup benchmark comparing per-connection setup with shared prepared application startup.

Documentation:

  • Update server, developer, user, and migration documentation for one-time application startup and shared prepared roots.

Tests:

  • Add regression coverage for startup factory call counts, preparation timing, readiness behavior, and typed startup errors.

Chores:

  • Remove obsolete example runtime bootstrap and server-loop helpers.

@coderabbitai

coderabbitai Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 100f9002-1394-4e23-a0a5-9db9c18ae57a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Warning

Your free Security trial is over. An organization admin can activate Security or dismiss this notice.


Comment @coderabbitai help to get the list of available commands.

codescene-access[bot]

This comment was marked as outdated.

@sourcery-ai

sourcery-ai Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

The server now builds and prepares its application once before readiness, propagates a shared immutable prepared root through workers and connection tasks, reports startup failures as typed errors, and updates APIs, examples, documentation, tests, and benchmarks for the new lifecycle contract.

Sequence diagram for prepared application server startup

sequenceDiagram
    participant Caller
    participant Server as WireframeServer
    participant Factory as AppFactory
    participant App as WireframeApp
    participant Workers as AcceptLoops
    participant Connection as ConnectionTask

    Caller->>Server: run_with_shutdown()
    Server->>Factory: build()
    Factory-->>Server: WireframeApp
    Server->>App: prepare()
    App-->>Server: PreparedApp
    Server->>Workers: install accept loops with Arc<PreparedApp>
    Server-->>Caller: readiness signal
    Workers->>Connection: spawn_connection_task(Arc<PreparedApp>)
    Connection->>App: handle_connection_result()
Loading

State diagram for server readiness and startup failures

stateDiagram-v2
    [*] --> Unbound
    Unbound --> Bound: bind()
    Bound --> Starting: run_with_shutdown()
    Starting --> FactoryBuildFailed: build() error
    Starting --> PreparationFailed: prepare() error
    Starting --> Ready: workers installed
    Ready --> Running: readiness signalled
    FactoryBuildFailed --> [*]
    PreparationFailed --> [*]
    Running --> Stopping: shutdown
    Stopping --> [*]
Loading

File-Level Changes

Change Details Files
Move application construction and preparation into server startup, then share one immutable prepared root across all workers and connections.
  • Evaluate AppFactory once per run and prepare the application before worker installation/readiness signaling.
  • Pass Arc through runtime, accept loops, and connection tasks.
  • Add regression coverage for factory call counts and startup ordering.
src/server/runtime.rs
src/server/runtime/accept.rs
src/server/connection_spawner.rs
src/server/runtime/tests.rs
tests/prepared_app.rs
Add explicit APIs and typed errors for startup application lifecycle handling.
  • Introduce ServerError variants for factory-build and preparation failures.
  • Add WireframeServer::from_app for already-built applications.
  • Document the once-per-run factory contract and move connection-specific state to lifecycle setup state C.
src/server/mod.rs
src/server/error.rs
src/server/config/mod.rs
docs/server/configuration.md
docs/users-guide.md
docs/v0-2-0-to-v0-3-0-migration-guide.md
CHANGELOG.md
Align examples and architectural guidance with the server-managed prepared application path.
  • Replace example-specific runtime bootstrap/server loop helpers with WireframeServer::from_app.
  • Update ADR and developer documentation for the unified startup and sharing topology.
  • Remove obsolete example support modules.
examples/echo.rs
examples/ping_pong.rs
examples/packet_enum.rs
examples/support/runtime_bootstrap.rs
examples/support/server_loop.rs
docs/adr-012-prepared-application-and-connection-runtime.md
docs/developers-guide.md
Add benchmark coverage for the startup work eliminated by prepared-root sharing.
  • Compare per-connection factory/preparation counts with one shared factory/preparation across connections.
  • Register the connection startup Criterion benchmark.
benches/connection_startup.rs
Cargo.toml

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

codescene-access[bot]

This comment was marked as outdated.

leynos added 2 commits August 28, 2026 22:10
Evaluate and prepare the startup app before accept loops signal readiness.
Share one immutable PreparedApp root with every worker and connection task,
while retaining shutdown tracking and panic isolation.

Document once-per-run factory behaviour, add from_app, and move bundled
examples onto the server runtime path. Add startup-count benchmarks and tests.
Hold middleware preparation behind a barrier and prove that the server does
not signal readiness until preparation completes. Keep the runtime test module
within the repository's 400-line limit.
@leynos
leynos force-pushed the issue-642-prepare-the-application-before-server-readiness branch from f03af46 to e606ea1 Compare August 28, 2026 20:13
@leynos
leynos marked this pull request as ready for review August 28, 2026 23:49

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry @leynos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 4 days and 21 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-28T23:53:04.571990Z e606ea1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e606ea1014

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread CHANGELOG.md
server run, prepares the result once, and shares that immutable application
root across connections. Move per-connection state to `on_connection_setup`,
or use `WireframeServer::from_app(app)` when the application is already
built. See `docs/v0-2-0-to-v0-3-0-migration-guide.md` for migration examples.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Point readers to the current migration guide

This unreleased breaking change belongs to the v0.3.0-to-v0.4.0 transition, but the link sends users to the already-published v0.2.0-to-v0.3.0 guide. More importantly, docs/v0-3-0-to-v0-4-0-migration-guide.md still says at lines 99–104 that factories retain per-connection evaluation and that this work is deferred to issue #642. A v0.3 user following the designated current guide will therefore implement the opposite lifecycle semantics; update that guide and link to it here.

Useful? React with 👍 / 👎.

Comment thread docs/developers-guide.md
Comment on lines +433 to +435
`WireframeServer::from_app` for tracing setup, server preparation, listener
binding, connection spawning, shutdown-aware accept loops, and current-thread
Tokio runtime startup. Keep example-specific address

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Document the bootstrap work required around from_app

WireframeServer::from_app only stores the application in a server builder; it does not initialize tracing, create a Tokio runtime, or parse configuration. The updated examples still perform those steps explicitly with try_init() and tokio::runtime::Builder, so this convention would cause a contributor following the developers' guide to omit required bootstrap code. Describe from_app as replacing application preparation and server-loop wiring while retaining explicit tracing and runtime setup.

Useful? React with 👍 / 👎.

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