runtime icon indicating copy to clipboard operation
runtime copied to clipboard

System.ComponentModel.EventBasedAsync.Tests threw System.Runtime.Serialization.SerializationException

Open jiangzeng opened this issue 10 years ago • 7 comments

No results generated. Details: https://mc.dot.net/#/product/netcore/master/source/official~2Fcorefx~2Fmaster~2F/type/test~2Ffunctional~2Fdesktop~2Fcli~2F/build/20161104.503/workItem/System.ComponentModel.EventBasedAsync.Tests

2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output: System.Runtime.Serialization.SerializationException: Type 'Xunit.Sdk.ThrowsException' in Assembly 'xunit.assert, Version=2.2.0.3300, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c' is not marked as serializable.
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeObject(Object obj, MemoryStream stm)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.AppDomain.Serialize(Object o)
2016-11-05 00:05:11,038: INFO: proc(55): run_and_log_output: Output:    at System.AppDomain.MarshalObject(Object o)

Build : Master - 20161104.503 (Full Framework Tests) Failing configurations: Windows.10.Amd64:AnyCPU-Debug Windows.10.Amd64:AnyCPU-Release

jiangzeng avatar Nov 07 '16 02:11 jiangzeng

After getting a repro state in my local machine and debugging I found out this is what's happening in this test fail.

  • The test BackgroundWorkerTests.TestThrowExceptionInDoWork is failing (Source code)
  • When an Assert.Throws fails on Xunit it throws a Xunit.Sdk.ThrowsException.
  • xunit runs in multiple App Domains and the Assert.Throws runs in one App Domain, since the Exception is being thrown in a different App Domain this Exception needs to be Marshaled so that it can be used in a different App Domain.
  • When Marshaling an object it needs to be Serialized, which means that it should be a Serializable object. Since Xunit.Sdk.ThrowsException is not Serializable it is throwing a System.Runtime.Serialization.SerializationException

If we run the tests using xunit.console.exe instead of using msbuild we could call it with -noappdomain as a parameter so that it runs in one App Domain and instead of getting a System.Runtime.Serialization.SerializationException we would get a test failure on that specific Assert.Throws, and we would get the expected test failure summary explaining what is the test, line and reason, instead of getting a random exception.

Why is the test failing only for Full Framework and not for .NET Core?

The test is expecting that the BackgroundWorker.DoWork delegate throws a TestException which is actually happening. The problem here is that when we are raising the event BackgroundWorker.RunWorkerCompleted we are calling our handler that is expecting a RunWorkerCompletedEventArgs object as the event parameter called e (Source code).

RunWorkerCompletedEventArgs has a property called Result which we are expecting it to throw the TestException.

Here is the issue

When accessing that Result property, if it is an Exception that should be thrown:

  • In Full Framework we throw a System.Reflection.TargetInvocationException with the expected Exception (in this case TestException) as an InnerException. The Assert.Throws is expecting a TestException but is finding a System.Reflection.TargetInvocationException.
  • In .NET Core when accessing that Result property we throw the actual expected Exception.

I Find two problems here

  1. Why do we have a different behavior in between Full Framework and .NET Core? Should we throw a System.Reflection.TargetInvocationException with the expected Exception as an InnerException in .NET Core as well?
  2. Should we make msbuild to run xunit.console.exe with argument -noappdomain to avoid this kind of exceptions on Assert failures when using xunit and so that we get the actual Assert summary and is easier to track and fix this issues?

cc @danmosemsft @weshaggard @joperezr @ericstj

safern avatar Nov 08 '16 07:11 safern

Good analysis @safern . I'm surprised that ThrowsException isn't serializable as it's so simple to do (probably just add [Serializable]) and it seems a fairly common need.

MSBuild just wraps xunit as you can see in the build log. We can figure out how to change the parameters to xunit, but if I read correctly the appdomain is being created within S.ComponentModel and not (or not only) by xunit?

danmoseley avatar Nov 08 '16 17:11 danmoseley

Sorry I had an error there. I meant xunit is the one that is creating the appdomain.

Yes we could just add that parameter Here in the test.targets file

I was surprized as well with the exception not being serialized, but the what I found out is that it is not only ThrowsException which is not serializable, all of their Assert failures are handled with exceptions and none of them are serializable. So that's why I'm suggesting to update the build tools. Even an Assert.True(false) would cause a System.Runtime.Serialization.SerializationException if xunit creates multiple appdomains.

@danmosemsft

safern avatar Nov 08 '16 17:11 safern

That seems to make XUnit's appdomain architecture (which is the default apparently) not very useful, if I understand right... I'd open an issue against them.

I suggest you update the targets to add a line like <XunitOptions Condition="'$(XUnitNoAppDomain)' == 'true'">$(XunitOptions) -noappdomain </XunitOptions>

then we can switch it off for just some tests if we want.

danmoseley avatar Nov 08 '16 18:11 danmoseley

@weshaggard is it expected to have different behaviours between .NET Core and Desktop? If not, I suppose Desktop has the right behaviour?

joperezr avatar Nov 08 '16 18:11 joperezr

There is some issues already opened against them but apparently they can't make their exceptions serializable because their assertion library is a PCL which targets platforms which do not support serialization. Issue opened agains them -- Another issue

@joperezr already spoke with @weshaggard and yes we should have the same behavior on both. Opening an issue on that.

safern avatar Nov 08 '16 18:11 safern

What isn't clear to me is whether or not all failed assertions in our desktop runs are hitting this issue. If they are then we should probably start passing the noappdomain flag but if it is only on some tests where we are doing background work that happens go go into another appdomain then I'm less inclined to start passing the noappdomain flag.

weshaggard avatar Nov 08 '16 18:11 weshaggard