LeetCodeBot: One Session, Many Commands
Context LeetCodeBot talks to its database through AsyncDatabaseManager, an async context manager that hands out SQLAlchemy sessions: async with self.async_database_manager as db: db.add(row) # leaving the block commits, or rolls back on error main.py creates one manager and shares it across every cog. The old version looked like this: async def __aenter__(self) -> AsyncSession: self.session = self._sessionmaker() return self.session async def __aexit__(self, exc_type, exc_val, exc_tb): try: assert self.session if exc_type: await self.session.rollback() else: await self.session.commit() await self.session.close() except AssertionError: return True finally: return False It has two bugs (#38). Bug 1: __aexit__ commits the wrong session __aenter__ returns a new session, so the caller’s db is bound to that object. But __aexit__ reads self.session, which belongs to the shared manager. ...