Skip to content

Every Vercel cold start imports aiogram, because the webhook module imports it at the top #275

Closed

Description

@lumenpearson

On Vercel every cold start imports aiogram, whatever the request is for. app/main.py:180-184 mounts the webhook when webhook_enabled is true, and imports app.api.telegram to do it; that module imports aiogram at its top (app/api/telegram.py:12-13, from aiogram import Bot, Dispatcher and from aiogram.types import Update). webhook_enabled is bot_token and webhook_secret, and on Vercel get_settings() makes both mandatory — so on the deployment this project ships to, the condition is always true.

The comment above the mount (app/main.py:170-179) says the opposite: that the deferral keeps aiogram off the cold start, and that «the heavy app.bot.bot import inside app/api/telegram.py is already deferred the same way». The inner half is deferred; the outer half is not, because the module's own top-level imports pull aiogram in.

Measured on 3 October 2026, on the owner's machine, importing app.main with Vercel's settings (VERCEL=1, BOT_TOKEN, WEBHOOK_SECRET and the other mandatory settings set to placeholders): 736 aiogram modules loaded, 3.07 s for the import. With app.api.telegram stubbed, aiogram is not loaded at all; the planning survey measured app.main at about 1.05 s without it and aiogram plus aiogram.types at about 2.67 s.

Failure scenario. A phone opens the app in the morning; its GET /api/v1/bundle lands on a fresh Vercel instance. Before the first byte of the timetable, the instance spends roughly three seconds importing a Telegram framework that this request never touches. Every cold start pays it, for the API and the cron tick alike.

#272 is the missing guard that would have caught this; this is the defect itself. The fix is to import aiogram inside the webhook endpoint (types under TYPE_CHECKING), and #272's test then holds it — in both configurations, with and without the webhook mounted.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions