title: Django uncaught exception when file upload request canceled
body: <p>I have an async file uploader using Dropzone.js and everything works fine except for a strange server error that appears from time to time when I cancel an upload request. The error is inconsistent, sometimes I get the expected result after canceling the upload request and other times the error shows up in Identical conditions.</p>
  
  <p><strong>Please note that I'm using chrome's network throttling so I can cancel uploads before the upload finishes.</strong></p>
  
  <p>This is the file upload view :</p>
  
  <pre><code>@login_required
  def media_upload(request):
  
      if request.method == 'POST' and request.FILES.getlist('media'):
  
          file = request.FILES.getlist('media')[0]
          fs = FileSystemStorage()
  
          try:
              img = Image.open(file)
              img.verify()
  
          except Exception as exp:
              return HttpResponse(exp, status=400)
  
          # Converting and saving media
          img = Image.open(file)
          media_prefix = ('evnt_u' + str(request.user.id))
          media_ext = '.jpg'
  
          if img.format != 'JPEG':
              img = img.convert('RGB')
              buffer01 = BytesIO()
              img.save(buffer01, format='JPEG')
              file = InMemoryUploadedFile(
                  buffer01, None, (media_prefix+media_ext), 'image/jpeg', buffer01.tell, None)
  
          media = fs.save((media_prefix+media_ext), file)
          return HttpResponse(media, status=200)
  
      else:
          return HttpResponse(status=400)
  </code></pre>
  
  <p>I receive <code>POST /media/upload/ HTTP/1.1" 400 0</code> correctly with the code above but that's not consistent. the other times I receive the appropriate response followed by a chain of uncaught exceptions:</p>
  
  <pre><code>Bad Request: /fa/media/upload/
  [05/Apr/2020 18:36:51] "POST /fa/media/upload/ HTTP/1.1" 400 0
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 138, in run
      self.finish_response()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
      self.write(data)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 274, in write
      self.send_headers()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 332, in send_headers
      self.send_preamble()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 255, in send_preamble
      ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 453, in _write
      result = self.stdout.write(data)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 796, in write
      self._sock.sendall(b)
  OSError: [Errno 41] Protocol wrong type for socket
  [05/Apr/2020 18:36:51] "POST /fa/media/upload/ HTTP/1.1" 500 59
  ----------------------------------------
  Exception happened during processing of request from ('127.0.0.1', 49199)
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 138, in run
      self.finish_response()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
      self.write(data)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 274, in write
      self.send_headers()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 332, in send_headers
      self.send_preamble()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 255, in send_preamble
      ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 453, in _write
      result = self.stdout.write(data)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 796, in write
      self._sock.sendall(b)
  OSError: [Errno 41] Protocol wrong type for socket
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 141, in run
      self.handle_error()
    File "/Volumes/Storage/WorkSpace/Projects/envs/meetup_dev/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 86, in handle_error
      super().handle_error()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 368, in handle_error
      self.finish_response()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
      self.write(data)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 274, in write
      self.send_headers()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 331, in send_headers
      if not self.origin_server or self.client_is_modern():
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 344, in client_is_modern
      return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
  TypeError: 'NoneType' object is not subscriptable
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 647, in process_request_thread
      self.finish_request(request, client_address)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 357, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 717, in __init__
      self.handle()
    File "/Volumes/Storage/WorkSpace/Projects/envs/meetup_dev/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 154, in handle
      handler.run(self.server.get_app())
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 144, in run
      self.close()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/simple_server.py", line 35, in close
      self.status.split(' ',1)[0], self.bytes_sent
  AttributeError: 'NoneType' object has no attribute 'split'
  ----------------------------------------
  </code></pre>