dotnet / dotnet/winforms

Disabled tests: WindowsFormsSynchronizationContext

Open
#3,297 8 comments 0 reactions 0 assignees View on GitHub
test-bug
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

**.NET Core Version:**
master branch

**Have you experienced this same bug with .NET Framework?:**
no

**Problem description:**

Following three tests are blocking the update of xunit.stafact (#3122) and will be skipped by that PR unless they get fixed before the package update PR gets merged.
* WindowsFormsSynchronizationContext_Dispose_MultipleTimes_Success
* WindowsFormsSynchronizationContext_Send_InvokeDisposed_Nop
* WindowsFormsSynchronizationContext_Post_InvokeDisposed_Nop

They are a WinFormsFact/Theory and dispose the SynchronizationContext early. This leads to either crashing the whole process in debug builds due to a failed assert, or failing tests in release builds. Reason is that WinFormsFact/Theory after the package update will pump messages via DoEvents, calling this after the SC gets disposed will lead to an exception (or crash in debug build).

(There may also be the question if this being an assert is the correct choice, maybe its worth checking explicitly for the current thread being a disposed thread, to give a useful exception instead. Or maybe its time to get rid of the public Dispose on WindowsFormsSynchronizationContext, it feels inappropriate since the WFSC is not the owner of the underlying infrastructure, there can be any number of WFSC and disposing one is terminating the infrastructure for all of them, thats not how the disposable pattern is supposed to work.)

The error gets blamed onto stafact infrastructure, but the tests are causing it by disposing early. Failed CI build can be found here: https://github.com/dotnet/winforms/runs/681811749

Debug build:
```
Unhandled exception. System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\Control.cs:line 6896
at System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args) in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\Control.cs:line 4707
at System.Windows.Forms.WindowsFormsSynchronizationContext.Post(SendOrPostCallback d, Object state) in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\WindowsFormsSynchronizationContext.cs:line 90
at Xunit.Sdk.Utilities.SyncContextAwaiter.OnCompleted(Action continuation)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter](TAwaiter& awaiter, IAsyncStateMachineBox box)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
System.Windows.Forms.Tests.ApplicationTests.Application_EnableVisualStyles_InvokeAfterGettingRenderWithVisualStyles_Success [SKIP]
Crash with AbandonedMutexException. See: https://github.com/dotnet/arcade/issues/5325
Process terminated. Assertion Failed
Marshaling control should have created its handle in its ctor.
at System.Windows.Forms.WindowsFormsSynchronizationContext..ctor() in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\WindowsFormsSynchronizationContext.cs:line 35
at System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfNeeded() in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\WindowsFormsSynchronizationContext.cs:line 143
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context) in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\Application.ThreadContext.cs:line 1119
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context) in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\Application.ThreadContext.cs:line 992
at System.Windows.Forms.Application.DoEvents() in F:\workspace\_work\1\s\src\System.Windows.Forms\src\System\Windows\Forms\Application.cs:line 811
at Xunit.Sdk.WinFormsSynchronizationContextAdapter.PumpTill(SynchronizationContext synchronizationContext, Task task)
at Xunit.Sdk.ThreadRental.<>c__DisplayClass11_0.b__0()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
```

Release build:
```
Unhandled exception. System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) in /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs:line 6896
at System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args) in /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs:line 4706
at System.Windows.Forms.WindowsFormsSynchronizationContext.Post(SendOrPostCallback d, Object state) in /_/src/System.Windows.Forms/src/System/Windows/Forms/WindowsFormsSynchronizationContext.cs:line 91
at Xunit.Sdk.Utilities.SyncContextAwaiter.OnCompleted(Action continuation)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter](TAwaiter& awaiter, IAsyncStateMachineBox box)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
```

**Expected behavior:**
Tests using WinFormsFact/Theory should not dispose the infrastructure they are running on

**Minimal repro:**
Execute one of those tests after updating xunit.stafact to `1.0.33-beta` or newer

/cc: @hughbe @RussKie

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.