Skip to content

Commit bde4472

Browse files
committed
Run session reconnect asynchronously in FletAppManager
Session reconnect now uses an async task to avoid blocking the websocket receive loop with user handlers. Errors during reconnect are logged and reported to the session if possible.
1 parent 850c90f commit bde4472

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

sdk/python/packages/flet-web/src/flet_web/fastapi/flet_app_manager.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,27 @@ async def reconnect_session(self, session_id: str, conn: Connection):
8181
logger.info(f"Session reconnected: {session_id}")
8282
if session_id in self.__sessions:
8383
session = self.__sessions[session_id]
84-
await session.connect(conn)
84+
85+
# Run connect asynchronously so websocket receive loop isn't blocked by
86+
# user handlers (e.g., on_connect invoking _invoke_method).
87+
88+
async def _connect():
89+
try:
90+
await session.connect(conn)
91+
except Exception as e:
92+
logger.error(
93+
f"Unhandled error reconnecting session {session_id}: {e}",
94+
exc_info=True,
95+
)
96+
try:
97+
session.error(str(e))
98+
except Exception:
99+
logger.error(
100+
"Failed to report reconnect error to session",
101+
exc_info=True,
102+
)
103+
104+
asyncio.create_task(_connect())
85105
else:
86106
raise RuntimeError(f"Session has expired or not found: {session_id}")
87107

0 commit comments

Comments
 (0)