Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZEon 1.35.0 #2508

Closed
aljazkosir opened this issue Nov 15, 2023 · 10 comments · Fixed by #2513
Closed

Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZEon 1.35.0 #2508

aljazkosir opened this issue Nov 15, 2023 · 10 comments · Fixed by #2513
Assignees
Labels
Integration: Django Type: Bug Something isn't working

Comments

@aljazkosir
Copy link

aljazkosir commented Nov 15, 2023

How do you use Sentry?

self hosted

Version

1.35.0

Steps to Reproduce

When uploading files through Django I get the following error on 1.35.0:

Traceback (most recent call last):
  File "E:\projects\test\venv311\Lib\site-packages\channels\routing.py", line 62, in __call__
    return await application(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\asgi.py", line 146, in _run_asgi3
    return await self._run_app(scope, receive, send, asgi_version=3)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\asgi.py", line 241, in _run_app
    raise exc from None
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\asgi.py", line 234, in _run_app
    return await self.app(
           ^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\django\asgi.py", line 83, in sentry_patched_asgi_handler
    return await middleware(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\asgi.py", line 146, in _run_asgi3
    return await self._run_app(scope, receive, send, asgi_version=3)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\asgi.py", line 161, in _run_app
    raise exc from None
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\asgi.py", line 157, in _run_app
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\django\core\handlers\asgi.py", line 160, in __call__
    await self.handle(scope, receive, send)
  File "E:\projects\test\venv311\Lib\site-packages\django\core\handlers\asgi.py", line 177, in handle
    request, error_response = self.create_request(scope, body_file)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\sentry_sdk\integrations\django\asgi.py", line 104, in sentry_patched_create_request
    _ = request.body
        ^^^^^^^^^^^^
  File "E:\projects\test\venv311\Lib\site-packages\django\http\request.py", line 337, in body
    raise RequestDataTooBig(
django.core.exceptions.RequestDataTooBig: Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.
ERROR    2023-11-15 11:05:58,330 django.channels.server    75700 70060 HTTP POST /photo_gallery/2703/upload-image/ 500 [0.34, 127.0.0.1:49724]

I'm using sentry replay integration. Error was reported to sentry and it seems to be originating from sentry_sdk.integrations.asgi.SentryAsgiMiddleware.

It works on 1.34.0 though.

Expected Result

File to be uploaded

Actual Result

Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE error is thrown.

@sentrivana
Copy link
Contributor

Hey @aljazkosir, thank you for reporting this. This indeed looks like a regression introduced in 1.35.0, so please stay on 1.34 until we fix this.

@antonpirker
Copy link
Member

Hey @aljazkosir ! Just to double check, you have Django with Channels and run in on Daphne (or another ASGI server) right?

@antonpirker
Copy link
Member

Oh, and what is your DATA_UPLOAD_MAX_MEMORY_SIZE setting and what is the size of the file you try to upload?

@antonpirker
Copy link
Member

Hey @aljazkosir

I just created a demo application. With 1.35.0 I get the error you mentioned. But only when the uploaded file size is over the default limit. But I also could verify that with 1.34.0 the error does not appear.

The cause for the problem is that in ASGI you can not read the body multiple times, so Sentry tries to read the body in any case so Django caches it. This enables your code to read the body and Sentry to read the body to attach it to exceptions.

But I guess I have to change my code that the body is only read when absolutely necessary to not trigger Djangos DATA_UPLOAD_MAX_MEMORY_SIZE check.

@aljazkosir
Copy link
Author

Django with channels on Daphe. DATA_UPLOAD_MAX_MEMORY_SIZE is default which is 2.5MB I think. File I tried to upload was 6MB. However it seems to work for smaller files on this release aswell.

@antonpirker
Copy link
Member

Thanks for the info!

Note to self:
This is the fix that cased this regression: #2495

@jberends
Copy link

jberends commented Nov 17, 2023

This bug wreaks havoc through our system on every file upload. Sending 🍰 to customers as we speak.

@jberends
Copy link

jberends commented Nov 17, 2023

Django with channels on Daphe. DATA_UPLOAD_MAX_MEMORY_SIZE is default which is 2.5MB I think. File I tried to upload was 6MB. However it seems to work for smaller files on this release aswell.

In Django the concepts of DATA_UPLOAD_MAX_MEMORY_SIZE and FILE_UPLOAD_MAX_MEMORY_SIZE are working in combination with each other. The default is 2.5 MB. The idea is that the request is to be streamed to disk whenever the request content-lenght is larger than this default. So when hitting the check for this limit in django/http/request.py (Line 335 in Django 3.2) it raises RequestDataTooBig exception.

It appears that sentry prevents the request body from streaming to disk and keeps the request body in memory invoking this exception. We experience this with every upload of media.

Screenshot 2023-11-17 at 08 43 13

@antonpirker
Copy link
Member

@jberends Sorry to hear that, and thanks for the additional information.
I am doing my best, to have a fix out asap!

@jberends
Copy link

@jberends Sorry to hear that, and thanks for the additional information. I am doing my best, to have a fix out asap!

no worries. These things happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Integration: Django Type: Bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants