Created At: 2026-11-18T07:54:13Z Completed At: 2026-11-18T07:54:18Z Command exited with code 1. Command output: ============================= test session starts ============================== platform linux -- Python 3.12.3, pytest-9.2.0, pluggy-1.7.0 -- /root/telegram-saas/.venv/bin/python3 cachedir: .pytest_cache rootdir: /root/telegram-saas configfile: pyproject.toml plugins: anyio-4.16.0, asyncio-1.5.0 asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function collecting ... collected 1 item tests/integration/test_celery_telegram_tasks.py::test_celery_telegram_update_task_execution FAILED [100%] =================================== FAILURES =================================== __________________ test_celery_telegram_update_task_execution __________________ coro = ._execute at 0x7393dff9f540> def _run_async(coro): """Executes coroutine synchronously even if another event loop is running in the current thread.""" try: asyncio.get_running_loop() # An event loop is already active in this thread (e.g. during tests) with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit(asyncio.run, coro) > return future.result() ^^^^^^^^^^^^^^^ apps/worker/tasks/telegram.py:20: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /usr/lib/python3.12/concurrent/futures/_base.py:456: in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ /usr/lib/python3.12/concurrent/futures/_base.py:401: in __get_result raise self._exception /usr/lib/python3.12/concurrent/futures/thread.py:58: in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /usr/lib/python3.12/asyncio/runners.py:194: in run return runner.run(main) ^^^^^^^^^^^^^^^^ /usr/lib/python3.12/asyncio/runners.py:118: in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /usr/lib/python3.12/asyncio/base_events.py:687: in run_until_complete return future.result() ^^^^^^^^^^^^^^^ apps/worker/tasks/telegram.py:47: in _execute return await service.handle_webhook_update( modules/bots/service.py:210: in handle_webhook_update bot = await self.repo.get_by_id(bot_id) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modules/bots/repository.py:61: in get_by_id res = await self.db.execute(stmt) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/session.py:652: in execute result = await greenlet_spawn( .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:214: in greenlet_spawn result = context.throw(*sys.exc_info()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2555: in execute return self._execute_internal( .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2432: in _execute_internal conn = self._connection_for_bind(bind) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2255: in _connection_for_bind return trans._connection_for_bind(engine, execution_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1282: in _connection_for_bind conn = bind.connect() ^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:3279: in connect return self._connection_cls(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:154: in __init__ self._dbapi_connection = engine.raw_connection() ^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:3303: in raw_connection return self.pool.connect() ^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py:460: in connect return _ConnectionFairy._checkout(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py:1454: in _checkout with util.safe_reraise(): .venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py:166: in __exit__ raise exc_value.with_traceback(exc_tb) .venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py:1392: in _checkout result = pool._dialect._do_ping_w_event( .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:968: in _do_ping_w_event return self.do_ping(dbapi_connection) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:1223: in do_ping dbapi_connection.ping() .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:879: in ping self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:856: in _handle_exception_no_connection super()._handle_exception_no_connection(dbapi, error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:409: in _handle_exception_no_connection raise error.with_traceback(exc_info[2]) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:877: in ping _ = await_(self._async_ping()) ^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:886: in _async_ping await tr.start() .venv/lib/python3.12/site-packages/asyncpg/transaction.py:146: in start await self._connection.execute(query) .venv/lib/python3.12/site-packages/asyncpg/connection.py:377: in execute result = await self._protocol.query(query, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > ??? E RuntimeError: Task ._execute() running at /root/telegram-saas/apps/worker/tasks/telegram.py:47> cb=[_run_until_complete_cb() at /usr/lib/python3.12/asyncio/base_events.py:182]> got Future attached to a different loop asyncpg/protocol/protocol.pyx:341: RuntimeError During handling of the above exception, another exception occurred: db_session = @pytest.mark.asyncio async def test_celery_telegram_update_task_execution(db_session: AsyncSession): # 1. Create Tenant tenant_id = uuid.uuid4() tenant = Tenant( id=tenant_id, name="Celery Task Tenant", slug=f"celery-tenant-{uuid.uuid4().hex[:6]}", status="active", plan_id="enterprise", ) db_session.add(tenant) await db_session.commit() # 2. Register mock bot unique_bot_name = f"celery_bot_{uuid.uuid4().hex[:6]}" token = f"celery_test_token_{uuid.uuid4().hex[:8]}_test" TelegramApiService.register_mock_bot(token, unique_bot_name, "Celery Bot") service = BotService(db_session) bot = await service.register_bot( tenant_id=tenant_id, req=BotRegisterRequest(token=token, name="Celery Bot", default_locale="en"), ) await db_session.commit() # 3. Setup known webhook secret known_secret = "secret_for_celery_task_verification_123" webhook = await service.repo.get_webhook(bot.id) webhook.secret_hash = hash_webhook_secret(known_secret) await db_session.commit() # 4. Invoke Celery Task update_payload = { "update_id": 8801, "message": { "message_id": 70, "from": { "id": 554433, "first_name": "CeleryUser", "username": "celery_user", "language_code": "en", }, "chat": {"id": 554433}, "text": "/start", }, } result = process_telegram_update_task( bot_id_str=str(bot.id), received_secret=known_secret, update_dict=update_payload, tenant_id_str=str(tenant_id), ) assert result.get("ok") is True assert result.get("processed") is True # 5. Invoke again with duplicate update_id -> must be detected as duplicate > dup_result = process_telegram_update_task( bot_id_str=str(bot.id), received_secret=known_secret, update_dict=update_payload, tenant_id_str=str(tenant_id), ) tests/integration/test_celery_telegram_tasks.py:72: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/celery/local.py:183: in __call__ return self._get_current_object()(*a, **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/celery/app/task.py:550: in __call__ return self.run(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ apps/worker/tasks/telegram.py:57: in process_telegram_update_task raise self.retry(exc=exc) ^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/celery/app/task.py:889: in retry raise_with_context(exc or Retry('Task can be retried', None)) apps/worker/tasks/telegram.py:54: in process_telegram_update_task return _run_async(_execute()) ^^^^^^^^^^^^^^^^^^^^^^ apps/worker/tasks/telegram.py:23: in _run_async return asyncio.run(coro) ^^^^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ main = ._execute at 0x7393dff9f540> debug = None, loop_factory = None def run(main, *, debug=None, loop_factory=None): """Execute the coroutine and return the result. This function runs the passed coroutine, taking care of managing the asyncio event loop, finalizing asynchronous generators and closing the default executor. This function cannot be called when another asyncio event loop is running in the same thread. If debug is True, the event loop will be run in debug mode. This function always creates a new event loop and closes it at the end. It should be used as a main entry point for asyncio programs, and should ideally only be called once. The executor is given a timeout duration of 5 minutes to shutdown. If the executor hasn't finished within that duration, a warning is emitted and the executor is closed. Example: async def main(): await asyncio.sleep(1) print('hello') asyncio.run(main()) """ if events._get_running_loop() is not None: # fail fast with short traceback > raise RuntimeError( "asyncio.run() cannot be called from a running event loop") E RuntimeError: asyncio.run() cannot be called from a running event loop /usr/lib/python3.12/asyncio/runners.py:190: RuntimeError ----------------------------- Captured stdout call ----------------------------- 2026-11-18 07:54:17 [info ] bot_registered_successfully bot_id=8d776121-0d6c-4932-9caf-b5f20387bf6e tenant_id=6eb2684f-be76-4417-93e1-13b4750f3918 username=celery_bot_48e514 2026-11-18 07:54:17 [info ] processing_telegram_update_in_celery bot_id=8d776121-0d6c-4932-9caf-b5f20387bf6e tenant_id=6eb2684f-be76-4417-93e1-13b4750f3918 2026-11-18 07:54:17 [info ] initializing_db_engine max_overflow=5 pool_size=10 2026-11-18 07:54:17 [info ] processing_telegram_update_in_celery bot_id=8d776121-0d6c-4932-9caf-b5f20387bf6e tenant_id=6eb2684f-be76-4417-93e1-13b4750f3918 2026-11-18 07:54:17 [error ] celery_telegram_update_failed bot_id=8d776121-0d6c-4932-9caf-b5f20387bf6e error='asyncio.run() cannot be called from a running event loop' ------------------------------ Captured log call ------------------------------- ERROR sqlalchemy.pool.impl.AsyncAdaptedQueuePool:base.py:389 Exception terminating connection > Traceback (most recent call last): File "asyncpg/protocol/protocol.pyx", line 585, in asyncpg.protocol.protocol.BaseProtocol.close File "asyncpg/protocol/protocol.pyx", line 677, in _drain_cancels File "asyncpg/protocol/protocol.pyx", line 624, in asyncpg.protocol.protocol.BaseProtocol._request_cancel File "/root/telegram-saas/.venv/lib/python3.12/site-packages/asyncpg/connection.py", line 1712, in _cancel_current_command self._cancellations.add(self._loop.create_task( ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/asyncio/base_events.py", line 456, in create_task self._check_closed() File "/usr/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/root/telegram-saas/.venv/lib/python3.12/site-packages/asyncpg/connection.py", line 1536, in close await self._protocol.close(timeout) File "asyncpg/protocol/protocol.pyx", line 610, in close File "asyncpg/protocol/protocol.pyx", line 697, in asyncpg.protocol.protocol.BaseProtocol._handle_waiter_on_connection_lost File "/usr/lib/python3.12/asyncio/base_events.py", line 795, in call_soon self._check_closed() File "/usr/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/root/telegram-saas/.venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 370, in _close_connection self._dialect.do_terminate(connection) File "/root/telegram-saas/.venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 1190, in do_terminate dbapi_connection.terminate() File "/root/telegram-saas/.venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py", line 440, in terminate await_(asyncio.shield(self._terminate_graceful_close())) File "/root/telegram-saas/.venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py", line 171, in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/telegram-saas/.venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py", line 209, in greenlet_spawn value = await result ^^^^^^^^^^^^ File "/root/telegram-saas/.venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 955, in _terminate_graceful_close await self._connection.close(timeout=2) File "/root/telegram-saas/.venv/lib/python3.12/site-packages/asyncpg/connection.py", line 1539, in close self._abort() File "/root/telegram-saas/.venv/lib/python3.12/site-packages/asyncpg/connection.py", line 1600, in _abort self._protocol.abort() File "asyncpg/protocol/protocol.pyx", line 567, in asyncpg.protocol.protocol.BaseProtocol.abort File "/usr/lib/python3.12/asyncio/sslproto.py", line 250, in abort self._force_close(None) File "/usr/lib/python3.12/asyncio/sslproto.py", line 255, in _force_close self._ssl_protocol._abort(exc) File "/usr/lib/python3.12/asyncio/sslproto.py", line 671, in _abort self._transport._force_close(exc) File "/usr/lib/python3.12/asyncio/selector_events.py", line 906, in _force_close self._loop.call_soon(self._call_connection_lost, exc) File "/usr/lib/python3.12/asyncio/base_events.py", line 795, in call_soon self._check_closed() File "/usr/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed =========================== short test summary info ============================ FAILED tests/integration/test_celery_telegram_tasks.py::test_celery_telegram_update_task_execution ============================== 1 failed in 0.78s =============================== sys:1: RuntimeWarning: coroutine 'Connection._cancel' was never awaited