Skip to content

[Commit fix] Fix thread pool starvation under multi-concurrency#2395

Merged
normj merged 3 commits into
devfrom
normj/thread-starvation
May 28, 2026
Merged

[Commit fix] Fix thread pool starvation under multi-concurrency#2395
normj merged 3 commits into
devfrom
normj/thread-starvation

Conversation

@normj
Copy link
Copy Markdown
Member

@normj normj commented May 28, 2026

Issue #, if available:
Recreation of #2387 with cleaned up commits

Description of changes:
Read description here: #2387

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

zachvan2 and others added 3 commits May 28, 2026 10:48
Parse AWS_LAMBDA_MAX_CONCURRENCY as integer to use as the default polling
task count instead of Math.Max(2, processorCount). This ensures enough
polling tasks exist to fill all available concurrency slots.

Add AdjustThreadPoolSettings() to pre-size the ThreadPool to
mcCount + processorCount at startup, preventing blocking handlers from
starving polling task continuations of threads needed to cycle back to
RAPID's /next endpoint.

Without both changes, blocking handlers (Thread.Sleep, .Result, .Wait())
exhaust the ThreadPool under multi-concurrency, causing Runtime.Unavailable
errors because polling tasks cannot resume their await continuations.

Changes:
- Utils.cs: Add GetMaxConcurrency() method, use parsed MC value in
  DetermineProcessingTaskCount (fallback to Math.Max(2, processorCount)
  for non-numeric values)
- LambdaBootstrap.cs: Add AdjustThreadPoolSettings() called after
  AdjustMemorySettings() in RunAsync startup sequence
- TestMultiConcurrencyRuntimeApiClient.cs: Make thread-safe for
  concurrent polling task access (ConcurrentDictionary, lock on Queue)
- Add tests demonstrating fix works with MC=10/20 blocking handlers
…ol tuning

Add customer-configurable environment variable AWS_LAMBDA_DOTNET_MIN_THREADS
to override the ThreadPool minimum worker thread count in multi-concurrency
mode. Default remains conservative at 2 * processorCount.

This addresses feedback that pre-creating mcCount + processorCount threads
is too aggressive. Benchmark data shows 2 * processorCount is sufficient
when combined with the polling task count fix (parsing MC value), while
the env var gives customers a knob to tune for heavy blocking workloads.

Changes:
- Constants.cs: Add ENVIRONMENT_VARIABLE_AWS_LAMBDA_DOTNET_MIN_THREADS
- LambdaBootstrap.cs: AdjustThreadPoolSettings checks env var override,
  falls back to 2 * processorCount
@normj normj requested review from a team as code owners May 28, 2026 17:50
@normj normj requested review from GarrettBeatty and philasmar May 28, 2026 17:50
@normj normj changed the title Normj/thread starvation [Commit fix] Fix thread pool starvation under multi-concurrency May 28, 2026
@normj normj merged commit 23c951c into dev May 28, 2026
5 of 6 checks passed
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.

4 participants