Arber
Arber

Reputation: 541

Hanging connections on Mac OS

I have a network problem with Mac OS while requesting resources on my server (the problem is not constant). However, I can reproduce the problem by using JavaScript to request a random resource 1000 times sequentially.

var req = (id) => fetch('https://www.example.com/my/resource/starter.js?' + id);

// use the 'limit' as identifier
var doRecursiveRequest = (limit) => 
  req(limit).then(res => {
    if (--limit) {
      return doRecursiveRequest(limit);
    }
});

doRecursiveRequest(1000);

In the network tab, I see that the resource loads very fast. The counter for the requests counts up very quickly and suddenly it stops. After that it takes about 30 seconds and it continues until it stops again (always after a few hundred requests). After some time, all requests will be successfully completed.

By using Google Devtools (Performance Tab), I can see what the problem is, but I do not understand the cause.

At some point, there are 6 parallel network tasks running (all executed by my JavaScript). It seems that Chrome allows a maximum of 6 such microtasks in parallel. After that it takes up to 30 seconds until one is "killed" (?!) and the next one is allowed to start. However, on server side, the response times were all very quick (and all 1000 requests has arrived the server and a response was send).

enter image description here enter image description here enter image description here enter image description here

Additional Information:

Upvotes: 1

Views: 579

Answers (1)

Arber
Arber

Reputation: 541

It turns out, that the HTTP Responses with a Keep-Alive Header are causing the problem. After disabling the keep alive mechanism and adding the Content-Length Header, the issue was no longer reproducible.

This seems to be a known issue, see https://stackoverflow.com/a/25996971/9816335

Upvotes: 2

Related Questions