New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle exception when receive request with custom method #1296
Comments
thanks @XCanG , I can also reproduce with uvicorn[standard]==0.16.0 |
not sure we should handle this differently, the request cant be parsed, open to ideas though, I kind of remember @florimondmanca made this change to try to pinpoint a long-standing nginx issue but I may be remember badly. |
I'm getting the same issue on Chango Channels
|
What at least should be done is better writing exception message, because without it, it looks confusing. After that you may decide another approach. I did some monkey-patch by including protocol information in exception: try:
self.parser.feed_data(data)
except httptools.HttpParserError as exc:
msg = f"Invalid HTTP request received. | {data}"
self.logger.warning(msg, exc_info=exc)
self.transport.close()
except httptools.HttpParserUpgrade:
self.handle_upgrade() Adding that |
Needs to be resolved in both |
I personally would just figure out a way to allow users to control logging of the traceback, or remove the traceback completely and replace it with a more descriptive but concise one-line error about what's going on. Edit: Another thought is to pass the error through to the application in some way, so that we can provide real http responses about it in our ASGI applications. |
ok I digged a little and the traceback was introduced in #886 as part of trying to solve #344 to get a better sense of what was happening. Now with #1263 merged, if that solves #344 (still need confirmation) then the change is obsolete I think we could revert #886, should we do that you'd have this log, would that be ok ?
|
@euri10 That would be better, but can you also put info about what was requested? Like the type of request. |
interestingly gunicorn returns a 200 !
|
It depends. Not all instances of this error return a 200. For |
I am also seeing this issue with 0.17.5. Is there a fix or workaround for this issue? I see a lot of recommendations and I have attempted many of them, but I still see the |
what is the best way to alleviate this issue right now? this isn't a critical issue right? |
We still need to do something on But I'm not against removing the whole trace there. 👍 |
@euri10 and @Kludex, With the revert I still see the Warnings, which is expected based on the code change. Is there something we should be doing in my deployment of Uvicorn to correct for this warning? Or is this just a Warning we should expect when we get requests with invalid methods? We are using FastAPI with Uvicorn. |
I guess the answer would be: "this is just a warning you should expect when you get requests with invalid method". |
yep, you'll just see |
So the latest fixes are not working imho. I have the same issue whereby I am deploying a Fast API application to a kubernetes cluster with uvicorn as the server of choice, and I am seeing the following issue:
Basically, a number of requests are being reported as "Invalid" - then the server process is shutting down and the pod enters into its |
We might need to add more information on that message, but besides that, there's no issue here... Can you track down the endpoint and the call? |
@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! |
The issue doesn't happen locally because you're not making the same call as the one being made in production... Again, can you please share the endpoint code and the call that is being made? |
Got the same error calling without method import json
import random
import socket
import time
import uuid
HOST = "localhost" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
NUM_MESSAGES = 3
if __name__ == "__main__":
msgs = []
for _ in range(NUM_MESSAGES):
msgs.append(
{
"timestamp": int(time.time()) + random.randint(1, 10),
"msg": str(uuid.uuid4()),
}
)
msg_str = json.dumps(msgs)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(bytes(msg_str, encoding="utf-8")) |
Checklist
master
.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:
The main issue in
/uvicorn/protocols/http/httptools_impl.py
:uvicorn/uvicorn/protocols/http/httptools_impl.py
Lines 131 to 138 in a9ea657
Steps to reproduce the bug
-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 exceptioncurl -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:
First line is come, probably from fastapi and other are exception.
Environment
Running uvicorn 0.15.0 with CPython 3.8.6 on Windows
uvicorn main:app --host 0.0.0.0 --port 80
Additional context
This is similar issues:
HttpParserInvalidMethodError
exception #75The text was updated successfully, but these errors were encountered: