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

Response Code 0 during HTTP post #1392

Closed
morrisond91 opened this issue Dec 18, 2019 · 11 comments
Closed

Response Code 0 during HTTP post #1392

morrisond91 opened this issue Dec 18, 2019 · 11 comments

Comments

@morrisond91
Copy link

morrisond91 commented Dec 18, 2019

I am performing a HTTP post using IRestClient and i intermittently receive a HTTP Response Status Code of 0. Having researched this online i have identified other posts reporting this behaviour during HTTP timeout or connection being closed, however i just want to post to determine if this is a known issue, or if there is a fix available.

I am using RestSharp version: 106.6.7

var request = new RestRequest("/", Method.POST);
request.AddHeader(SigningConstants.HubSignatureHeaderName, signature);
request.AddParameter("application/json", serializedDeliveryPayload, ParameterType.RequestBody);

 var response = await _restClient.ExecutePostTaskAsync(request);

May be related to #1379

@alexeyzimarev
Copy link
Member

What's the value of the response ErrorMessage property?

@morrisond91
Copy link
Author

Sorry for the delay, our issue seems rather intermittent so it's proving difficult to reproduce, i will the code so that it logs the error message property and update here issue when i have the info.

thanks.

@alexeyzimarev
Copy link
Member

I will keep it open until Ranger closes it. When you get more info, feel free to reopen even if the issue gets closed. Also, check the docs to understand how RestSharp handles exceptions, maybe you will find out what went wrong https://restsharp.dev/usage/exceptions.html

@morrisond91
Copy link
Author

It looks like the error message is

An error occurred while sending the request. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

@alexeyzimarev
Copy link
Member

Seems like you need to check the server-side and the connection.

@GrumpyCockatiel
Copy link

I've had this issue as well the entire time I've used RestSharp. I'm on 106.10.1
It is VERY intermittent. Mine happens on the same GET request with no input and a very small amount of JSON returned. It only happens on the first attempt, after that it works fine.
I have about 6 async calls bundled together, and the 2nd one called seems to be the culprit no matter which one I put there.
When I try the exact same thing on Postman - no issues. The backend is azure func.
I'm about to roll my own.

@JSurf
Copy link

JSurf commented Mar 6, 2020

We have a similar issue wit RestSharp,
We were getting random '0' responses in our code while doing GET requests.

Seems we could solve our issues by setting header 'Connection: close'

We could finally reproduce the issue with the following code:

`
using System;
using System.Threading;
using RestSharp;
using RestSharp.Authenticators;
using System.Collections.Generic;

namespace TestRestSharp
{
class Program
{

    static void Main(string[] args)
    {
        Console.WriteLine("Start Test");
        string ip = "someserver";
        int count = 10000;
        bool close = false;
        int thCount = 5;

        List<Thread> ths = new List<Thread>();
        
        for (int threads = 0; threads < thCount; threads++)
        {
            Thread t = new Thread(() => send(threads, count, ip, https, close, wait));
            t.Start();
            Thread.Sleep(100);
            ths.Add(t);
        }
    }

    private static void send(int id, int count, string ip, bool https, bool close, int wait)
    {
        var client = new RestClient("http://" + ip);
        client.Authenticator = new HttpBasicAuthenticator("user", "passwd");
        client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;           
        
        for (int i = 0; i <= count; i++)
        {
            var request = new RestRequest("someurl");
            if(close)
                request.AddHeader("Connection", "close");
            try
            {
                var response = client.Get(request);
                Console.WriteLine(id  + " => " + i + " " + response.StatusCode);
                if (response.Content == null || response.Content.Length == 0)
                    Environment.Exit(0);
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

}
`

'someurl' returns some small json. Originally the server was a jetty behind an nginx proxy, but the same behaviour occured fetching a static file from an apache webserver

To reproduce we needed to use at least 2 threads. It consistently fails after ~ 3000 requests per thread. If you restart the test just after the error occured, the request returns '0' immediately.
Then after waiting for some time and then restarting the test, it starts working again for some ~3000 requests.

We think it is caused by poor handling of keep-alive connections in .net core.

If Connection header is set to close, the test program works fine (just set 'close' to true)

See also:
dotnet/runtime#29153
dotnet/runtime#20013
We think the client caching introduced in .net might be messed up

@alexeyzimarev
Copy link
Member

@JSurf great analysis. One issue I with HttpWebRequest for sure that it instantiates a new HttpClient every time, which prevents using the connection pooling. I will need to test it on the new RS version with HttpClient being used instead of HttpWebRequest when it's more or less done. I will also try to find a way to include your test to our rest suite.

@JSurf
Copy link

JSurf commented Mar 6, 2020

I actually think they messed up while trying overcome that limitation with a simple caching mechanism, see the second issue linked:
dotnet/runtime#20013

@stale
Copy link

stale bot commented Jun 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 4, 2020
@repo-ranger
Copy link

repo-ranger bot commented Jun 4, 2020

⚠️ This issue has been marked wontfix and will be closed in 3 days

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

No branches or pull requests

4 participants