Intermittent bug with asyncio and MS Edge

Barry Scott barry at barrys-emacs.org
Sat Mar 21 14:04:11 EDT 2020



> On 21 Mar 2020, at 13:43, Frank Millman <frank at chagford.com> wrote:
> 
> Hi all
> 
> I have a strange intermittent bug.
> 
> The role-players -
>    asyncio on Python 3.8 running on Windows 10
>    Microsoft Edge running as a browser on the same machine
> 
> The bug does not occur with Python 3.7.
> It does not occur with Chrome or Firefox.
> It does not occur when MS Edge connects to another host on the network, running the same Python program (Python 3.8 on Fedora 31).
> 
> The symptoms -
>    On receiving a connection, I send an HTML page to the browser,
>        which has 20 lines like this -
> 
>    <script type="text/javascript" src="src/main.js"></script>
>    <script type="text/javascript" src="src/on_load.js"></script>
>    ...
> 
> Intermittently, one or other of the script files is not received by MS Edge.
> 
> I have checked the Network tab in Developer Tools in MS Edge. It shows the first few requests getting a Status 200 OK, then some are shown as 'Pending'. This seems to be where the problem occurs.
> 
> I am hoping that someone can give me some hints about how to debug this.
> 
> My function to send the script file looks like this -
> 
>    async def send_file(writer, fname):
> 
>        status = 'HTTP/1.1 200 OK\r\n'
>        writer.write(status.encode())
> 
>        headers = []
>        headers.append(('CONNECTION', 'keep-alive'))
>        headers.append(('DATE',
>            email.utils.formatdate(usegmt=True)))
>        headers.append(('SERVER',
>           f'Python {sys.version.split()[0]} asyncio'))
>        headers.append(('Content-type', 'text/javascript'))
>        headers('Transfer-Encoding', 'chunked'))
>        for key, val in headers:
>            writer.write(f'{key}: {val}\r\n'.encode())
>        writer.write('\r\n'.encode())
>        await writer.drain()
> 
>        with open(fname 'rb') as fd:
>            chunk = fd.read(8192)
>            while chunk:
>                writer.write(hex(len(chunk))[2:].encode() + b'\r\n')
>                writer.write(chunk + b'\r\n')
>                await writer.drain()
>                chunk = fd.read(8192)
>            writer.write(b'0\r\n\r\n')
>            await writer.drain()
> 
>        writer.close()
>        await writer.wait_closed()
> 
> I have asked the same question on StackOverflow, from an MS Edge perspective -
> https://stackoverflow.com/questions/60785767/ms-edge-randomly-does-not-load-script
> 
> I don't know whether the problem lies with Python or MS Edge, but as it does not happen with Python 3.7, I am suspecting that something changed in 3.8 which does not match MS Edge's expectations.

I'd look at the network  traffic with wireshark to see if there is anything different between edge and the other browsers.

Aside: headers are case blind, but usually written capitalised. Some buggy software expects that its capitalised.

I wonder if it will start working if you do not do chunked (your chunked encoding looks correct).
You can use os.stat to find the size of the file for the Content-Length header.

Barry



> 
> Any hints much appreciated.
> 
> Frank Millman
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list