question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FastAPI file upload not working with sentry after 1.9.6

See original GitHub issue

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.9.6

Steps to Reproduce

from fastapi import FastAPI, File, UploadFile
from PIL import Image
import uvicorn
import io

import sentry_sdk
from sentry_sdk.integrations.starlette import StarletteIntegration
from sentry_sdk.integrations.fastapi import FastApiIntegration

sentry_sdk.init(
    dsn=SECRET,
    release="server",
    integrations=[
        StarletteIntegration(),
        FastApiIntegration(),
    ],
    attach_stacktrace=True,
    request_bodies="always",
    send_default_pii=True,
    traces_sample_rate=0.0,
)

app = FastAPI()


@app.post("/predict")
async def predict(file: UploadFile = File(default=None)):
    contents = await file.read()
    Image.open(io.BytesIO(contents)).convert("RGB")
    return {"result": "ok"}


uvicorn.run(app, host="0.0.0.0", port=8888, loop="asyncio", lifespan="on")

Expected Result

{"result": "ok"}

Actual Result

Please remove 'SentryAsgiMiddleware' from your project.
See https://docs.sentry.io/platforms/python/guides/asgi/ for more information.

so, when disabling sentry - all works fine, when adding sentry - it breaks. apparently sentry messes up content of uploaded file.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
RockBombercommented, Sep 15, 2022

with version 1.9.8 still got the error.

1reaction
tristancommented, Sep 16, 2022

We managed to work around this problem for now by adding await file.seek(0) before the await file.read() call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request Files - FastAPI
Define File Parameters​​ The files will be uploaded as "form data". If you declare the type of your path operation function parameter as...
Read more >
sentry Changelog - PyUp.io
**Breaking Change**: This version introduces a change to the grouping of issues. The SDK now sets the `inApp` flag for frames originating from...
Read more >
Python - CVE - Search Results
The problem has been patched in XWiki 14.6RC1, 13.10.8 and 14.4.3. ... This could lead to uploading python files which can be later...
Read more >
How to Upload File using FastAPI? - python - Stack Overflow
I just use f = open(file) method once, and when I send the request several times, the f will be closed after the...
Read more >
Source Packages in "jammy", Subsection misc - Ubuntu
Source Packages in "jammy", Subsection misc. 0xffff (0.9-1) [universe]; 1oom (1.0-2) [multiverse]; 2048 (0.20210105.1243-1) [universe] ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found