13

I've been trying to use the python websocket-client module to receive and store continuous updates from an exchange. Generally, the script will run smoothly for a day or so before raising the following error: websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

I've looked at the websocket-client source code and apparently the error is being raised in line 92 by the code if not bytes_:. Furthermore, the WebSocketConnectionClosedException is supposed to be raised "If remote host closed the connection or some network error happened".

Can anybody tell me why this is happening, and what I could do to stop or handle it.

4
  • Show us some code please. Commented Jan 22, 2017 at 0:03
  • It means that someone closed the connection. Can you just re-establish it? I think you'd need that for resiliency anyway. Commented Jan 22, 2017 at 0:16
  • 3
    Did you ever find a solution to this problem? I'm running into the same problem. Commented Dec 15, 2017 at 18:04
  • I would like to know how to reproduce this error because this has cost me a lot of money. I think adding a check like: try: ws.close() except: pass finally: self.connect() would work but idk yet Commented May 17, 2021 at 3:05

1 Answer 1

2

Most likely, the remote host closed the connection. You cannot stop it. You can handle it by re-connecting.

People running web servers will implement automatic cleanup to get rid of potentially stale connections. Closing a connection that's been open for 24 hours sounds like a sensible approach. And there's no harm done, because if the client is still interested, it can re-establish the connection. That's also useful to re-authenticate the client, if authentication is required.

On second thought, it might be a network disconnect as well. Some DSL providers used to disconnect and re-assign a new IP every 24 hours, to prevent users from running permanent services on temporarily assigned IP addresses. Don't know if they still do that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.