Skip to content

Fix SurrealDB server readiness: add health check, remove provisioning retry loop - #1503

Merged
afscrome merged 4 commits into
mainfrom
afscrome-fix-surrealdb-readiness
Aug 2, 2026
Merged

afscrome merged 4 commits into
mainfrom
afscrome-fix-surrealdb-readiness

Conversation

@afscrome

@afscrome afscrome commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Based on analysis from #1497

The SurrealDbServerResource had a startup race: OnResourceReady fired as soon as the container was running, before SurrealDB was actually accepting WebSocket/RPC traffic. Namespace/database bootstrapping could then fail at the connection level, and the fallback retry loop (while (!ct.IsCancellationRequested)) could hang indefinitely until the outer test timeout.

This PR fixes the race cleanly without adding more recovery logic:

Server health check -- AddSurrealServer now registers a SurrealDbServerHealthCheck (via WithHealthCheck) that calls client.Health() over WebSocket. The check resolves the connection string lazily via ConnectionStringExpression.GetValueAsync(ct) on each poll, so no OnConnectionStringAvailable plumbing is needed. A 5-second linked CancellationTokenSource bounds each probe, since SurrealDbClient.Health() has no internal timeout on the WS engine. OnResourceReady now only fires after the health check passes, i.e. after the RPC channel is confirmed usable.

Provisioning fail-fast -- The open-ended while (!ct.IsCancellationRequested) retry loop in EnsuresNsDbCreated is removed and replaced with a single Use() call. CreateNamespaceAsync and CreateDatabaseAsync no longer swallow exceptions; failures throw DistributedApplicationException immediately with resource name, namespace, and database in the message.

Log cleanup -- Two LogInformation calls in SurrealDbHealthCheck that fired on every poll are removed.

PR Checklist

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • PR doesn't include merge commits (always rebase on top of our main, if needed)
  • New integration
    • Docs are written
    • Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that)
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Contains NO breaking changes
  • Every new API (including internal ones) has full XML docs
  • Code follows all style conventions

Other information

The SurrealDbServerHealthCheck private class is self-contained inside SurrealDbBuilderExtensions and is not part of the public API. The existing SurrealDbHealthCheck (used for database-level health checks) is unchanged except for removal of the noisy info logs.

Docker integration tests (AppHostTests) are the authoritative end-to-end validation and require Docker to run.

… retry loop

- Add WithHealthCheck to SurrealDbServerResource so OnResourceReady only
  fires after SurrealDB WebSocket/RPC is confirmed usable via SurrealDbHealthCheck
  (Health() + RETURN 1 over WebSocket)
- Add OnConnectionStringAvailable to populate SurrealDbOptions for the
  server health check, matching the pattern used by namespace/database resources
- Remove the open-ended namespace-existence retry loop in EnsuresNsDbCreated;
  replace with a single Use() call that fails immediately on error since the
  health check already proves the server is ready
- Make CreateNamespaceAsync throw DistributedApplicationException on failure
  instead of silently logging and continuing
- Make CreateDatabaseAsync throw DistributedApplicationException on failure
  instead of silently logging and continuing; error messages include resource
  name, namespace, and database for easy diagnosis
- Remove noisy LogInformation calls from SurrealDbHealthCheck that fired
  on every health check poll
- Add AddSurrealServerRegistersHealthCheck test to verify the annotation

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@afscrome
afscrome requested a review from Odonno as a code owner August 1, 2026 22:44
Copilot AI review requested due to automatic review settings August 1, 2026 22:44
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/CommunityToolkit/Aspire/main/eng/scripts/dogfood-pr.sh | bash -s -- 1503

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/CommunityToolkit/Aspire/main/eng/scripts/dogfood-pr.ps1) } 1503"

Copilot AI 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.

Pull request overview

This PR improves the SurrealDB hosting integration’s startup reliability by gating SurrealDbServerResource readiness on an actual WS/RPC health probe, and by making provisioning failures fail fast instead of retrying indefinitely.

Changes:

  • Register and attach a new server-level health check for AddSurrealServer, and use it to gate OnResourceReady.
  • Remove the open-ended namespace provisioning retry loop and throw DistributedApplicationException with more context on provisioning failures.
  • Reduce health-check log noise by removing per-poll LogInformation lines.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/CommunityToolkit.Aspire.Hosting.SurrealDb.Tests/AddSurrealServerTests.cs Adds a test asserting the server resource has a health check annotation (suggestion: also assert DI registration).
src/CommunityToolkit.Aspire.SurrealDb/SurrealDbHealthCheck.cs Removes noisy per-poll info logging from the DB-level health check.
src/CommunityToolkit.Aspire.Hosting.SurrealDb/SurrealDbBuilderExtensions.cs Adds server readiness health check, removes provisioning retry loop, and improves fail-fast exceptions for namespace/database creation.

Comment thread src/CommunityToolkit.Aspire.Hosting.SurrealDb/SurrealDbBuilderExtensions.cs Outdated
Comment on lines +732 to +736
catch (Exception ex)
{
logger.LogError(ex, "SurrealDB server health check for '{ResourceName}' raised an exception.", server.Name);
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
Comment on lines +89 to +94
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();

var containerResource = Assert.Single(appModel.Resources.OfType<SurrealDbServerResource>());
var healthCheckAnnotation = Assert.Single(containerResource.Annotations.OfType<HealthCheckAnnotation>());
Assert.Equal("surreal_server_check", healthCheckAnnotation.Key);
}
afscrome and others added 2 commits August 2, 2026 00:12
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@afscrome
afscrome temporarily deployed to azure-artifacts August 1, 2026 23:19 — with GitHub Actions Inactive
Comment thread src/CommunityToolkit.Aspire.Hosting.SurrealDb/SurrealDbBuilderExtensions.cs Outdated
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Branch Rate Complexity Health
CommunityToolkit.Aspire.Hosting.SurrealDb 89% 74% 200 ✔
CommunityToolkit.Aspire.Hosting.SurrealDb.ApiService 85% 62% 236 ✔
CommunityToolkit.Aspire.SurrealDb 86% 75% 38 ✔
Summary 87% (980 / 1122) 67% (278 / 412) 474 ✔

@afscrome
afscrome temporarily deployed to azure-artifacts August 2, 2026 10:53 — with GitHub Actions Inactive
@afscrome
afscrome enabled auto-merge August 2, 2026 10:58
@afscrome
afscrome merged commit 3aa2aa0 into main Aug 2, 2026
17 checks passed
@afscrome
afscrome deleted the afscrome-fix-surrealdb-readiness branch August 2, 2026 11:10

This branch was previously deployed

1 inactive deployment
azure-artifacts — d6cad0a0 Deployed Aug 2, 2026 by afscrome via publish-azure-artifacts #3532
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants