Intermittent bug with asyncio and MS Edge

Barry Scott barry at barrys-emacs.org
Mon Mar 23 08:05:32 EDT 2020



> On 23 Mar 2020, at 09:02, Frank Millman <frank at chagford.com> wrote:
> 
> On 2020-03-22 12:11 PM, Chris Angelico wrote:
>> On Sun, Mar 22, 2020 at 8:30 PM Frank Millman <frank at chagford.com> wrote:
>>> 
>>> On 2020-03-22 10:45 AM, Chris Angelico wrote:
>> If you can recreate the problem with a single socket and multiple
>> requests, that would be extremely helpful. I also think it's highly
>> likely that this is the case.
> 
> I am working on a stripped-down version, but I realise there are a few things I have not grasped.
> 
> Hope you don't mind, but can you scan through what follows and tell me if I am on the right lines?
> 
> Both the client and the server can send a header with 'Keep-alive', but what does it actually mean?
> 
> If the client sends it, does that mean that it wants the server to keep the connection open, and only close it when the client closes it from the other end?
> 
> Conversely, if the server sends it, does it mean that it wants the client to keep the connection open? If so, under what condition would the server close the connection (other than a timeout). Should the server only send 'Keep-alive' if it receives 'Keep-alive'?

See https://tools.ietf.org/html/rfc2616 <https://tools.ietf.org/html/rfc2616> secton 14.10 for the details.

When the browser sends "Connection: keep-alive" it mean that the browser wishes to have
a persistent connection. The server is free to choose.

If the server replies with "Connection: close" the connection is not persistent and will
be closed after the response.

If the server replies with "Connection: keep-aiive" then connect is persistent that means that
after this response the server will allow another transaction, GET, POST etc.

Note: You need to know if the client is using HTTP/1.0 or HTTP/1.1 protocol the rules are different in
each version.

> 
> In my program, when I send a file in response to a GET, I send a header with 'Keep-alive', then I send the file, then I close the connection. This now seems wrong. It could even be the cause of my bug, but then why has it only appeared now? Both Edge and Chrome send 'Keep-alive' headers.

Its a protocol violation. In your case you must send "Connection: close". Fix that and I think your code is likely to work.

If you care about performance add the extra code to handle persistent connections. Its speeds up the browsers
and should reduce the impact on the server as well.


> If I am thinking along the right lines, then the exchange should go like this -
> 
> Client sends request, with 'Keep-alive' header.
> 
> Server sends response, but does not close the connection. The server should reply with a 'Keep-alive' header. If it does not, the client will close the connection, in which case the server will also close.

You must include a "Connection:" head for HTTP/1.1 I believe.

> 
> Assuming that they both send 'Keep-alive', the onus is on the client to close the connection when it has no more requests.

Yes.

> The server should have a timeout in case the client goes away.

Yes to stop resources running out.

> 
> Assuming that the above is correct, the client will rely on 'Content-length' to determine when it has received the entire request. If the client has more than one request, it will send the first, wait for the response, when fully received as per the 'Content-length' it will send the next one, until all requests have been sent and all responses received, at which point it will close the connection.

You can use "Content-Length" header or chunked encoding.

> 
> All this assumes only one connection. Alternatively the client could open multiple connections for the requests. In that case it would make sense to use 'Connection: Close', so that the server can close the connection straight away, making it available for reuse.

Modern browsers open many persistent connection. I recall its 8/site in the case of chrome.


> 
> If this all makes sense, I should write two versions of the client program, one using a single connection, and one using a pool of connections.

Is this for a test program?

You can use curl for the single connection case.


> 
> All comments appreciated!
> 
> Frank

Barry

> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list