Skip to content

fix: Initialize endpoints on Server start. - #4139

Closed
SandPod wants to merge 1 commit into
serverpod:mainfrom
SandPod:initialize-endpoints-on-server-start
Closed

SandPod wants to merge 1 commit into
serverpod:mainfrom
SandPod:initialize-endpoints-on-server-start

Conversation

@SandPod

@SandPod SandPod commented Oct 31, 2025 •

Copy link
Copy Markdown
Contributor

Moves the initialize call for endpoint from the Serverpod constructor to the server start sequence.

This enables adding dependencies on global state to be configured before the endpoint is constructed.

This will be used in the new auth modules to ensure that AuthConfig is initialized before the endpoint is initialized, enabling us to throw an exception when the server is started with invalid configuration.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

We don't give a guarantee today when the endpoint is initialized, so this should not be something that users are depending on.

Summary by CodeRabbit

  • Bug Fixes

    • Improved server startup reliability with enhanced error handling for endpoint initialization failures. Server will now properly report and prevent startup if initialization encounters issues.
  • Refactor

    • Consolidated endpoint initialization logic to execute at a single point during server startup for more predictable initialization behavior.

@coderabbitai

coderabbitai Bot commented Oct 31, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Endpoint initialization is consolidated to occur in Server.start() with error handling, rather than at multiple initialization points in the Serverpod class. The initialization call is wrapped in a try/catch that reports a framework exception and returns false on failure.

Changes

Cohort / File(s) Summary
Endpoint Initialization Error Handling
packages/serverpod/lib/src/server/server.dart
Wraps endpoints.initializeEndpoints(this) in try/catch block. On exception, reports framework error "Failed to initialize endpoints." and returns false.
Remove Endpoint Initialization Calls
packages/serverpod/lib/src/server/serverpod.dart
Removes endpoints.initializeEndpoints() calls from _innerInitializeServerpod() and _configureInsightsServer() methods.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Serverpod
    participant Server
    participant Endpoints

    Note over Client,Endpoints: Old Flow (Before)
    Client->>Serverpod: new Serverpod()
    Serverpod->>Endpoints: initializeEndpoints(server)
    Serverpod->>Serverpod: _configureInsightsServer()
    Serverpod->>Endpoints: initializeEndpoints(insightsServer)
    Client->>Server: start()
    Server->>Endpoints: initializeEndpoints(this)

    Note over Client,Endpoints: New Flow (After)
    Client->>Serverpod: new Serverpod()
    Note over Serverpod: No endpoint initialization
    Client->>Server: start()
    Server->>Endpoints: try: initializeEndpoints(this)
    alt initialization succeeds
        Endpoints-->>Server: success
    else initialization fails
        Endpoints-->>Server: exception
        Server->>Server: report framework error
        Server-->>Client: false
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • The change consolidates initialization logic with straightforward error handling
  • Limited file scope (2 files) with focused, localized modifications
  • Minor risk: verify that removing initialization from Serverpod constructor methods does not affect initialization order or timing for dependent code that expects endpoints to be initialized earlier

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Description Check ⚠️ Warning The pull request description provides a clear explanation of what is being changed (moving endpoint initialization from the Serverpod constructor to the server start sequence) and why (to enable global state dependencies to be configured before endpoint construction). The author includes a pre-launch checklist and addresses breaking changes. However, the description has a critical gap: the template explicitly requires "You must list at least one issue" in the main description, but the author has not listed or linked to any specific issues that this PR fixes. Instead, only a general explanation of the change's purpose is provided. The author must explicitly list at least one GitHub issue that this PR fixes in the main description section above the pre-launch checklist. This is a required element of the repository's PR template. The current description should be updated to include a line such as "Fixes #XXXX" or "This PR fixes the following issues: #XXXX" with the relevant issue number(s).
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "Initialize endpoints on Server start" accurately captures the primary change in the changeset. The modifications remove endpoint initialization calls from the Serverpod constructor (in two locations) and add a wrapped initialization call in Server.start() with error handling, which directly implements the stated objective of moving endpoint initialization to the server start sequence. The title is concise, specific, and clearly communicates the main intent without vague terms or excessive detail. A teammate reviewing the history would understand that this PR changes when endpoints are initialized in the lifecycle.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@SandPod

SandPod commented Oct 31, 2025

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 31, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@SandPod
SandPod marked this pull request as ready for review October 31, 2025 15:54
@SandPod
SandPod requested a review from a team October 31, 2025 15:54
@SandPod

SandPod commented Oct 31, 2025

Copy link
Copy Markdown
Contributor Author

This PR needs to be complemented with lazy endpoint initialization in the test framework.

@SandPod
SandPod marked this pull request as draft October 31, 2025 16:06

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🚀

@SandPod

SandPod commented Nov 3, 2025

Copy link
Copy Markdown
Contributor Author

@coderabbitai We will not implement this feature right now since it is not critical. Please log an issue for this.

@coderabbitai

coderabbitai Bot commented Nov 3, 2025

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

@SandPod: I'll create a GitHub issue to track this follow-up work.


🏁 Script executed:

#!/bin/bash
# Create a GitHub issue for lazy endpoint initialization in test framework

gh issue create \
  --title "Implement lazy endpoint initialization in test framework" \
  --body "## Description

This issue tracks the need to implement lazy endpoint initialization in the test framework as a follow-up to PR #4139.

PR #4139 moved the endpoint initialization call from the Serverpod constructor to the server start sequence. However, the test framework needs complementary changes to support lazy endpoint initialization.

## Context

- **Related PR:** https://github.com/serverpod/serverpod/pull/4139
- **Comment:** https://github.com/serverpod/serverpod/pull/4139#discussion_r1
- **Requested by:** @SandPod

This feature is not critical for the current release but should be addressed in a future update.

## Tasks

- [ ] Analyze test framework initialization flow
- [ ] Implement lazy endpoint initialization for test framework
- [ ] Ensure test compatibility with new initialization timing" \
  --assignee SandPod

Length of output: 88


Let me retry that with the repository context:


🏁 Script executed:

#!/bin/bash
# Create a GitHub issue for lazy endpoint initialization in test framework

gh issue create \
  --title "Implement lazy endpoint initialization in test framework" \
  --body "## Description

This issue tracks the need to implement lazy endpoint initialization in the test framework as a follow-up to PR #4139.

PR #4139 moved the endpoint initialization call from the Serverpod constructor to the server start sequence. However, the test framework needs complementary changes to support lazy endpoint initialization.

## Context

- **Related PR:** https://github.com/serverpod/serverpod/pull/4139
- **Comment:** https://github.com/serverpod/serverpod/pull/4139#issuecomment
- **Requested by:** @SandPod

This feature is not critical for the current release but should be addressed in a future update.

## Tasks

- [ ] Analyze test framework initialization flow
- [ ] Implement lazy endpoint initialization for test framework
- [ ] Ensure test compatibility with new initialization timing" \
  --assignee SandPod

Length of output: 50


@SandPod: I've created issue #4146 to track the lazy endpoint initialization in the test framework. The issue has been assigned to you and includes a reference back to this PR.

Issue: #4146


🧠 Learnings used
Learnt from: SandPod
Repo: serverpod/serverpod PR: 3904
File: modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_server/test/jwt/integration/authentication_tokens_test.dart:141-143
Timestamp: 2025-08-20T13:29:38.181Z
Learning: In Serverpod test files, SandPod prefers to avoid redundant null-checks in tests when the preconditions guarantee non-null values. They consider such defensive assertions as "redundant bloat" when the test environment is controlled and the implementation guarantees the presence of values like refreshToken.

Learnt from: SandPod
Repo: serverpod/serverpod PR: 4127
File: modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/lib/src/providers/passkey/endpoints/passkey_account_base_endpoint.dart:11-16
Timestamp: 2025-10-30T10:11:34.781Z
Learning: In Serverpod authentication endpoints, SandPod prefers eager initialization of AuthConfig-dependent fields (e.g., `final TokenManager tokenManager = AuthConfig.instance.tokenManager;`) rather than lazy getters, to detect misconfiguration at server startup rather than deferring errors until the first request to the endpoint.

@SandPod SandPod closed this Nov 3, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants