Embedio: Responses with KeepAlive = false take unreasonably long

Created on 9 Apr 2019  路  8Comments  路  Source: unosquare/embedio

Describe the bug
I'm trying a bit around with EmbedIO and I realized that the first connection always takes really long until it gets closed after a response has been sent.
Usually this is not that much noticable when KeepAlive is set to true, since only the first request takes long and subsequent request are fast.
However I wanted to write a custom api middleware which closes the socket after each request, so I set KeepAlive is set to false, now every request takes really long.

To Reproduce
I have this minimal test setup

using System;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Constants;

class EmbedIOTest
{
    static void Main(string[] args)
    {
        using (var ws = new WebServer(9696))
        {
            ws.RegisterModule(new TestModule());
            ws.RunAsync();
            Console.ReadKey();
        }
    }
}

class TestModule : WebModuleBase
{
    public override string Name => "TestModule";
    public TestModule()
    {
        AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, (ctx, ct) =>
        {
            ctx.Response.KeepAlive = false;
            return ctx.StringResponseAsync("test");
        });
    }
}

The responses seem to differ on clients:

  • Firefox (66.0.2 (64-bit)) has no problems at all
  • Chrome (73.0.3683.103 (Official Build) (64-bit)) takes >300ms on the first request (https://i.imgur.com/SUzGkAw.png). The Waterfall just shows an unexplained empty gap: https://i.imgur.com/ZTxy1hO.png
  • Postman (7.0.7) consistently takes >500ms on each request: https://i.imgur.com/5Ai1ta4.png
  • (untestet) I've head from some of my users that this happens on php too

I'm running everything on localhost.
And I recommend using postman, as it shows the problem the most clear.

Expected behavior
The connection should be correctly closed.

Desktop:

  • Using Lastest EmbedIO nuget (2.3.1)
  • Tested on net4.6 and net4.7.2
bug

All 8 comments

I created the branch Issue267-KeepAlive

Seems like upgrading to 2.5.0 has fixed it, thanks.

Well, I have to reopen the issue because I have found the problem. Seems like I messed up my testsetup (I always referenced the ns1.3 dll).
The problem occours with the netstandard1.3 build of embedio.

This preprocessor directive is here to be because standard1.3 streams do not have a close method.
https://github.com/unosquare/embedio/blob/cc0d80cc049bbb71553ca182c734ce01bede6be0/src/Unosquare.Labs.EmbedIO/System.Net/ResponseStream.cs#L45-L49

The problem is that the Close method in the netstandard1.3 never gets called (Per visual studio 'find references' and manual checking)

My best suggestion would be to change the close to:

#if !NETSTANDARD1_3
    public override void Close()
    {
        Dispose();
    }
#endif

    protected override void Dispose(bool disposing)
    {
        if (_disposed) return;
        _disposed = true;
        if (!disposing) return;
        var ms = GetHeaders()
        // ...

Let me check!

@Splamy I did some changes, can you check them? Or do you need a nuget?

I tested master and it works now, thanks.

I just published a new nuget: https://www.nuget.org/packages/EmbedIO/2.7.2

Let me know if I can close this issue.

Works like a charm. Thanks again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixerdy picture felixerdy  路  7Comments

shdwp picture shdwp  路  6Comments

Dewyer picture Dewyer  路  7Comments

bufferUnderrun picture bufferUnderrun  路  4Comments

ST-Emanuel picture ST-Emanuel  路  3Comments