Skip to content

Keep BackgroundService failures visible to the host behind the lifecycle decorator - #5819

Merged
timothycoleman merged 1 commit into
masterfrom
timcoleman/db-2281-lifecycle-logging-decorator-hides
Nov 26, 2026
Merged

timothycoleman merged 1 commit into
masterfrom
timcoleman/db-2281-lifecycle-logging-decorator-hides

Conversation

@timothycoleman

@timothycoleman timothycoleman commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

This does not change how KurrentDB Release builds behave. Release builds of the server and the embedded node use BackgroundServiceExceptionBehavior.Ignore, so a background service that fails never stopped the node and still won't. The only difference in Release is that the host's own error log for the failure is written again. Debug builds use StopHost, which had stopped working and works again.

The lifecycle logging decorator from #5680 wraps every hosted service in a plain IHostedService. The host only watches ExecuteTask for services it sees as BackgroundService, so behind the decorator it never noticed a background service fail after startup. Background services are now wrapped in a decorator derived from BackgroundService that passes ExecuteTask through.

IHostedLifecycleService would be hidden the same way, but nothing implements it today, so it is left out.

Fixes DB-2281.

🤖 Generated with Claude Code

…cle decorator.

The host only watches ExecuteTask and applies BackgroundServiceExceptionBehavior
for services it sees as BackgroundService, so wrapping them in a plain
IHostedService decorator hid their failures and made StopHost a no-op. Background
services now get a decorator derived from BackgroundService that passes
ExecuteTask through and delegates start/stop logging to the existing decorator.

DB-2281

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timothycoleman
timothycoleman requested a review from a team as a code owner November 26, 2026 09:36
Copilot AI balanced review requested due to automatic review settings November 26, 2026 09:36
@linear-code

linear-code Bot commented Nov 26, 2026

Copy link
Copy Markdown

DB-2281

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The lifecycle decorator now uses a BackgroundService-derived wrapper for background services and retains the existing decorator for other hosted services. Tests cover the wrapper type, inner task completion, and host shutdown after a background-service failure.

Changes: Hosted service lifecycle

Layer / File(s) Summary
Background service decoration and lifecycle validation
src/KurrentDB.Hosting/HostedServiceLifecycleDecorator.cs, src/KurrentDB.Hosting/NodeWebApplication.cs, src/KurrentDB.Components.Tests/HostedServiceLifecycleDecoratorTests.cs
The decorator selects a wrapper based on service type. The wrapper delegates lifecycle operations and exposes the inner ExecuteTask. Tests check task completion and verify that a failure triggers host shutdown when BackgroundServiceExceptionBehavior.StopHost is configured.

Sequence Diagram(s)

sequenceDiagram
  participant NodeWebApplication
  participant HostedServiceLifecycleDecorator
  participant BackgroundServiceLifecycleDecorator
  participant BackgroundService
  NodeWebApplication->>HostedServiceLifecycleDecorator: Decorate hosted service
  HostedServiceLifecycleDecorator->>BackgroundServiceLifecycleDecorator: Select wrapper for BackgroundService
  BackgroundServiceLifecycleDecorator->>BackgroundService: Delegate start and stop
Loading
Merge Risk: Minimal · up to ece23

This change keeps background service failures visible to the host so the configured StopHost behavior applies. No blocking issues were identified. Tests were not run locally, so rely on CI before merging.

Security architecture risk: Minimal · up to ece23

Lifecycle logging no longer hides background-service failures from the host. Debug builds can now shut down as configured, while other builds log the failure without triggering shutdown through this policy. The change does not add a request entry point or transfer ownership of the background work.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — A newly observed fault affects the host that registered the worker. Under StopHost it requests shutdown of that host, not just the worker; under Ignore it does not request shutdown through this policy.

Trust Boundaries and Controls

  • observed — The decorator selects a wrapper from the runtime type of a registered service, not from request data. The generic host retains authority over the response to an observed task fault.

Resilience and Maintainability Implications

  • observed — The embedded host already has a handler that faults its completion when ApplicationStopping is signaled without an explicit stop request. Newly visible background faults can reach that existing notification path when StopHost applies.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 1d52a8a3-d9c3-4eab-9b79-13674876d729
Commits

Reviewing files that changed from the base of the PR and between ac6319d and ece2344.

Files selected for processing: 3

  • src/KurrentDB.Components.Tests/HostedServiceLifecycleDecoratorTests.cs
  • src/KurrentDB.Hosting/HostedServiceLifecycleDecorator.cs
  • src/KurrentDB.Hosting/NodeWebApplication.cs
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Finishing Touches
Fix failing CI checks

  • Commit to this branch
  • Create a new PR

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

🟢 Approval recommended

No active review issues are recorded for the change restoring host visibility into background-service failures while preserving lifecycle logging.

No open issues 🎉

What changed in this PR

Restores the generic host’s ability to observe background-service failures and apply BackgroundServiceExceptionBehavior while preserving lifecycle logging.

Changes:

  • Adds a BackgroundService decorator that exposes the wrapped service’s execution task and delegates lifecycle logging.
  • Updates hosted-service registration to select the appropriate decorator per instance.
  • Adds tests for decorator selection, execution-task forwarding, and host shutdown on background-service failure.
File Description
src/​KurrentDB.Hosting/​NodeWebApplication.cs Registers the instance-aware decorator factory.
src/​KurrentDB.Hosting/​HostedServiceLifecycleDecorator.cs Adds decorator selection and preserves background-task visibility to the host.
src/​KurrentDB.Components.Tests/​HostedServiceLifecycleDecoratorTests.cs Tests decorator behavior and the StopHost failure policy.

Review effort: Balanced

@timothycoleman
timothycoleman merged commit d22884e into master Nov 26, 2026
167 of 169 checks passed
@timothycoleman
timothycoleman deleted the timcoleman/db-2281-lifecycle-logging-decorator-hides branch November 26, 2026 11:02
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.

3 participants