2

I have a question regarding strange behavior of a process that performs lots of async unsuccessful requests to a server. The strange thing is that it's almost impossible to exit/stop/kill the process. If you compile and run this program on a windows 8 machine the process hangs and I haven't found a solution how to kill it. Only reboot helps.

Could somebody explain the mentioned behavior please.

Thanks.

PS: This is what I get when use taskmanager to kill the process

This is what I get when use taskmanager to kill the process

internal class Program
{
    private const int totalRequests = 2000;

    private static void Main(string[] args)
    {
        var cancellationTockenSource = new CancellationTokenSource();
        LoadTest(cancellationTockenSource);

        Console.WriteLine("Press any key to break");
        Console.ReadLine();
        cancellationTockenSource.Cancel();
    }

    private static async Task LoadTest(CancellationTokenSource cancellationTockenSource)
    {
        Stopwatch stopWatch = Stopwatch.StartNew();
        var requests = new List<Task>(totalRequests);

        for (int i = 0; i < totalRequests; ++i)
        {
            requests.Add(Request(cancellationTockenSource.Token));
        }
        try
        {
            await Task.WhenAll(requests);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        stopWatch.Stop();
        Console.WriteLine("{0} req/sec", stopWatch.Elapsed.TotalSeconds/totalRequests);
    }

    private static HttpRequestMessage CreateMessage()
    {
        var url = new Uri("http://ya.ru:8080/234234234234x");
        var message = new HttpRequestMessage(HttpMethod.Get, url);
        message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
        return message;
    }

    protected static async Task<string> Request(CancellationToken token)
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = await client.SendAsync(CreateMessage(), token);
            string content = await response.Content.ReadAsStringAsync();
            return content;
        }
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
Stas
  • 41
  • 5
  • 2
    Have you verified that the requests are actually the reason the process cannot be killed? – svick Feb 21 '13 at 18:52
  • One does not simply verify that the requests are actually the reason the process cannot be killed. – The Muffin Man Feb 21 '13 at 18:57
  • Ok, It seems that the process continues performing requests to the existent server. At least fiddler shows it. But, why can't I terminate the process and why cancellationtoken doesn't work? Because yandex doesn't listen 8080 port request/response process takes some time until connections fail. – Stas Feb 21 '13 at 19:01
  • http://stackoverflow.com/questions/14921218/why-does-cancellation-block-for-so-long-when-cancelling-a-lot-of-http-requests it's almost the same question – Stas Feb 21 '13 at 19:31
  • 2
    You likely don't have rights to kill the process, regardless of what it's doing. async requests don't magically reduce your access. – Peter Ritchie Feb 21 '13 at 20:41
  • I tried to kill a process from Admin Console using Stop-Process (powershell), taskkill, pskill(pstools from sysinternals) without any success http://screencast.com/t/ULwEqKZNOAhH – Stas Feb 21 '13 at 21:33

0 Answers0