Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sockets end in close_wait state after some time #20

Open
GopinathSadasivam opened this issue Jun 7, 2019 · 1 comment
Open

sockets end in close_wait state after some time #20

GopinathSadasivam opened this issue Jun 7, 2019 · 1 comment

Comments

@GopinathSadasivam
Copy link

GopinathSadasivam commented Jun 7, 2019

Looks like we are missing a time-out on stream reader in uhttpsharp\HttpClient.cs

When remote http client gets disconnected due to some network issues (or because of proxies in between not handling the keep-alive header properly), uHttpSharp library code fails to close the socket, this results in open sockets stuck in close_wait state.
Am new to C# not sure how to add this time out.

 {
        try
        {
            await InitializeStream();

            while (_client.Connected)
            {
                // TODO : Configuration.
                var limitedStream = new NotFlushingStream(new LimitedStream(_stream));

                var request = await _requestProvider.Provide(new MyStreamReader(limitedStream)).ConfigureAwait(false);

                if (request != null)
                {
                    UpdateLastOperationTime();

                    var context = new HttpContext(request, _client.RemoteEndPoint);

                    Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint);


                    await _requestHandler(context).ConfigureAwait(false);

                    if (context.Response != null)
                    {
                        var streamWriter = new StreamWriter(limitedStream) { AutoFlush = false };
                        streamWriter.NewLine = "\r\n";
                        await WriteResponse(context, streamWriter).ConfigureAwait(false);

                         //v----- connections seem to be stuck here, resulting in piling up of  sockets in close_wait state
                        await limitedStream.ExplicitFlushAsync().ConfigureAwait(false); 

                        if (!request.Headers.KeepAliveConnection() || context.Response.CloseConnection)
                        {
                            _client.Close();
                        }
                    }

                    UpdateLastOperationTime();
                }
                else
                {
                    _client.Close();
                }
            }
        }
        catch (Exception e)
        {
            // Hate people who make bad calls.
            Logger.WarnException(string.Format("Error while serving : {0}", _remoteEndPoint), e);
            _client.Close();
        }

        Logger.InfoFormat("Lost Client {0}", _remoteEndPoint);
    }
@fbaghdadlian-b
Copy link

Hi
I have the same problem with sockets end in close_wait state in other projects,
Did you find solution for this one in this project ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants