Http request fails with 400 data after connection close

Hi all, I know I’m am often the one doing the helping out on here but I have a very wierd issue I am struggling to resolve. I know @bdraco has done work on aiohttp and wondered if he can help me with this.

Basic Background

The Wiser integration (available in HACs) communicates over a simple rest api with a hardware hub, that in turn controlls the boiler and many accessories over zigbee.

Until the update of aiohttp to 3.9.1 in HA, this worked really well and was very stable.

Since then, no end of issues with http communication. So why am I not logging an issue with aiohttp? As below.

What is Happening

Originally with aiohttp v3.8, we used http/1.1 protocol and the hub would normally respond with a chuncked encoded response with no issues.

With aiohttp 3.9.1 and above, using http/1.1, we would frequently get the below (but very inconsistantly).

Response error trying to communicate with Wiser Hub 192.168.x.x for url http://192.168.x.x:80/data/v2/schedules/.  Error is 400, message="Data after `Connection: close`:\n\n  b'0'\n     ^", url=URL('http://192.168.200.36:80/data/v2/schedules/').

Using wireshark, I can see that the hub sends a Continuation message after the last chunk and this is what is being complained about. However, it does this for every request, the firmware on the hub has not changed from the time of aiohttp3.8 and the error only happens sometimes.

Still a aiohttp issue?

Ok, so, if I run the same update routine (using our same api as used in the Wiser integration) outside of HA , this never fails.

So, yes this may still be an aiohttp issue but I am scratching my head as how to diagnose this and find a solution. What is it about running it under HA that causes it to behave differently (and inconsistantly differently).

ATM, we are having to detect this error and retry the request on http/1.0, which mainly works. However, if we use http/1.0 all the time, the hub sometimes still sends a chunked response which gives other issues.

Links to code

Integration

API

Wireshark Screenshots

From running direct in console test script

Running under HA - I have shown 2 requests here as the first was successful, the second returned a 400 error, but they look exactly the same to me.

Thanks for any help available.

Its likely the llhttp update that aiohttp did between those versions that is now causing it to reject. The parser is much stricter about RFC conformance in newer versions.

Thanks for replying. Yes I was originally thinking that but it is the inconsistancy that is causing the head scratching. Why would lots of requests work and then one not and resubmitting that request then works. And why would it work outside of HA without issue?

It sounds like something might be blocking the event loop when you’re running inside of HA which changes the timing