question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle exception when receive request with custom method

See original GitHub issue

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

I’m developing fastapi application which is using starlette and run with uvicorn.

Currently I developing locally on my PC which I’ve done for a long time, so I frequently get some external random requests from the internet from random IPs.

While it is fine if I get some request that lead to some php endpoint and server log it and show nice 404 error in the logs, sometimes I get weird custom requests that break uvicorn and it throw errors, it look like this in the console: image

The main issue in /uvicorn/protocols/http/httptools_impl.py: https://github.com/encode/uvicorn/blob/a9ea657339e64c2236f3fb49616e3e0a133273c1/uvicorn/protocols/http/httptools_impl.py#L131-L138

Steps to reproduce the bug

  • Use curl to make custom request, the custom method is -X flag where you in theory should use GET/POST/… but instead in my case I get some weird methods, that make uvicorn to throw exception
curl -X MUX -i -H "Custom-Header: Test" http://127.0.0.1/

Expected behavior

I should get minimal and useful log, this may be need to pass on underlying libraries (to handle it there) or being able to control behavior from them. Right now when you get this as it shows you don’t understand what is happening, who is sending to you (IP) and what (what method?).

Actual behavior

Logs get full of this exception spam, it essentially work as DDoS for a server. I have to patch library manually to be able to fix this.

Debugging material

I attach screenshot above, there is also example from one request:

WARNING:  Invalid HTTP request received.
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 131, in data_received
    self.parser.feed_data(data)
  File "httptools/parser/parser.pyx", line 212, in httptools.parser.parser.HttpParser.feed_data
httptools.parser.errors.HttpParserInvalidMethodError: Invalid method encountered

First line is come, probably from fastapi and other are exception.

Environment

  • Windows, WSL, Docker
  • Running uvicorn 0.15.0 with CPython 3.8.6 on Windows
  • Running fastapi application like that: uvicorn main:app --host 0.0.0.0 --port 80

Additional context

This is similar issues:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
euri10commented, Jul 11, 2022

yep, you’ll just see WARNING: Invalid HTTP request received. and not the full traceback anymore, there’s no functional change

1reaction
michealrobertscommented, Aug 9, 2022

@Kludex I feel like there is an issue, because this basically stops the server from reporting a healthy status to the kubernetes pod health checker, as it is causing it to reload and then shutdown after 5-10 invalid HTTP requests. The issue doesn’t happen on local, and those get requests are coming from the liveness probe. It’s definitely a uvicorn issue, because I have had multiple different servers running on kubernetes but uvicorn has so far been very problematic. Happy to be educated…of course!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Error Message Handling for REST API - Baeldung
Exception Handling for a REST API - illustrate the new Spring recommended ... A Custom Error Message ... Request method: GET Request path: ......
Read more >
Get Started with Custom Error Handling in Spring Boot (Java)
This approach involves defining a custom exception carrying the HTTP status to use, and all the data required to describe the error that ......
Read more >
How to manage exceptions thrown in filters in Spring?
First, you need to make sure, that you have a class that handles exceptions occurring in a regular RestController/Controller (a class annotated with...
Read more >
Handling Exceptions Returned from the Web API
case statement to the function. The first case you add is 500. You'll add more cases later in this article. When a 500...
Read more >
Guide to Spring Boot REST API Error Handling - Toptal
Altogether, the most common implementation is to use @ExceptionHandler on methods of @ControllerAdvice classes so that the Spring Boot exception handling will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found