Skip to content

Commit 1f04fd4

Browse files
committed
Not closing segment in after_request in case of internal error
1 parent 5af2357 commit 1f04fd4

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

aws_xray_sdk/ext/flask/middleware.py

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def _after_request(self, response):
8181
if cont_len:
8282
segment.put_http_meta(http.CONTENT_LENGTH, int(cont_len))
8383

84+
if response.status_code >= 500:
85+
return response
86+
8487
if self.in_lambda_ctx:
8588
self._recorder.end_subsegment()
8689
else:

tests/ext/flask/test_flask.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ def template():
3939
recorder.configure(service='test', sampling=False, context=Context())
4040
XRayMiddleware(app, recorder)
4141

42-
# enable testing mode
43-
app.config['TESTING'] = True
42+
# We don't need to enable testing mode by doing app.config['TESTING'] = True
43+
# because what it does is disable error catching during request handling,
44+
# so that you get better error reports when performing test requests against the application.
45+
# But this also results in `after_request` method not getting invoked during unhandled exception which we want
46+
# since it is the actual application behavior in our use case.
4447
app = app.test_client()
4548

4649
BASE_URL = 'http://localhost{}'

0 commit comments

Comments
 (0)