python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.07k stars 506 forks source link

Force a ConnectionClosed to test client listener #1380

Closed FrankC01 closed 12 months ago

FrankC01 commented 12 months ago

I'd like to test my exception handling:

            async for websock in ws_connect(
                self.config.socket_url,
                extra_headers=self._ADDITIONL_HEADER,
                ssl=ssl.SSLContext(ssl.PROTOCOL_SSLv23),
            ):
                try:
                    # ...
                 except ws_connect.ConnectionClosed as ws_cc:
                    if continue_on_close:
                        logger.warning(
                            "Subscription listener ConnectionClosed... reconnecting"
                        )
                        continue
                    else:
                        logger.error(
                            f"Subscription listener ConnectionClosed occured for shutdown -> {self._in_shutdown} {ws_cc.args}"
                        )
                        return SuiRpcResult(False, "ConnectionClosed", ws_cc)

Any suggestions?

aaugustin commented 12 months ago

Close the connection (from another Task) with websock.close()?

FrankC01 commented 12 months ago

The client gave me the URL that was so finicky I was able to test with their wss URL which suffered 6 ConnectionClosed in 1 hour and kept running (going on 24 hrs now with no issue)!

Another question: are previous connections removed from memory?

aaugustin commented 12 months ago

That's managed by Python's garbage collector. If your code doesn't reference them anymore, then they're garbage collected.