|
9 | 9 | Response, |
10 | 10 | ) |
11 | 11 | from aws_lambda_powertools.event_handler.middlewares import NextMiddleware |
12 | | -from aws_lambda_powertools.event_handler.middlewares.async_utils import AsyncMiddlewareFrame |
| 12 | +from aws_lambda_powertools.event_handler.middlewares.async_utils import AsyncMiddlewareFrame, wrap_middleware_async |
13 | 13 | from tests.functional.utils import load_event |
14 | 14 |
|
15 | 15 | API_REST_EVENT = load_event("apiGatewayProxyEvent.json") |
@@ -45,6 +45,28 @@ async def next_handler(app: ApiGatewayResolver): |
45 | 45 | asyncio.run(frame(app)) |
46 | 46 |
|
47 | 47 |
|
| 48 | +def test_wrap_middleware_async_sync_raising_before_next_does_not_deadlock(): |
| 49 | + # GIVEN a sync middleware that raises before calling next(), using wrap_middleware_async |
| 50 | + # This exercises _run_sync_middleware_in_thread directly |
| 51 | + app = _make_app() |
| 52 | + |
| 53 | + class AuthError(Exception): |
| 54 | + pass |
| 55 | + |
| 56 | + def failing_middleware(app, next_middleware): |
| 57 | + raise AuthError("denied") |
| 58 | + |
| 59 | + async def next_handler(app): |
| 60 | + return Response(200, content_types.TEXT_HTML, "should not reach") |
| 61 | + |
| 62 | + wrapped = wrap_middleware_async(failing_middleware, next_handler) |
| 63 | + |
| 64 | + # WHEN calling the wrapped middleware |
| 65 | + # THEN the exception propagates without deadlocking |
| 66 | + with pytest.raises(AuthError, match="denied"): |
| 67 | + asyncio.run(wrapped(app)) |
| 68 | + |
| 69 | + |
48 | 70 | def test_async_middleware_raising_before_next_propagates(): |
49 | 71 | # GIVEN an async middleware that raises before calling next() |
50 | 72 | app = _make_app() |
|
0 commit comments