Round #702

10 May 2026, 15:08· csharp· Meta-Llama-3.3-70B-Instruct· r13-clean-hints
Complexity
band 0
ZP / UO / OBS
3 / 3 / 3
Journey weight
649
Lightshift ΔL
+0.00

Prompt

Implement a graceful shutdown handler using IHostApplicationLifetime, with a 30-second drain window and structured logging at each phase.

Zero-point index (what the engine surfaced)

ZPI-ZPI-0001 | 2026-05-10T15:08:18Z Implement a graceful shutdown handler using IHostApplicationLifetime, with a 30-second drain window and structured logging at each phase. ZERO POINT — 3 entries (structural invariants) ● hsl(209.91, 100%, 15%) ILogger<TCategoryName> [contract] ● hsl(192.14, 100%, 15%) ILoggerFactory [contract] ● hsl(258.53, 100%, 15%) ILoggerProvider [contract] UNOBSERVED — 3 entries (implied truths) ○ hsl(83.08, 100%, 42%) ILogger of T injection [function] ○ hsl(86.77, 100%, 7%) Structured log message [function] ○ hsl(90.46, 100%, 7%) High-performance logging [function] OBSERVED — 3 entries (explicit in request) ◉ hsl(85.13, 100%, 50%) using [feature] ◉ hsl(98.46, 100%, 50%) with [feature] ◉ hsl(234.87, 100%, 50%) using [feature] COMPLEXITY │ 9 entries │ spread: 152° │ band: LOW

Baseline response (no ZPI)

Here's an example implementation of a graceful shutdown handler using `IHostApplicationLifetime` in C#: ```csharp using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Threading; using System.Threading.Tasks; public class GracefulShutdownHandler { private readonly IHostApplicationLifetime _lifetime; private readonly ILogger<GracefulShutdownHandler> _logger; public GracefulShutdownHandler(IHostApplicationLifetime lifetime, ILogger<GracefulShutdownHandler> logger) { _lifetime = lifetime; _logger = logger; } public async Task HandleShutdownAsync() { _logger.LogInformation("Application shutdown initiated"); // Start the shutdown process _lifetime.StopApplication(); // Wait for 30 seconds to allow for a drain window _logger.LogInformation("Waiting for 30 seconds to allow for a drain window"); await Task.Delay(TimeSpan.FromSeconds(30)); // Check if the application is still running if (_lifetime.ApplicationStopping.IsCancellationRequested) { _logger.LogInformation("Application is stopping, waiting for it to complete"); await _lifetime.ApplicationStopping.WaitAsync(); } // Log a message indicating that the shutdown is complete _logger.LogInformation("Application shutdown complete"); } } ``` To use this handler, you can register it in the `Startup.cs` file or in the `Program.cs` file, depending on your application's architecture. Here's an example of how to register it in the `Program.cs` file: ```csharp using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public class Program { public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); // Register the shutdown handler var shutdownHandler = host.Services.GetService<GracefulShutdownHandler>(); host.Services.Configure<HostOptions>(options => { options.ShutdownTimeout = TimeSpan.FromSeconds(30); }); // Start the host await host.RunAsync(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddSingleton<GracefulShutdownHandler>(); services.AddHostedService<HostedService>(); }); } public class HostedService : IHostedService { private readonly IHostApplicationLifetime _lifetime; private readonly GracefulShutdownHandler _shutdownHandler; public HostedService(IHostApplicationLifetime lifetime, GracefulShutdownHandler shutdownHandler) { _lifetime = lifetime; _shutdownHandler = shutdownHandler; } public async Task StartAsync(CancellationToken cancellationToken) { // Start the application } public async Task StopAsync(CancellationToken cancellationToken) { // Handle the shutdown await _shutdownHandler.HandleShutdownAsync(); } } ``` In this example, the `GracefulShutdownHandler` is registered as a singleton service, and the `HostedService` is used to start and stop the application. When the application is shut down, the `StopAsync` method is called, which in turn calls the `HandleShutdownAsync` method of the `GracefulShutdownHandler`. This method logs messages at each phase of the shutdown process and waits for 30 seconds to allow for a drain window.

Enhanced response (with ZPI)

```csharp using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System.Threading; using System.Threading.Tasks; public class GracefulShutdownHandler { private readonly IHostApplicationLifetime _lifetime; private readonly ILogger<GracefulShutdownHandler> _logger; public GracefulShutdownHandler(IHostApplicationLifetime lifetime, ILogger<GracefulShutdownHandler> logger) { _lifetime = lifetime; _logger = logger; } public async Task HandleShutdownAsync() { _logger.LogInformation("Starting shutdown sequence"); // Start the shutdown process _lifetime.StopApplication(); // Wait for 30 seconds to allow for draining _logger.LogInformation("Draining for 30 seconds"); await Task.Delay(TimeSpan.FromSeconds(30)); // Request cancellation of any remaining tasks _logger.LogInformation("Requesting cancellation of remaining tasks"); _lifetime.ApplicationStopping.Cancel(); // Wait for the application to stop _logger.LogInformation("Waiting for application to stop"); await _lifetime.ApplicationStopping.WaitAsync(); _logger.LogInformation("Shutdown complete"); } } ``` To register the `GracefulShutdownHandler` in the DI container, you can use the following code in the `Startup.cs` file: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IHostApplicationLifetime, HostApplicationLifetime>(); services.AddSingleton<GracefulShutdownHandler>(); services.AddHostedService<HostedService>(); } public class HostedService : IHostedService { private readonly GracefulShutdownHandler _shutdownHandler; public HostedService(GracefulShutdownHandler shutdownHandler) { _shutdownHandler = shutdownHandler; } public async Task StartAsync(CancellationToken cancellationToken) { // Start the application } public async Task StopAsync(CancellationToken cancellationToken) { await _shutdownHandler.HandleShutdownAsync(); } } ``` In this implementation, the `GracefulShutdownHandler` class uses the `IHostApplicationLifetime` interface to manage the application's lifetime. The `HandleShutdownAsync` method is called when the application is shutting down, and it logs structured messages at each phase of the shutdown process. The `ILogger<T>` interface is used for structured logging, which provides high-performance logging capabilities. The `ILogger<T>` instance is injected into the `GracefulShutdownHandler` constructor, allowing for logging messages with the category name `GracefulShutdownHandler`. The `IHostApplicationLifetime` interface provides methods for managing the application's lifetime, such as `StopApplication` and `ApplicationStopping`. The `StopApplication` method is used to start the shutdown process, and the `ApplicationStopping` property is used to wait for the application to stop. The `Task.Delay` method is used to wait for 30 seconds to allow for draining, and the `Cancel` method is used to request cancellation of any remaining tasks. Finally, the `WaitAsync` method is used to wait for the application to stop.

Grader chain

Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.

V1samba/Meta-Llama-3.3-70B-Instruct
Better
V4qwen2.5-coder-3b-instruct
Better
V5openrouter/qwen/qwen3-235b-a22b-2507
Better
V7qwen2.5-coder-7b-instruct
Better
V8openai/gpt-4.1
Worse
V9anthropic/claude-opus-4-7
Worse
V12openai/gpt-4o
Better

Comments

Our grader said what it said. What do you say? Comment as a guest below.

No comments yet. Be the first to say what you make of this round.

← back to all rounds