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.

Request middlware runs twice when exception is thrown

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ashleysommercommented, Jan 4, 2022

Yep, this can be closed. There was a problem with my test setup. As you see, my example code runs without issue.

0reactions
ahopkinscommented, Jan 4, 2022

Phew 😌

I was worried this morning when I saw this issue in my inbox 😆

Read more comments on GitHub >

github_iconTop 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 >

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