flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
9.43k stars 360 forks source link

OAuth expiry of token will hang fastapi server #3150

Closed skaark closed 1 week ago

skaark commented 2 weeks ago

There is a deadlock situation in sdk/python/packages/flet/src/flet/fastapi/flet_app_manager.py The following code is locked with a non-recursive lock.

async def __evict_expired_oauth_states(self):
        while True:
            await asyncio.sleep(10)
            with self.__states_lock:
                ids = []
                for id, state in self.__states.items():
                    if (
                        state.expires_at
                        and datetime.now(timezone.utc) > state.expires_at
                    ):
                        ids.append(id)
                for id in ids:
                    logger.info(f"Delete expired oauth state: {id}")
                    self.retrieve_state(id)

However, the self.retrieve_state(id) call locks the code again. As the lock is non-recursive, the code dead-locks at this point. You need to either make the lock recursive(RLock() instead of Lock()), or create an internal unprotected version of retrieve_state, which can be called under locking from the public retrieve_state.

FeodorFitsner commented 2 weeks ago

Good catch, thanks!