Flask middleware errors when an earlier Flask extension throws an exception in a before_request method
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
調査の方向性
aws_xray_sdk/ext/flask/middleware.py の、リンクされている after_request ハンドラー付近から始め、issue に記載されている Flask の拡張機能の順序に関する動作と、リンクされているドキュメントを確認します。app.py とその requirements を使って添付の再現手順を実行し、失敗を確認します。リクエストが期待される Bad Request レスポンスで完了し、Flask のガイダンスで順序の問題が扱われていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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).
- 主要言語
- Python
- スター
- 339
- フォーク
- 148
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
aws/aws-xray-sdk-python のほかの issue
-
psycopg connection context manager drops X-Ray tracing wrapper, so cursor execute calls are not traced対応中かも @reginaldalfret が 17 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
aws/aws-xray-sdk-python#490 · コメント 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 1/100
aws/aws-xray-sdk-python#460 ·
-
Next releaseオープン
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
aws/aws-xray-sdk-python#457 · リアクション 1 件 ·
-
mysql-connector 9.2.0 - TypeError: 'MySQLConnection' object is not iterable対応中かも @reginaldalfret が 17 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
aws/aws-xray-sdk-python#453 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
aws/aws-xray-sdk-python#452 · リアクション 1 件 ·
aws/aws-xray-sdk-python の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
Status: Triage Type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
-
Objects.List treats versions=false (and similar flags) as true対応中かも @andaaron が今日担当しました。 オープンpriority: p2 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
googleapis/storage-testbench#829 ·
メンテナーはふだん 2 日以内に返信
-
[Bug]: memory provider treats 1:1 DMs as shared surfaces, so DM turns never receive reviewed recordsオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
rlaope/oh-my-hermes#2088 ·
メンテナーはふだん 1 日以内に返信