Repository: gradio-app/gradio
File: gradio/routes.py (Python)

Code before:
                        except EmptyQueue:
                            await asyncio.sleep(check_rate)
                            if time.perf_counter() - last_heartbeat > heartbeat_rate:
                                # Fix this
                                message = HeartbeatMessage()
                                # Need to reset last_heartbeat with perf_counter
                                # otherwise only a single hearbeat msg will be sent
                                # and then the stream will retry leading to infinite queue 😬
                                last_heartbeat = time.perf_counter()

                        if blocks._queue.stopped:
                            message = UnexpectedErrorMessage(
                                message="Server stopped unexpectedly.",
                                success=False,
                            )
                        if message:
                            response = process_msg(message)
                            if response is not None:
                                yield response
                            if (
                                isinstance(message, ProcessCompletedMessage)
                                and message.event_id
                            ):
                                # It's possible that the event_id has already been removed
                                # for example, the user sent two duplicate `/cancel` requests.
                                # The firtst one would have removed the event_id from pending_event_ids_session
                                if (
                                    message.event_id
                                    in (
                                        blocks._queue.pending_event_ids_session[
                                            session_hash
                                        ]
                                    )
                                ):
                                    blocks._queue.pending_event_ids_session[
                                        session_hash
                                    ].remove(message.event_id)
                                if message.msg == ServerMessage.server_stopped or (
                                    message.msg == ServerMessage.process_completed
                                    and (
                                        len(
                                            blocks._queue.pending_event_ids_session[
                                                session_hash
                                            ]
                                        )
                                        == 0
                                    )
                                ):
                                    message = CloseStreamMessage()
                                    response = process_msg(message)
                                    if response is not None:

Review comment (suggestion):
```suggestion
                                # The first one would have removed the event_id from pending_event_ids_session
```

Code after:
                        except EmptyQueue:
                            await asyncio.sleep(check_rate)
                            if time.perf_counter() - last_heartbeat > heartbeat_rate:
                                # Fix this
                                message = HeartbeatMessage()
                                # Need to reset last_heartbeat with perf_counter
                                # otherwise only a single hearbeat msg will be sent
                                # and then the stream will retry leading to infinite queue 😬
                                last_heartbeat = time.perf_counter()

                        if blocks._queue.stopped:
                            message = UnexpectedErrorMessage(
                                message="Server stopped unexpectedly.",
                                success=False,
                            )
                        if message:
                            response = process_msg(message)
                            if response is not None:
                                yield response
                            if (
                                isinstance(message, ProcessCompletedMessage)
                                and message.event_id
                            ):
                                # It's possible that the event_id has already been removed
                                # for example, the user sent two duplicate `/cancel` requests.
                                # The first one would have removed the event_id from pending_event_ids_session
                                if (
                                    message.event_id
                                    in (
                                        blocks._queue.pending_event_ids_session[
                                            session_hash
                                        ]
                                    )
                                ):
                                    blocks._queue.pending_event_ids_session[
                                        session_hash
                                    ].remove(message.event_id)
                                if message.msg == ServerMessage.server_stopped or (
                                    message.msg == ServerMessage.process_completed
                                    and (
                                        len(
                                            blocks._queue.pending_event_ids_session[
                                                session_hash
                                            ]
                                        )
                                        == 0
                                    )
                                ):
                                    message = CloseStreamMessage()
                                    response = process_msg(message)
                                    if response is not None: