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

Strange problem, client cannot receive messages from the server, after sending a certain amount of messages. #1457

Closed
killerwhite opened this issue Apr 10, 2024 · 2 comments

Comments

@killerwhite
Copy link

The problem is, after a certain number of messages are sent from the server, the client cannot receive the messages.

I created a websockets server. I called the send_node_data function in the main thread to send a message to the client. At first, everything runs well. The strange thing is that after I sent the message to the client about 700 times in a row, the client no longer received the message sent anymore. At the same time, the server executed the send method without throwing any exception. It just continued to execute normally.

This is my code, I created a new websockets server.

    def __start_thread(self):
        debug_thread = threading.Thread(target=self.__debug_serve, daemon=True)
        debug_thread.setName("websocket_thread")
        debug_thread.start()

    def __debug_serve(self):
        try:
            asyncio.run(self.__start_server())
        except Exception as e:
            LOG.error("server has error: ", e)

    async def __start_server(self):
        self.server = websockets.serve(self.__start,
                                       "127.0.0.1",
                                       self.port,
                                       ping_interval=None,
                                       max_size=None)

        async with self.server:
            await asyncio.Future()  # run forever

    async def __start(self, client, path):
        if self.client is not None:
            return

        self.client = client
        await self.recv_start_request()
        self.lock.set()
        try:
            await self.recv_other_request()
        except websockets.exceptions.ConnectionClosed as e:
            # The client is disconnected and the sys exception is thrown.
            self.lock.set()
            LOG.error("client is closed")
            self._async_raise(threading.main_thread().ident,
                              SystemExit)

And I'm sending a message to the client via send_node_data.

async def send_data(self):
    """send data"""
    await self.client.send(json.dumps(self.response, separators=(',', ':')))

def send_node_data(node):
    if web_socket.response["output"]["status"] == "succeed":
        file_key = get_file_path(node)
        web_socket.response["output"].update({
            "xmlId": node.xml_id,
            "file_path": file_key
        })
    asyncio.run(web_socket.send_data())

I tried adding a delay after sending the message with await asyncio.sleep, but that didn't work either.

@killerwhite
Copy link
Author

The version of websockets I'm using is 11.0.3

@aaugustin
Copy link
Member

Sorry I don't know how to help you with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants