Django drops connection on some requests

The problem I have is that when sending some requests to Django sever, it closes the connection before time.

Here is a simple project on Github

This is how it works for a simple POST request without a body: enter image description here

But when I post some ~ 15MB binary data, Postmen displays a connection error: enter image description here

But curl works fine

curl -X POST -d "@bin/ngrok" localhost:3000/test/

      

I thought it was a bug in Postman, but it doesn't work on mobile either.

I tried to compare the request headers. I tried to throw up midding. I tried to debug Djnago code. But I can find a solution. Can you help me?

UPDATE 1

in the settings.py file

FILE_UPLOAD_MAX_MEMORY_SIZE = 1000 * 1000 * 1000
DATA_UPLOAD_MAX_MEMORY_SIZE = 1000 * 1000 * 1000

      

does not fix the problem

UPDATE 2

I added a line print(len(request.body))

to the index method:

@csrf_exempt
def index(request):
    print(len(request.body))

    return HttpResponse("Hello")

      

And now it works. But why should I direct the body to a request? In my real project, I am checking an authentication token, and if it is wrong, I have not read something from the body.

+3


source to share


2 answers


Yup, I can reproduce this using your repo.

I took a look with Wireshark and it looks like Django's devanserver actually responds before sending the entire payload. I'm not sure what the HTTP spec says about whether this is good or not, but it is obvious that some client libraries (used by Postman and your mobile device) consider this error, while others like libcurl are fine with it.

When you add print(len(request.body))

, it forces Django (and the underlying stack) to consume the whole organism to figure out its length before issuing a response.



This also happens when running the application under uwsgi, for what it's worth.

The TCP stream looks like this in Wireshark: enter image description here

+1


source


I had the same problem a few weeks ago for one of my projects on Heroku. Heroku logs showed a not very useful "Server Abort" error. After a few days of debugging and studying the django source code, I found out that it has something to do with django Upload Handlers .



There is a FILE_UPLOAD_MAX_MEMORY_SIZE option, which is 2.5 MB by default. Any download less than this parameter will be handled by the MemoryFileUploadHandler (in memory), otherwise the TemporaryFileUploadHandler is responsible for handling the files. I never found out why, but on Heroku, TemporaryFileUploadHandler always caused the application to crash. I created a workaround by increasing the setting to the maximum file size that my client will upload.

0


source







All Articles