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

wsgiref BaseHandler / SimpleHandler can raise additional errors when handling an error #71869

Closed
SG mannequin opened this issue Aug 4, 2016 · 26 comments
Closed

wsgiref BaseHandler / SimpleHandler can raise additional errors when handling an error #71869

SG mannequin opened this issue Aug 4, 2016 · 26 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@SG
Copy link
Mannequin

SG mannequin commented Aug 4, 2016

BPO 27682
Nosy @pjeby, @kristjanvalur, @tjguk, @bitdancer, @asvetlov, @cjerdonek, @berkerpeksag, @vadmium, @zooba, @PetterS, @tirkarthi, @wkoot
PRs
  • bpo-27682: Handle client connection terminations in wsgiref. #9713
  • [3.7] bpo-27682: Handle client connection terminations in wsgiref (GH-9713) #13043
  • bpo-27682: Update whatsnew/3.8. #13748
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-05-01.18:01:07.989>
    created_at = <Date 2016-08-04.12:33:13.624>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'wsgiref BaseHandler / SimpleHandler can raise additional errors when handling an error'
    updated_at = <Date 2019-07-22.18:17:19.501>
    user = 'https://bugs.python.org/SG'

    bugs.python.org fields:

    activity = <Date 2019-07-22.18:17:19.501>
    actor = 'Petter S'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-01.18:01:07.989>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2016-08-04.12:33:13.624>
    creator = 'SG'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 27682
    keywords = ['patch']
    message_count = 26.0
    messages = ['271969', '271973', '271975', '271977', '271984', '271985', '271989', '271994', '272208', '272228', '272244', '272252', '272255', '310363', '327564', '327571', '329171', '329221', '329248', '339438', '341217', '341219', '341220', '348301', '348302', '348303']
    nosy_count = 16.0
    nosy_names = ['pje', 'kristjan.jonsson', 'tim.golden', 'r.david.murray', 'asvetlov', 'chris.jerdonek', 'berker.peksag', 'martin.panter', 'steve.dower', 'iMath', 'SG', 'William Hingston', 'Petter S', 'xtreak', 'lorcan', 'wkoot']
    pr_nums = ['9713', '13043', '13748']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27682'
    versions = ['Python 3.7', 'Python 3.8']

    @SG
    Copy link
    Mannequin Author

    SG mannequin commented Aug 4, 2016

    While saving, this error shows up and is whatever is saving gets saved twice-

    Traceback (most recent call last):
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 138, in run
        self.finish_response()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 274, in write
        self.send_headers()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
    [04/Aug/2016 17:25:59] "GET /api/importeop/ HTTP/1.1" 500 59
    ----------------------------------------
    Exception happened during processing of request from ('127.0.0.1', 51082)
    Traceback (most recent call last):
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 138, in run
        self.finish_response()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 274, in write
        self.send_headers()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 141, in run
        self.handle_error()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\servers\basehttp.py", line 92, in handle_error
        super(ServerHandler, self).handle_error()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 368, in handle_error
        self.finish_response()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 274, in write
        self.send_headers()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 331, in send_headers
        if not self.origin_server or self.client_is_modern():
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\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 "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 625, in process_request_thread
        self.finish_request(request, client_address)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 354, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\servers\basehttp.py", line 99, in __init__
        super(WSGIRequestHandler, self).__init__(*args, **kwargs)
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 681, in __init__
        self.handle()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle
        handler.run(self.server.get_app())
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 144, in run
        self.close()
      File "C:\Users\Sushant Gahlot\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\simple_server.py", line 36, in close
        self.status.split(' ',1)[0], self.bytes_sent
    AttributeError: 'NoneType' object has no attribute 'split'

    @SG SG mannequin added the stdlib Python modules in the Lib dir label Aug 4, 2016
    @bitdancer
    Copy link
    Member

    The traceback looks like wsgiref.close() was called before handle_error() was called (perhaps for a second time?)

    We'll probably need a reproducer (as simple as possible please) to debug this further, which might be difficult since it may be tricky to simulate the windows error. But you might also want to, say, post to python-list and see if anyone can spot a problem with your code. Could it be that it is calling wsgiref.close at the wrong time?

    @SG
    Copy link
    Mannequin Author

    SG mannequin commented Aug 4, 2016

    The code is actually quite simple, I'll post it over here-

    def import_eop(request):
      with open('C:/Users/sg/Desktop/projects/drfapi/venv/drf/prices_companies_indices_transpose.csv') as f:
        contents = f.readlines()
        dates = contents[0]
        dates = dates.split(",")
        dates = dates[1:]
        d = []
        for i in range(len(dates)):
          d.append(Dates(date=dates[i]))
        Dates.objects.bulk_create(d)
        companies = []
        entry = []
        for i in range(1, len(contents)):
          csline = contents[i].split(",")
          prices = csline[1:len(csline)]
          company = csline[0]
          prices =[float(x) for x in prices]
          entry = (Endday(company=company, eop=prices))
          entry.save()
      f.close()
      print('Saved to database')
      return HttpResponse('Ok')

    Now as you see, I'm not even handling wsgiref.close() at all. This isn't a django issue since django community themselves pointed me to this direction. Let me know if I can be of any help.

    @bitdancer
    Copy link
    Member

    If the save is happening twice, that must mean that that method is getting called twice, and I'm guessing that the second time it is called wsgiref is closed and thus you get the error. But that's just a guess, your code snippet by itself isn't enough to see how everything fits together.

    @SG
    Copy link
    Mannequin Author

    SG mannequin commented Aug 4, 2016

    There have been a few times when this code ran perfectly fine, without any errors whatsoever.

    So the structure is like this-

    This code reads a csv, processes it and save it in a model-

    class Endday(models.Model):
      company = models.TextField(null=True)
      eop = models.TextField(max_length=100000) 

    Now the database is postgresql, as soon as save is called, data is posted there. This code runs fine on my colleague's computer(same code) but he has windows 7. Thinking this might be a windows error, I re-installed windows 10 from the scratch. But to no help. At last, I had to knock python's door.

    @SG
    Copy link
    Mannequin Author

    SG mannequin commented Aug 4, 2016

    Also, since I re-installed everything, I got python 3.5 too

    @berkerpeksag
    Copy link
    Member

    Which browser do you use? How big is the CSV file? Modern browsers tend to do aggressive prefetching so this might be the cause here.

    @SG
    Copy link
    Mannequin Author

    SG mannequin commented Aug 4, 2016

    The CSV is around 75 MB, not that big I guess. And I've tried Chrome, Edge and Firefox

    @iMath
    Copy link
    Mannequin

    iMath mannequin commented Aug 9, 2016

    I use the following django view function also produce the same exception

    def sendFiles(request): 
        fileName = request.GET['fileName']
    
        pathToFile = os.path.join(filesDir, fileName)
        response = FileResponse(open(pathToFile, 'rb'))
    response['Content-Type'] = 'application/octet-stream'
    response[
        'Content-Disposition'] = 'attachment; fileName="{}"'.format(fileName)
    response['Content-Length'] = os.path.getsize(pathToFile)
    # HttpResponse(open(os.path.join(os.getcwd(), 'LYYDownloaderServer.log'), 'r'), content_type='text/plain')
    return response
    

    Tested with sending file Git-2.8.4-32-bit.exe (29.8MB), if tested with sending file GitHubSetup.exe(670kb),then no exception occurred.

    the exception like the following

        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接
    。
    [09/Aug/2016 10:30:13] "GET /FileHost/?fileName=Git-2.8.4-32-bit.exe HTTP/1.1" 5
    00 59

    Exception happened during processing of request from ('127.0.0.1', 62237)
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 138, in run
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socket.py", lin
    e 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接
    。
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 141, in run
        self.handle_error()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 92, in handle_error
        super(ServerHandler, self).handle_error()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 368, in handle_error
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 331, in send_headers
        if not self.origin_server or self.client_is_modern():
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.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 "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 625, in process_request_thread
        self.finish_request(request, client_address)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 354, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 99, in __init__
        super(WSGIRequestHandler, self).__init__(*args, **kwargs)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 681, in __init__
        self.handle()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 179, in handle
        handler.run(self.server.get_app())
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 144, in run
        self.close()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\simple_
    server.py", line 36, in close
        self.status.split(' ',1)[0], self.bytes_sent
    AttributeError: 'NoneType' object has no attribute 'split'
    ----------------------------------------
    [09/Aug/2016 10:30:13] "GET /FileHost/?fileName=Git-2.8.4-32-bit.exe HTTP/1.1" 2
    00 8192
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 138, in run
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socket.py", lin
    e 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接
    。
    [09/Aug/2016 10:30:13] "GET /FileHost/?fileName=Git-2.8.4-32-bit.exe HTTP/1.1" 5
    00 59
    ----------------------------------------
    Exception happened during processing of request from ('127.0.0.1', 62229)
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 138, in run
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socket.py", lin
    e 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接
    。
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 141, in run
        self.handle_error()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 92, in handle_error
        super(ServerHandler, self).handle_error()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 368, in handle_error
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 331, in send_headers
        if not self.origin_server or self.client_is_modern():
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.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 "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 625, in process_request_thread
        self.finish_request(request, client_address)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 354, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 99, in __init__
        super(WSGIRequestHandler, self).__init__(*args, **kwargs)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 681, in __init__
        self.handle()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 179, in handle
        handler.run(self.server.get_app())
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 144, in run
        self.close()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\simple_
    server.py", line 36, in close
        self.status.split(' ',1)[0], self.bytes_sent
    AttributeError: 'NoneType' object has no attribute 'split'
    ----------------------------------------
    [09/Aug/2016 10:30:13] "GET /FileHost/?fileName=Git-2.8.4-32-bit.exe HTTP/1.1" 2
    00 8192
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 138, in run
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socket.py", lin
    e 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接
    。
    [09/Aug/2016 10:30:13] "GET /FileHost/?fileName=Git-2.8.4-32-bit.exe HTTP/1.1" 5
    00 59
    ----------------------------------------
    Exception happened during processing of request from ('127.0.0.1', 62231)
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 138, in run
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 332, in send_headers
        self.send_preamble()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 255, in send_preamble
        ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 453, in _write
        result = self.stdout.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socket.py", lin
    e 593, in write
        return self._sock.send(b)
    ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接
    。
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 141, in run
        self.handle_error()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 92, in handle_error
        super(ServerHandler, self).handle_error()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 368, in handle_error
        self.finish_response()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 180, in finish_response
        self.write(data)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 274, in write
        self.send_headers()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 331, in send_headers
        if not self.origin_server or self.client_is_modern():
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.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 "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 625, in process_request_thread
        self.finish_request(request, client_address)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 354, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 99, in __init__
        super(WSGIRequestHandler, self).__init__(*args, **kwargs)
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py
    ", line 681, in __init__
        self.handle()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\site-packages\d
    jango\core\servers\basehttp.py", line 179, in handle
        handler.run(self.server.get_app())
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handler
    s.py", line 144, in run
        self.close()
      File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\simple_
    server.py", line 36, in close
        self.status.split(' ',1)[0], self.bytes_sent
    AttributeError: 'NoneType' object has no attribute 'split'

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented Aug 9, 2016

    This error is a protocol error. It is the analog to WSAECONNRESET.
    ECONNRESET occurs when the local host receives a RST packet from the peer, usually because the peer closed the connection.
    WSAECONNABORT occurs when the local tcp layer decides that the connection is dead, (it may have sent RST to the peer itself). This can occur for various reasons. Often because the client has cone away, closed the connection or other things. It is best to treat WSACONNRESET as WSACONNABORT, i.e., there was a TCP protocol error and the transaction (http request) probably wasn't completed completely by both parties. See also here:
    https://www.chilkatsoft.com/p/p_299.asp

    In your case, I would expect a problem with the client uploading the file. It probably closes the connection after sending the data without waiting for the http response.

    @bitdancer
    Copy link
    Member

    Given that django is involved, it looks like it is most likely django that is calling the method after wsgiref is closed. This doesn't *look* like a stdlib problem from what we can see so far, but it certainly could be. We'd need a reproducer that didn't involve django to make any further progress from our side.

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented Aug 9, 2016

    As already stated, this error bubbles up from the TCP layer. It means that the tcp stack, for example, gave up resending a tcp frame and timed out, determining that the recipient was no longer listening.
    You cannot create this error yourself. If you, for example, call s.shutdown(SHUT_WR), you get a WSAESHUTDOWN error. If the connection is closed (via s.close()) you get a EBADF error.
    Now, the interaction with the client may cause the client to misbehave, but this sort of error is usually either due to the network (other host becomes unreachable) or misconfiguration of the local host's tcp stack.

    @bitdancer
    Copy link
    Member

    Kristján: right, what I'm saying about the possible bug is the final part of the traceback resulting from (apparently) wsgiref being closed and a new operation being attempted. *Probably* that is a django bug in handling a network error, but maybe it is a wsgiref bug in handling the network error. (I'm assuming here that the Windows error is triggered by the retry, but I could of course be completely wrong about that.)

    @vstinner vstinner changed the title Windows Error 10053, ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine wsgiref: Windows Error 10053, ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine Aug 17, 2016
    @WilliamHingston
    Copy link
    Mannequin

    WilliamHingston mannequin commented Jan 20, 2018

    I'm still seeing this with

    Python 3.6.3
    Django 2.0.1
    Windows 10 Pro Version 1709

    Was this determined to be a Django bug?

    @tirkarthi
    Copy link
    Member

    I think this is an issue with wsgiref handling client closure rather than a Windows/Django bug. I saw this sporadically but not consistently while running make serve to serve the docs which uses wsgiref simple server to serve docs on my Mac OS. Related bpo-34547 with a PR that catches ConnectionAbortedError and returns.

    PR link : #9713

    Sample traceback while serving docs locally with make serve on Mac OS:

    127.0.0.1 - - [12/Oct/2018 10:56:57] "GET /_sources/library/asyncio-eventloop.rst.txt HTTP/1.1" 500 59
    ----------------------------------------

    Exception happened during processing of request from ('127.0.0.1', 50436)
    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)
    BrokenPipeError: [Errno 32] Broken pipe
    
    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 "/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 313, in _handle_request_noblock
        self.process_request(request, client_address)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 344, in process_request
        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 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/simple_server.py", line 133, 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'

    Thanks

    @PetterS
    Copy link
    Mannequin

    PetterS mannequin commented Oct 12, 2018

    The Github pull request #9713 addresses this.

    @cjerdonek cjerdonek added 3.7 (EOL) end of life 3.8 only security fixes labels Nov 2, 2018
    @cjerdonek cjerdonek changed the title wsgiref: Windows Error 10053, ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine wsgiref BaseHandler / SimpleHandler can raise additional errors when handling an error Nov 2, 2018
    @cjerdonek cjerdonek added the type-bug An unexpected behavior, bug, or error label Nov 2, 2018
    @cjerdonek
    Copy link
    Member

    This seems like a bug in wsgiref.BaseHandler to me. BaseHandler.run() calls handle_error() if an error occurs inside finish_response():

    self.handle_error()

    However, finish_response() calls close() in its finally block:

    try:
    if not self.result_is_file() or not self.sendfile():
    for data in self.result:
    self.write(data)
    self.finish_content()
    finally:
    self.close()

    @vadmium
    Copy link
    Member

    vadmium commented Nov 4, 2018

    There seem to be at least two different issues raised here (and in bpo-34547):

    1. Double exceptions due to “self.environ” and “self.status” being None when the error handler is called. This problem was specifically raised in bpo-29183.

    2. What to do about exceptions such as ConnectionAbortedError, which are caused by the network or the client, and are outside the server’s control. I find it annoying when these exceptions are logged, and tend to add handlers for these exception types to silence them. I think this is what Petter is trying to do in <https://github.com/python/cpython/pull/9713\>.

    @PetterS
    Copy link
    Mannequin

    PetterS mannequin commented Nov 4, 2018

    I think this is what Petter is trying to do in <https://github.com/python/cpython/pull/9713\>.

    That's right.

    @PetterS
    Copy link
    Mannequin

    PetterS mannequin commented Apr 4, 2019

    The pull request solves this problem, but the discussion over there is really slow.

    @berkerpeksag
    Copy link
    Member

    New changeset 3d37ea2 by Berker Peksag (Petter Strandmark) in branch 'master':
    bpo-27682: Handle client connection terminations in wsgiref (GH-9713)
    3d37ea2

    @berkerpeksag
    Copy link
    Member

    New changeset 47ffc1a by Berker Peksag (Miss Islington (bot)) in branch '3.7':
    bpo-27682: Handle client connection terminations in wsgiref (GH-9713)
    47ffc1a

    @berkerpeksag
    Copy link
    Member

    Thank you!

    @PetterS
    Copy link
    Mannequin

    PetterS mannequin commented Jul 22, 2019

    The following message can still appear in the log when running the Django development server:

    ----------------------------------------

    Exception happened during processing of request from ('127.0.0.1', 50713)
    Traceback (most recent call last):
      File "d:\python37\Lib\socketserver.py", line 650, in process_request_thread
        self.finish_request(request, client_address)
      File "d:\python37\Lib\socketserver.py", line 360, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "d:\python37\Lib\socketserver.py", line 720, in __init__
        self.handle()
      File "D:\Virtualenvs\***\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
        self.handle_one_request()
      File "D:\Virtualenvs\***\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle_one_request
        self.raw_requestline = self.rfile.readline(65537)
      File "d:\python37\Lib\socket.py", line 589, in readinto
        return self._sock.recv_into(b)
    ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

    This is much better than before but even this message is completely uninteresting as web browsers disconnect from servers all the time. However, this could be better to fix in Django.

    @PetterS
    Copy link
    Mannequin

    PetterS mannequin commented Jul 22, 2019

    Could potentially already be fixed in Django: django/django@7785e03#diff-f6d1c75ec606389da5af6558bf57f171L51

    @PetterS
    Copy link
    Mannequin

    PetterS mannequin commented Jul 22, 2019

    No it is not fixed in Django, but I verified that adding ConnectionAbortedError on this line

    https://github.com/django/django/blob/c6581a40be3bb4c1e13861f0adbb3fe01f09107f/django/core/servers/basehttp.py#L55

    fixes the error.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants