def heartbeat(
            session_hash: str,
            request: fastapi.Request,
            background_tasks: BackgroundTasks,
            username: str = Depends(get_current_user),
        ):
            """Clients make a persistent connection to this endpoint to keep the session alive.
            When the client disconnects, the session state is deleted.
            """
            heartbeat_rate = 0.25 if os.getenv("GRADIO_IS_E2E_TEST", None) else 15

            async def iterator():
                stop_stream_task = asyncio.create_task(app.stop_event.wait())
                while True:
                    try:
                        yield "data: ALIVE\n\n"
                        # We need to close the heartbeat connections as soon as the server stops
                        # otherwise the server can take forever to close
                        wait_task = asyncio.create_task(asyncio.sleep(heartbeat_rate))
                        done, _ = await asyncio.wait(
                            [wait_task, stop_stream_task],
                            return_when=asyncio.FIRST_COMPLETED,
                        )
                        if stop_stream_task in done:
                            raise asyncio.CancelledError()
                    except asyncio.CancelledError:
                        if not stop_stream_task.done():
                            stop_stream_task.cancel()

                        req = Request(request, username, session_hash=session_hash)
                        root_path = route_utils.get_root_url(
                            request=request,
                            route_path=f"{API_PREFIX}/heartbeat/{session_hash}",
                            root_path=app.root_path,
                        )
                        body = PredictBodyInternal(
                            session_hash=session_hash, data=[], request=request
                        )
                        unload_fn_indices = [
                            i
                            for i, dep in app.get_blocks().fns.items()
                            if any(t for t in dep.targets if t[1] == "unload")
                        ]
                        for fn_index in unload_fn_indices:
                            # The task running this loop has been cancelled
                            # so we add tasks in the background
                            background_tasks.add_task(
                                route_utils.call_process_api,
                                app=app,
                                body=body,
                                gr_request=req,
                                fn=app.get_blocks().fns[fn_index],
                                root_path=root_path,
                            )
                        # This will mark the state to be deleted in an hour
                        if session_hash in app.state_holder.session_data:
                            app.state_holder.session_data[session_hash].is_closed = True
                        for (
                            event_id
                        ) in app.get_blocks()._queue.pending_event_ids_session.get(
                            session_hash, []
                        ):
                            event = app.get_blocks()._queue.event_ids_to_events[
                                event_id
                            ]
                            event.run_time = math.inf
                            event.signal.set()
                        return

            return StreamingResponse(iterator(), media_type="text/event-stream")
Clients make a persistent connection to this endpoint to keep the session alive.
            When the client disconnects, the session state is deleted.
            
gradio/routes.py