aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.67k stars 1.01k forks source link

`chalice local` not handle websocket error 54 or it not correctly handle socket closure #2039

Open R0uter opened 1 year ago

R0uter commented 1 year ago

When a Chalice project including websocket api, run it locally, it works fine except client closing the socket will trigger an error like this:

  File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 683, in process_request_thread
    self.finish_request(request, client_address)
  File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "[masked]/venv/lib/python3.9/site-packages/chalice/local.py", line 585, in __init__
    BaseHTTPRequestHandler.__init__(
  File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 747, in __init__
    self.handle()
  File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/server.py", line 435, in handle
    self.handle_one_request()
  File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/server.py", line 401, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

In my react.js client, it was correctly closed the socket by calling mysocket.close()

Not sure how to fix this but in my experience, we should handle this exception ConnectionResetError since it not triggering any error in the aws api gateway, and it is quite a common action that the client-side drop the connection.

Please leave a comment if you guys need further information 😬

davidfortuoso-ti commented 1 year ago

Not related to your question, but how did you manage to connect to the websocket while running on chalice local?

R0uter commented 1 year ago

Not related to your question, but how did you manage to connect to the websocket while running on chalice local?

U don't, u need to deploy chalice first, then local debug. The websocket service between client and chalice is up in aws.🤔