Skip to content
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

Fix handling of errors in background tasks in Starlette middleware #63

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions apitally/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "http" and scope["method"] != "OPTIONS":
request = Request(scope)
response_status = 0
response_time = 0.0
response_time: Optional[float] = None
response_headers = Headers()
response_body = b""
response_size = 0
response_chunked = False
exception: Optional[BaseException] = None
start_time = time.perf_counter()

async def send_wrapper(message: Message) -> None:
Expand Down Expand Up @@ -92,24 +93,19 @@ async def send_wrapper(message: Message) -> None:
try:
await self.app(scope, receive, send_wrapper)
except BaseException as e:
self.add_request(
request=request,
response_status=500,
response_time=time.perf_counter() - start_time,
response_headers=response_headers,
response_body=response_body,
response_size=response_size,
exception=e,
)
exception = e
raise e from None
else:
finally:
if response_time is None:
response_time = time.perf_counter() - start_time
self.add_request(
request=request,
response_status=response_status,
response_time=response_time,
response_headers=response_headers,
response_body=response_body,
response_size=response_size,
exception=exception,
)
else:
await self.app(scope, receive, send) # pragma: no cover
Expand All @@ -129,6 +125,8 @@ def add_request(
consumer = self.get_consumer(request)
consumer_identifier = consumer.identifier if consumer else None
self.client.consumer_registry.add_or_update_consumer(consumer)
if response_status == 0 and exception is not None:
response_status = 500
self.client.request_counter.add_request(
consumer=consumer_identifier,
method=request.method,
Expand Down
30 changes: 29 additions & 1 deletion tests/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

if find_spec("starlette") is None:
pytest.skip("starlette is not available", allow_module_level=True)
else:
# Need to import BackgroundTasks at package level to avoid NameError in FastAPI
from starlette.background import BackgroundTasks

if TYPE_CHECKING:
from starlette.applications import Starlette
Expand Down Expand Up @@ -64,13 +67,22 @@ def stream_response():

return StreamingResponse(stream_response())

def task(request: Request):
def task_func_with_error():
raise ValueError("task")

tasks = BackgroundTasks()
tasks.add_task(task_func_with_error)
return PlainTextResponse("ok", background=tasks)

routes = [
Route("/foo/", foo),
Route("/foo/{bar}/", foo_bar),
Route("/bar/", bar, methods=["POST"]),
Route("/baz/", baz, methods=["POST"]),
Route("/val/", val),
Route("/stream/", stream),
Route("/task/", task, methods=["POST"]),
]
app = Starlette(routes=routes)
app.add_middleware(ApitallyMiddleware, client_id=CLIENT_ID, env=ENV)
Expand Down Expand Up @@ -117,6 +129,14 @@ def stream_response():

return StreamingResponse(stream_response())

@app.post("/task/")
def task(background_tasks: BackgroundTasks):
def task_func_with_error():
raise ValueError("task")

background_tasks.add_task(task_func_with_error)
return "ok"

return app


Expand Down Expand Up @@ -176,6 +196,14 @@ def test_middleware_requests_error(app: Starlette, mocker: MockerFixture):
exception = mock2.call_args.kwargs["exception"]
assert isinstance(exception, ValueError)

# Throws a ValueError in a background task, but returns 200
response = client.post("/task/")
assert response.status_code == 200
assert mock1.call_count == 2
assert mock1.call_args is not None
assert mock1.call_args.kwargs["status_code"] == 200
mock2.assert_called_once() # Not called again


def test_middleware_requests_unhandled(app: Starlette, mocker: MockerFixture):
from starlette.testclient import TestClient
Expand Down Expand Up @@ -216,7 +244,7 @@ def test_get_startup_data(app: Starlette, mocker: MockerFixture):
app.middleware_stack = app.build_middleware_stack()

data = _get_startup_data(app=app.middleware_stack, app_version="1.2.3", openapi_url=None)
assert len(data["paths"]) == 6
assert len(data["paths"]) == 7
assert data["versions"]["starlette"]
assert data["versions"]["app"] == "1.2.3"
assert data["client"] == "python:starlette"