Skip to content

Fix thread pool worker accounting drift in ThreadPoolExecutor#6

Draft
BoBiene wants to merge 1 commit into
mainfrom
codex/fix-thread-pool-problems
Draft

Fix thread pool worker accounting drift in ThreadPoolExecutor#6
BoBiene wants to merge 1 commit into
mainfrom
codex/fix-thread-pool-problems

Conversation

@BoBiene
Copy link
Copy Markdown
Owner

@BoBiene BoBiene commented Feb 6, 2026

Motivation

  • Prevent _freeThreads counter from drifting under contention by ensuring it is only adjusted while a worker is actually waiting.
  • Avoid desynchronization between scheduling and worker state updates that could cause incorrect pool sizing decisions.
  • Ensure thread lifecycle bookkeeping remains consistent by cleaning up _runningThreads and _pool when a worker exits.

Description

  • Removed the eager decrement of _freeThreads in InternalExecute so the free-thread count is not manually manipulated when enqueuing tasks.
  • Rewrote RunPoolThread wait loop so a worker increments _freeThreads only while inside Monitor.Wait and always decrements it in a finally block, and the worker dequeues and runs tasks after waking.
  • Added an outer try/finally in RunPoolThread to decrement _runningThreads and remove the finished thread from _pool when a worker terminates, preserving consistent pool state.
  • Preserved the existing suppression of unexpected exceptions inside the worker loop to avoid breaking shutdown semantics.

Testing

  • Attempted to run dotnet test SharpCifs.SMBv1.sln, but the test run failed in this environment because dotnet is not installed.
  • No other automated tests were executed in this environment due to the missing runtime.

Codex Task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant