Registry Mirror
Skip to content

Conversation

@grandnode-dev

Copy link
Copy Markdown
Member

Type: bugfix

Issue

With Redis:PersistKeysToRedis on, an instance does not start while that Redis is unreachable. AddGrandDataProtection calls ConnectionMultiplexer.Connect(PersistKeysToRedisUrl) with AbortOnConnectFail at its default (true), and the default PersistKeysToRedisUrl in appsettings has no abortConnect=false, so ConfigureServices throws:

Unhandled exception. StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). Error connecting right now. To allow this multiplexer to continue retrying until it's able to connect, use abortConnect=false ...
   at Grand.Web.Common.Infrastructure.ServiceCollectionExtensions.AddGrandDataProtection(...)

A short Redis restart during a deploy or a pod reschedule therefore takes down every new instance, even though a running instance would ride it out.

Solution

Parse the connection string with ConfigurationOptions.Parse and set AbortOnConnectFail = false, the same way the pub/sub multiplexer already does in StartupApplication.RegisterCache. Connect now returns a multiplexer that keeps reconnecting in the background.

What Data Protection does while the key ring cannot be read (from KeyRingProvider in ASP.NET Core, confirmed by running it):

  • At startup its hosted service tries to load the ring, logs Key ring failed to load during application startup and lets the app start.
  • With no ring cached yet (cold start), every operation that needs it (antiforgery, auth cookies, TempData) logs An error occurred while reading the key ring and throws, so those requests answer 500 after the Redis backlog timeout (syncTimeout, 5 s by default). Nothing is cached, so the next request tries again.
  • No new key is generated while Redis is down: the read throws, it does not return an empty ring, so instances cannot end up with different keys.
  • An instance that already has a ring keeps using it; a failed refresh extends the cached ring by a few minutes and logs the error.
  • Once Redis answers, the next request reads the ring and requests succeed again, with no restart.

So the instance starts and /health/live answers, but pages that need Data Protection fail until Redis is back. That is the intended trade-off: readiness still does not probe Redis (see R11 in the resilience roadmap).

Breaking changes

None. An explicit abortConnect=true in PersistKeysToRedisUrl is now overridden, as it already was for RedisPubSubConnectionString.

Testing

  1. dotnet test src/Tests/Grand.Web.Common.Tests --filter DataProtectionRedisTests: AddGrandDataProtection no longer throws with an unreachable Redis (it failed with RedisConnectionException before the fix).
  2. With no Redis listening on 6379, start the storefront with Redis__PersistKeysToRedis=true. Before: the process exits with the exception above. After: Application started, /health/live returns 200, / returns 500 after ~5 s and the log shows An error occurred while reading the key ring.
  3. Leave the app running and start Redis (docker run --rm -p 6379:6379 redis:7-alpine). Within a few seconds / responds normally without a restart, and redis-cli -n 1 KEYS * shows DataProtection-Keys.
  4. Full suite: dotnet test GrandNode.slnx passes (Aspire.ServiceDefaults.Tests failed once in the parallel run and passed 3 out of 3 times on its own; it does not touch this code).

The key store multiplexer was created with AbortOnConnectFail at its
default, so an unreachable Redis threw from ConfigureServices and the
instance never started. Parse the connection string and turn the abort
off, the same way the pub/sub multiplexer does; Data Protection logs
the failed key ring read and retries on the next request.
Copilot AI balanced review requested due to automatic review settings December 2, 2026 16:07

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@grandnode-dev
grandnode-dev merged commit 948bb7b into develop Dec 2, 2026
9 checks passed
@grandnode-dev
grandnode-dev deleted the fix/data-protection-redis-startup branch December 2, 2026 16:54
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.

2 participants