aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
14.96k stars 2k forks source link

BadStatusLine: 400 not expected random exceptions #6706

Open iandresolares opened 2 years ago

iandresolares commented 2 years ago

Describe the bug

Hi, I have setup a webserver which handles some POST requests. This works fine, however when the server is running for some hours I start to see some errors handling requests which were not supposed to happen: Error handling request Traceback (most recent call last): File "/usr/local/lib/python3.9/dist-packages/aiohttp/web_protocol.py", line 314, in data_received messages, upgraded, tail = self._request_parser.feed_data(data) File "aiohttp/_http_parser.pyx", line 546, in aiohttp._http_parser.HttpParser.feed_data aiohttp.http_exceptions.BadStatusLine: 400, message="Bad status line 'invalid HTTP method'"

This happens a few times every few hours and I am the only one doing requests to the webserver right now so I have control of them. I have read some issues having similar problems but I still can't find a solution. What are these requests which fail? It doesn't make my system fail or anything but it is very annoying to try to find something in the logs and just see a bunch of these exceptions and I haven't been able to wrap it in a Try Except block. Where should I place this try except block if that would be the solution?

To Reproduce

  1. Set up a webserver:
    
    app = web.Application()
    app.add_routes([web.post("/whatever", handler)])

async def create_webserver(): runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner) await site.start()

await create_webserver()

2. Run it.

### Expected behavior

To not get BadStatusLine: 400 exception when there are no expected requests.

### Logs/tracebacks

```python-traceback
Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/aiohttp/web_protocol.py", line 314, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
  File "aiohttp/_http_parser.pyx", line 546, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: 400, message="Bad status line 'invalid HTTP method'"

Python Version

Version: 3.9.10

aiohttp Version

Version: 3.7.4

multidict Version

Version: 5.2.0

yarl Version

Version: 1.7.2

OS

Ubuntu 20.04

Related component

Server

Additional context

No response

Code of Conduct

mousetail commented 2 years ago

This error happens whenever there is a request with a invalid HTTP verb. Any production server will receive many invalid requests and they shouldn't all throw errors. This buries real errors in the code. Such issues should be logged separately.

Dreamsorcerer commented 2 weeks ago

I think this is probably resolved by one of our parser fixes in recent releases. Please retest and report back.