From d3492e4b1fca784640b0a00892d92f1d5c6b0240 Mon Sep 17 00:00:00 2001 From: Jonas Ha Date: Thu, 11 Dec 2025 08:51:02 -0500 Subject: [PATCH 1/4] fix(lambda-host): reduce default init timeout to 500ms - Adjust `InitTimeout` default from 5 seconds to 500ms for Lambda initialization. - Ensures faster failure handling and improved responsiveness. --- src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs b/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs index 953c05cc..fbb1a7e5 100644 --- a/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs +++ b/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs @@ -47,9 +47,9 @@ public class LambdaHostOptions /// Gets or sets the timeout duration for handlers. /// /// This value is used as the duration of the that is - /// passed to handlers. Default is 5 seconds. + /// passed to handlers. Default is 500 milliseconds. /// - public TimeSpan InitTimeout { get; set; } = TimeSpan.FromSeconds(5); + public TimeSpan InitTimeout { get; set; } = TimeSpan.FromMilliseconds(500); /// /// Gets or sets the buffer duration subtracted from the Lambda function's remaining From af0c6e2b5fb91db37d2ad80b9ba190ed2a8e4dc2 Mon Sep 17 00:00:00 2001 From: Jonas Ha Date: Thu, 11 Dec 2025 08:51:48 -0500 Subject: [PATCH 2/4] fix(lambda-host-options): update default cancellation buffer to 500ms - Changed `InvocationCancellationBuffer` default from 3 seconds to 500ms. - Improves cancellation timing precision for Lambda function invocations. --- src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs b/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs index fbb1a7e5..de7a02bd 100644 --- a/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs +++ b/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs @@ -55,8 +55,8 @@ public class LambdaHostOptions /// Gets or sets the buffer duration subtracted from the Lambda function's remaining /// invocation time when creating cancellation tokens. /// - /// Default is 3 seconds. - public TimeSpan InvocationCancellationBuffer { get; set; } = TimeSpan.FromSeconds(3); + /// Default is 500 milliseconds. + public TimeSpan InvocationCancellationBuffer { get; set; } = TimeSpan.FromMilliseconds(500); /// /// Gets or sets the duration between when AWS sends SIGTERM and SIGKILL to the Lambda From ec333a4245ac0b3a433f9868d03a924ead5d359d Mon Sep 17 00:00:00 2001 From: Jonas Ha Date: Thu, 11 Dec 2025 09:09:04 -0500 Subject: [PATCH 3/4] fix(lambda-host-options): update default init timeout to 5 seconds - Changed `InitTimeout` default from 500ms to 5 seconds for `ILambdaOnInitBuilder.OnInit` handlers. - Provides more time for initialization tasks to complete successfully. --- src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs b/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs index de7a02bd..c846a0ae 100644 --- a/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs +++ b/src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs @@ -47,9 +47,9 @@ public class LambdaHostOptions /// Gets or sets the timeout duration for handlers. /// /// This value is used as the duration of the that is - /// passed to handlers. Default is 500 milliseconds. + /// passed to handlers. Default is 5 seconds. /// - public TimeSpan InitTimeout { get; set; } = TimeSpan.FromMilliseconds(500); + public TimeSpan InitTimeout { get; set; } = TimeSpan.FromSeconds(5); /// /// Gets or sets the buffer duration subtracted from the Lambda function's remaining From 89f723a4645c01eb8c26776d8bdad0c0a38e7c6c Mon Sep 17 00:00:00 2001 From: Jonas Ha Date: Thu, 11 Dec 2025 09:17:45 -0500 Subject: [PATCH 4/4] fix(docs): update default values and improve formatting in guides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated `InvocationCancellationBuffer` default value to 500ms in documentation. - Adjusted tables in dependency-injection and configuration guides for improved readability. - Standardized time unit formatting across tables (e.g., `500 ms` to `500ms`). --- docs/guides/configuration.md | 24 ++++++++++++------------ docs/guides/dependency-injection.md | 12 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/guides/configuration.md b/docs/guides/configuration.md index fc5f82e9..8b9c23dd 100644 --- a/docs/guides/configuration.md +++ b/docs/guides/configuration.md @@ -79,15 +79,15 @@ builder.Services.ConfigureLambdaHostOptions(options => }); ``` -| Option | Type | Default | Description | -|--------------------------------|--------------------------|------------------------------------|-----------------------------------------------------------------------------| -| `InitTimeout` | `TimeSpan` | 5 seconds | Maximum time all `OnInit` handlers collectively have before cancellation. | -| `InvocationCancellationBuffer` | `TimeSpan` | 3 seconds | Buffer subtracted from remaining time before the invocation token fires. | -| `ShutdownDuration` | `TimeSpan` | `ShutdownDuration.ExternalExtensions` (500 ms) | Expected window between SIGTERM and SIGKILL. | -| `ShutdownDurationBuffer` | `TimeSpan` | 50 ms | Safety margin deducted from `ShutdownDuration` to guarantee cleanup. | -| `ClearLambdaOutputFormatting` | `bool` | `false` | Automatically register the built-in OnInit handler that clears console formatting. | -| `BootstrapHttpClient` | `HttpClient?` | `null` | Custom client for the Lambda bootstrap runtime API. | -| `BootstrapOptions` | `LambdaBootstrapOptions` | `new()` | Low-level runtime bootstrap configuration (timeouts, heartbeats, etc.). | +| Option | Type | Default | Description | +|--------------------------------|--------------------------|-----------------------------------------------|------------------------------------------------------------------------------------| +| `InitTimeout` | `TimeSpan` | 5 seconds | Maximum time all `OnInit` handlers collectively have before cancellation. | +| `InvocationCancellationBuffer` | `TimeSpan` | 500 milliseconds | Buffer subtracted from remaining time before the invocation token fires. | +| `ShutdownDuration` | `TimeSpan` | `ShutdownDuration.ExternalExtensions` (500ms) | Expected window between SIGTERM and SIGKILL. | +| `ShutdownDurationBuffer` | `TimeSpan` | 50ms | Safety margin deducted from `ShutdownDuration` to guarantee cleanup. | +| `ClearLambdaOutputFormatting` | `bool` | `false` | Automatically register the built-in OnInit handler that clears console formatting. | +| `BootstrapHttpClient` | `HttpClient?` | `null` | Custom client for the Lambda bootstrap runtime API. | +| `BootstrapOptions` | `LambdaBootstrapOptions` | `new()` | Low-level runtime bootstrap configuration (timeouts, heartbeats, etc.). | ### `InitTimeout` @@ -142,9 +142,9 @@ lambda.OnShutdown(async (ITelemetry telemetry, CancellationToken ct) => Choose from the provided constants when possible: -- `ShutdownDuration.NoExtensions` – No extensions installed (0 ms window). -- `ShutdownDuration.InternalExtensions` – Only internal extensions (300 ms). -- `ShutdownDuration.ExternalExtensions` – External extension installed (500 ms, default). +- `ShutdownDuration.NoExtensions` – No extensions installed (0ms window). +- `ShutdownDuration.InternalExtensions` – Only internal extensions (300ms). +- `ShutdownDuration.ExternalExtensions` – External extension installed (500ms, default). - Any custom `TimeSpan` when your environment requires more/less time. ### `ClearLambdaOutputFormatting` diff --git a/docs/guides/dependency-injection.md b/docs/guides/dependency-injection.md index 952d9144..726a75d6 100644 --- a/docs/guides/dependency-injection.md +++ b/docs/guides/dependency-injection.md @@ -70,7 +70,7 @@ If your handler doesn't need the Lambda payload, omit the `[Event]` parameter en !!! tip "Cancellation buffers" The cancellation token fires slightly **before** AWS kills the process: - - The runtime subtracts `LambdaHostOptions.InvocationCancellationBuffer` (default 3s) from the + - The runtime subtracts `LambdaHostOptions.InvocationCancellationBuffer` (default 500ms) from the remaining time when creating the token. - Always pass it down to outbound SDK calls and database queries so you can stop work cleanly. @@ -131,12 +131,12 @@ lambda.MapHandler(( ## Host-Specific Pitfalls -| Pitfall | Impact | Fix | -|---------|--------|-----| +| Pitfall | Impact | Fix | +|---------------------------------------|-----------------------------------------------------|-----------------------------------------------------------------------------------------| | Singleton depends on a scoped service | Scoped instance from first invocation leaks forever | Inject `IServiceProvider`, create a scope, resolve the scoped service inside the method | -| Storing scoped services in singletons | `ObjectDisposedException` on later invocations | Keep scoped dependencies scoped; pass data instead of services | -| Over-injecting handlers | Hard-to-test functions with 8+ services | Move orchestration into services; keep handlers thin | -| Forgetting cancellation tokens | Lambda kills the environment mid-work | Always inject `CancellationToken` and pass it down | +| Storing scoped services in singletons | `ObjectDisposedException` on later invocations | Keep scoped dependencies scoped; pass data instead of services | +| Over-injecting handlers | Hard-to-test functions with 8+ services | Move orchestration into services; keep handlers thin | +| Forgetting cancellation tokens | Lambda kills the environment mid-work | Always inject `CancellationToken` and pass it down | ## Key Takeaways