Flask middleware errors when an earlier Flask extension throws an exception in a before_request method
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- backend, documentation, observability-sre
Hướng nghiên cứu
Bắt đầu với aws_xray_sdk/ext/flask/middleware.py, tại khu vực quanh handler after_request được liên kết, sau đó xem xét hành vi về thứ tự của các extension Flask được mô tả trong issue và tài liệu được liên kết. Chạy bản tái hiện đính kèm bằng app.py và các requirements của nó để xác nhận lỗi. Được xem là hoàn tất khi request hoàn thành với phản hồi Bad Request như mong đợi và hướng dẫn của Flask đề cập đến vấn đề về thứ tự.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
If the Flask middleware is configured after another extension, and that extension throws an exception in a before_request method, the Flask middleware throws an exception in its after_request method.
File "/home/sam/.pyenv/versions/3.9.7/envs/flask-test-2-3.9.7/lib/python3.9/site-packages/flask/app.py", line 1508, in finalize_request
response = self.process_response(response)
File "/home/sam/.pyenv/versions/3.9.7/envs/flask-test-2-3.9.7/lib/python3.9/site-packages/flask/app.py", line 2002, in process_response
response = self.ensure_sync(func)(response)
File "/home/sam/.pyenv/versions/3.9.7/envs/flask-test-2-3.9.7/lib/python3.9/site-packages/aws_xray_sdk/ext/flask/middleware.py", line 74, in _after_request
segment.put_http_meta(http.STATUS, response.status_code)
AttributeError: 'NoneType' object has no attribute 'put_http_meta'
cannot find the current segment/subsegment, please make sure you have a segment open
This does not happen if the Flask middleware is configured before other extensions.
The root cause appears to be that when an extension throws an exception in a before_request method, processing of subsequent before request methods is suppressed, but after_request methods are still executed.
I think there's two things needed to do to resolve this:
- Make it clear in https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/frameworks.html#flask that Flask should be the first extension configured.
- In the after_request handler, handle the current segment or subsegment being None without raising an exception.
Note that even if the after_request handler doesn't raise an exception, having other middleware prevent the Flask middleware's before_request method from executing is a problem, because it means that you will not get X-Ray traces when this happens. I suggest that the after_request handler should log a warning or error explaining the problem if the current segment or subsegment is None.
I've attached a reproduction. This relies on the fact that Flask-WTF throws an exception to indicate a bad request when a CSRF token is missing from a request that requires one. To reproduce the bug:
- Unzip the zip to a directory.
- Install the requirements using pip install -r requirements.txt.
- In one shell, build and run the local X-Ray agent using
docker build -t xray-daemon . && docker run --rm --attach STDOUT -v ~/.aws/:/root/.aws/:ro --net=host -e AWS_PROFILE=PROFILE_NAME--name xray-daemon -p 2000:2000/udp xray-daemon -owhere PROFILE_NAME is a valid AWS profile that can talk to X-Ray (obviously skip this if you have a better way to get a local X-Ray daemon running). - In another shell, run the Flask application using
gunicorn -b 0.0.0.0:8080 app:app. - In a third shell, make a POST command using
curl -X POST http://localhost:8080/post. - You will see the exception in the Flask application.
You can see that ordering matters by swapping the following two lines in app.py:
csrf = CSRFProtect(app)
XRayMiddleware(app, xray_recorder)
If the XRayMiddleware is initialised first, the request will succeed, and you'll see the Bad Request error that you should (because the request does not have a CSRF token).
- Ngôn ngữ chính
- Python
- Star
- 340
- Fork
- 148
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của aws/aws-xray-sdk-python
-
psycopg connection context manager drops X-Ray tracing wrapper, so cursor execute calls are not tracedCó thể đã có người làm @reginaldalfret đã nhận 20 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
aws/aws-xray-sdk-python#490 · 1 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 1/100
aws/aws-xray-sdk-python#460 ·
-
Next releaseĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
aws/aws-xray-sdk-python#457 · 1 reaction ·
-
mysql-connector 9.2.0 - TypeError: 'MySQLConnection' object is not iterableCó thể đã có người làm @reginaldalfret đã nhận 20 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
aws/aws-xray-sdk-python#453 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
aws/aws-xray-sdk-python#452 · 1 reaction ·
Tất cả issue của aws/aws-xray-sdk-python
Issue tương tự
-
Workflow.schedule stops when a cron deadline passes before sleepCó thể đã có người làm @HuaTNA đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] HiveDialect shares its colspecs dict with SQLAlchemy's DefaultDialect (python/pyhive)Có thể đã có người làm @rusackas đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 80/100
apache/kyuubi#7786 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[20.0] hr_work_entry: Duration column of Export Time Entries is truncated to whole hoursCó thể đã có người làm @odooaktiv đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
fix formatting of the sample database page of the docsCó thể đã có người làm @Aaqibhafeezkhan đã nhận hôm nay. Đang mởdocumentation good first issue help wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
chaoss/CollectOSS#544 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày