Skip to content

Microsoft.Owin.Testing.OwinClientHandler: Sometimes the offload task never runs, resulting in hanging tests. #566

@kalbert312

Description

@kalbert312

My team is utilizing this [email protected] to run an in-memory test server for our E2E tests. While debugging tests, I've observed that most of the time, tests that utilize the server hang and the requests are never received by the web controllers. Debugging led me to OwinClientHandler where I saw:

            state.OwinContext.Request.Body = body;
            CancellationTokenRegistration registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            Task offload = Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        await _next(state.Environment);
                        state.CompleteResponse();
                    }
                    catch (Exception ex)
                    {
                        state.Abort(ex);
                    }
                    finally
                    {
                        registration.Dispose();
                        state.Dispose();
                    }
                });

            return await state.ResponseTask;

When the test hangs, the breakpoint set at await _next(state.Environment); is never hit. When it does not hang, the breakpoint is hit, and then subsequently the web controller breakpoint is hit. I'm not familiar with Task.Factory.StartNew but it seems like there might be a bug here based on some quick google searches. There's mentions that StartNew does not understand async delegates, and that a call to Unwrap() is necessary, or use Task.Run instead. https://sergeyteplyakov.github.io/Blog/async/2019/05/21/The-Dangers-of-Task.Factory.StartNew.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions