Skip to content

Fix thread safety, null reference, and race conditions in SmartThreadPool#1

Draft
Copilot wants to merge 2 commits intomvdhorstclb-patch-1from
copilot/check-code-for-major-errors
Draft

Fix thread safety, null reference, and race conditions in SmartThreadPool#1
Copilot wants to merge 2 commits intomvdhorstclb-patch-1from
copilot/check-code-for-major-errors

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 29, 2026

Several major bugs in the SmartThreadPool codebase: missing locks on shared state, null dereferences on ThreadStatic fields, and a thread-unsafe Dispose pattern.

Changes

  • SmartThreadPool.IsWorkItemCanceled — added null guards for CurrentThreadEntry and CurrentWorkItem (both can be null outside a thread pool thread); now throws InvalidOperationException with a clear message instead of crashing
  • SmartThreadPool.ValidateWorkItemsGroupWaitForIdle — replaced re-access of CurrentThreadEntry.CurrentWorkItem with the already-captured local workItem; the property can be nulled by another thread between the null-check and the access
  • WorkItemsGroup.Concurrency setter — wrapped read-modify-write of _concurrency in lock(_lock); without it, concurrent setters could compute a wrong diff and enqueue the wrong number of work items
  • WorkItemsGroup.Start() and OnSTPIsStarting() — both read/write _isSuspended without holding _lock; concurrent calls could double-start or observe stale state
  • SynchronizedDictionary.Count — the only property in the class that skipped lock(_lock); fixed for consistency and correctness
  • WorkItemsQueue.Dispose() — moved _isDisposed = true inside the lock and after Cleanup(), preventing concurrent callers from both entering Cleanup()
// Before: NullReferenceException if called from a non-pool thread
public static bool IsWorkItemCanceled => CurrentThreadEntry.CurrentWorkItem.IsCanceled;

// After: clear contract enforcement
public static bool IsWorkItemCanceled
{
    get
    {
        if (CurrentThreadEntry == null || CurrentThreadEntry.CurrentWorkItem == null)
            throw new InvalidOperationException(
                "IsWorkItemCanceled can only be called from a work item executing in the SmartThreadPool");
        return CurrentThreadEntry.CurrentWorkItem.IsCanceled;
    }
}
Original prompt

Controleer de code op grote fouten


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Check code for major errors Fix thread safety, null reference, and race conditions in SmartThreadPool Mar 29, 2026
Copilot AI requested a review from mvdhorstclb March 29, 2026 22:13
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.

2 participants