justcoding121 / titanium-web-proxy

A cross-platform asynchronous HTTP(S) proxy server in C#.
MIT License
1.93k stars 617 forks source link

Race condition on HttpStream #798

Open valutcizen opened 4 years ago

valutcizen commented 4 years ago

Sometimes working with Open Streets Maps (OSM) on specific time (starts about 15:00 UTC+2) there is response that cause bufferLength is less than 0! Everywhere in source it is impossible until it is race condition.

My app: proxy that fixes OSM issue with endless connection. I wait for body 30 seconds and if this is not appears I ask few times again by HttpClient.

private

AtChangedPlace

bytesRead <= 0 is changed by me to prevent endless loop.

honfika commented 4 years ago

Could you please create a small test app to reproduce the issue? Parallel read shoudl not happen here, so no race condition..unless you are doing something strange.

valutcizen commented 4 years ago

The problem with small test app is that there is no problems with open street maps since 18th September, and this issue I have only on these servers, so now it is not possible to reproduce this issue.

honfika commented 4 years ago

But what did you wrote to the TWP event handler? It should not be called parallel.

valutcizen commented 4 years ago
private async Task Proxy_BeforeResponse(object sender, SessionEventArgs e)
{
    try
    {
        if (e.Exception != null || e.HttpClient.Response.StatusCode != 200)
        {
            await TryRetringAsync(e);
        }
        else
        {
            Task bodyTask = e.GetResponseBody();

            if (!bodyTask.Wait(timeout))
                await TryRetringAsync(e);
        }
    }
    catch (Exception ex)
    {
        LogToFile(ex.ToString());
    }
}

Where TryRetryingAsync is method that download body by HttpClient and changes response to new body.