Skip to content

Commit 1c2ab54

Browse files
fix(asm): fix another body read blocking problem on Flask (#4446) (#4480)
Signed-off-by: Juanjo Alvarez <[email protected]> Signed-off-by: Juanjo Alvarez <[email protected]> (cherry picked from commit 4008a0f) Co-authored-by: Juanjo Alvarez Martinez <[email protected]>
1 parent 8e6f979 commit 1c2ab54

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

ddtrace/contrib/flask/patch.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,15 @@ def traced_wsgi_app(pin, wrapped, instance, args, kwargs):
359359
wsgi_input = environ.get("wsgi.input", "")
360360

361361
# Copy wsgi input if not seekable
362-
try:
363-
seekable = wsgi_input.seekable()
364-
except AttributeError:
365-
seekable = False
366-
if not seekable:
367-
body = wsgi_input.read()
368-
environ["wsgi.input"] = BytesIO(body)
362+
if wsgi_input:
363+
try:
364+
seekable = wsgi_input.seekable()
365+
except AttributeError:
366+
seekable = False
367+
if not seekable:
368+
content_length = int(environ.get("CONTENT_LENGTH", 0))
369+
body = wsgi_input.read(content_length) if content_length else wsgi_input.read()
370+
environ["wsgi.input"] = BytesIO(body)
369371

370372
try:
371373
if content_type == "application/json":
@@ -387,10 +389,11 @@ def traced_wsgi_app(pin, wrapped, instance, args, kwargs):
387389
log.warning("Failed to parse werkzeug request body", exc_info=True)
388390
finally:
389391
# Reset wsgi input to the beginning
390-
if seekable:
391-
wsgi_input.seek(0)
392-
else:
393-
environ["wsgi.input"] = BytesIO(body)
392+
if wsgi_input:
393+
if seekable:
394+
wsgi_input.seek(0)
395+
else:
396+
environ["wsgi.input"] = BytesIO(body)
394397

395398
trace_utils.set_http_meta(
396399
span,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
ASM: fix a body read problem on some corner case where don't passing the content length makes wsgi.input.read() blocks.

0 commit comments

Comments
 (0)