Skip to content

When a native exception of corrupted state is thrown, nunit test thread crashes and the nunit-console process hangs #2091

Closed

Description

@endrih

When using NUnit in C++/CLI and the state is somehow corrupted (unrecoverable errors), the nunit thread that runs the test crashes. The nunit-console thread that is waiting on the operation to complete, never exits the wait and therefore hangs the whole process.

Maybe using NUnit for C++/CLI is not supported, but we have a lot of them.

I have made a quick fix like this in NUnit.Framework.Internal.Reflect in Reflect.cs:

[HandleProcessCorruptedStateExceptions] //put here to handle native corrupt state exceptions. 
        public static object InvokeMethod( MethodInfo method, object fixture, params object[] args )
        {
            if(method != null)
            {
                try
                {
                    return method.Invoke(fixture, args);
                }
#if !PORTABLE && !NETSTANDARD1_6
                catch (System.Threading.ThreadAbortException)
                {
                    // No need to wrap or rethrow ThreadAbortException
                    return null;
                }
#endif
                catch (TargetInvocationException e)
                {
                    throw new NUnitException("Rethrown", e.InnerException);
                }
                catch (Exception e)
                {
                    throw new NUnitException("Rethrown", e);
                }
#pragma warning disable 1058
                catch
#pragma warning restore 1058
                {
                    throw new NUnitException("Rethrown", new Exception("Unknown Native exception thrown."));
                }
            }

            return null;
        }

Activity

  1. jnm2 commented on Oct 23, 2017

    @jnm2
    Contributor

    A note on catch: In C#, any non-Exception objects get wrapped and catch (Exception e) already catches everything.

    See RuntimeWrappedException on MSDN.

  2. added this to the 3.9 milestone on Oct 26, 2017
  3. jnm2 commented on Jul 2, 2019

    @jnm2
    Contributor

    Fyi: this was never fixed. The PR has never had any effect because #1775 added [assembly: AllowPartiallyTrustedCallers] and the docs say this means that [HandleProcessCorruptedStateExceptions] is ignored:

    The HandleProcessCorruptedStateExceptionsAttribute attribute is ignored when it is encountered in partially trusted or transparent code, because a trusted host should not allow an untrusted add-in to catch and ignore these serious exceptions.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions