Skip to content

Fix NullReferenceExceptions caused by WorkItemQueue not being thread-safe #2428

Closed
@ChrisMaddock

Description

@ChrisMaddock

NUnit 3.8 started throwing various null reference exceptions e.g.

 Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
    at NUnit.Framework.Internal.Execution.WorkItemQueue.Dequeue()
    at NUnit.Framework.Internal.Execution.TestWorker.TestWorkerThreadProc()
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at **System.Threading.ThreadHelper.ThreadStart()**
at NUnit.Framework.Internal.Execution.WorkItemQueue.get_IsEmpty()
at NUnit.Framework.Internal.Execution.WorkShift.get_HasWork()
at NUnit.Framework.Internal.Execution.ParallelWorkItemDispatcher.StartNextShift()
at NUnit.Framework.Internal.Execution.ParallelWorkItemDispatcher.OnEndOfShift(Object sender, EventArgs ea)
at NUnit.Framework.Internal.Execution.WorkShift.<StartWorkers>b__26_1(TestWorker s, WorkItem ea)
at NUnit.Framework.Internal.Execution.TestWorker.TestWorkerThreadProc()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

@garuma diagnosed the below

This seems to come from the queue array having null in it. In turn, I believe this is due to InitializeQueues being not concurrent-safe, creating an array and initializing it directly on shared state.

In #2417 @garuma is proposing a general refactor of this section of code. @CharliePoole suggested that we should put in a bare minimum fix as a hotfix, and do the refactor separately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions