Request middlware runs twice when exception is thrown
See original GitHub issueI can’t tell if this is intentional or not. I think I’ve brought it up before, but its affecting me again when maintaining some of my plugins.
Minimal reproduction:
from sanic import Sanic
from sanic.exceptions import SanicException
from sanic.response import html
app = Sanic(__name__)
@app.middleware("request")
def my_middleware1(request):
print("Middleware 1 ran", flush=True)
@app.middleware("request")
def my_middleware2(request):
print("Middleware 2 ran", flush=True)
raise SanicException()
@app.get("/")
def index(request):
return html("<p>hello world</p>")
if __name__ == "__main__":
app.run(debug=True, auto_reload=False, access_log=False)
Output:
[2022-01-04 13:32:31 +1000] [45332] [INFO] Starting worker [45332]
Middleware 1 ran
Middleware 2 ran
[2022-01-04 13:32:39 +1000] [45332] [ERROR] Exception occurred while handling uri: 'http://localhost:8000/'
Traceback (most recent call last):
File "handle_request", line 62, in handle_request
...
File "_run_request_middleware", line 26, in _run_request_middleware
...
File "/home/ash/CODE/sanic/sanicpluginsframework/this.py", line 17, in my_middleware2
raise SanicException()
sanic.exceptions.SanicException: None
Middleware 1 ran #<--- Ran again!
Middleware 2 ran #<--- Ran again!
This is because app handle_exception
does executes _run_request_middleware
again, after handle_request
has already executed _run_request_middleware
.
Offending code is here: https://github.com/sanic-org/sanic/blob/a7bc8b56bab01e066357e6dcc67c0dc9df864298/sanic/app.py#L786-L791
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Why is my middleware executed twice? - Stack Overflow
The reason why your middleware is getting run twice is that it is being run once for the initial request, and once for...
Read more >RequestLoggingMiddleware does not swallow exception #129
In development environment exceptions are logged twice , because RequestLoggingMiddleware is not swallowing exceptions and ...
Read more >Health checks in ASP.NET Core | Microsoft Learn
If CheckHealthAsync throws an exception during the check, ... By default, the Health Checks Middleware runs all registered health checks.
Read more >Avoiding Startup service injection in ASP.NET Core 3
In this post I describe the changes to dependency injection in your Startup class when upgrading an ASP.NET Core 2.x app to .NET...
Read more >Exception Handling in Spring MVC
Normally any unhandled exception thrown when processing a web-request causes the server to return an HTTP 500 response. However, any exception ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep, this can be closed. There was a problem with my test setup. As you see, my example code runs without issue.
Phew 😌
I was worried this morning when I saw this issue in my inbox 😆