Skip to content

Optimize lifespan middleware #5220

Closed

Description

@masenf

Describe the bug
Instead of creating a new "top_level_asgi" Starlette instance to handle the lifespan tasks after applying api_transformer, we could use a lighterweight and more generic middleware at the top level who's only purpose in life is to handle the lifespan.

Related to #5218

The current fix is general and workable, but not efficient as it could be.

Specifics (please complete the following information):

  • Python Version: 3.13
  • Reflex Version: 0.7.9

Activity

  1. linear commented on Apr 30, 2025

    @linear
  2. added
    enhancement
    on Apr 30, 2025
  3. vikas-gits-good commented on Oct 13, 2025

    @vikas-gits-good

    Hi, I'm running into issues related to this lifespan thing. I tried uninstalling uvicorn and also downgraded to python 3.11.11 doesnt help. Im working on a portfolio project to put on my resume. Please help.

    ` bash
    File ".../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/.venv/lib/python3.11/site-packages/reflex/app_mixins/lifespan.py", line 35, in _run_lifespan_tasks
    for task in self.lifespan_tasks:
    TypeError: 'BaseStateMeta' object is not iterable

    ERROR: Application startup failed. Exiting.`

    Here is the full debug log
    `bash
    Debug: Overriding config value loglevel with env var LOGLEVEL=debug
    ───────────────────────────────────────────────────────── System Info ──────────────────────────────────────────────────────────
    Debug: Config file:
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/.venv/lib/python3.11/site-packages/reflex/co
    nfig.py'
    Debug: Config: Config(app_name='Frontend', app_module_import='Frontend.test4', loglevel=<LogLevel.DEBUG: 'debug'>,
    frontend_port=None, frontend_path='', backend_port=None, api_url='http://localhost:8000', deploy_url='http://localhost:3000',
    backend_host='0.0.0.0', db_url='sqlite:///reflex.db', async_db_url=None, redis_url=None, telemetry_enabled=False,
    enable_pyleak_monitoring=False, pyleak_blocking_threshold=0.1, pyleak_thread_grace_period=0.2, pyleak_action=None,
    bun_path=PosixPath('.../.local/share/reflex/bun/bin/bun'), static_page_generation_timeout=60, cors_allowed_origins=('*',),
    react_strict_mode=True, frontend_packages=[], state_manager_mode=<StateManagerMode.DISK: 'disk'>, redis_lock_expiration=10000,
    redis_lock_warning_threshold=1000, redis_token_expiration=3600, _non_default_attributes={'plugins', 'telemetry_enabled',
    'app_name', 'app_module_import', 'loglevel'}, env_file=None, state_auto_setters=None, show_built_with_reflex=None,
    is_reflex_cloud=False, extra_overlay_function=None, plugins=[SitemapPlugin(), TailwindV4Plugin(config={'plugins':
    ['@tailwindcss/[email protected]']})], disable_plugins=[])
    Debug: Running command: [PosixPath('/usr/bin/node'), '--version']
    Debug: Running command: [PosixPath('.../.local/share/reflex/bun/bin/bun'), '--version']
    Debug: [Reflex 0.8.14.post1 with Python 3.11.11 (PATH:
    .../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/.venv/bin/python3)]
    Debug: [Node 24.5.0 (Minimum: 20.19.0) (PATH:/usr/bin/node)]
    Debug: [Bun 1.2.20 (Minimum: 1.2.17) (PATH: .../.local/share/reflex/bun/bin/bun)]
    Debug: [OS Linux Manjaro Linux]
    Debug: Using package installer at: ['.../.local/share/reflex/bun/bin/bun', '/usr/bin/npm']
    Debug: Using package executer at: [['.../.local/share/reflex/bun/bin/bun'], ['/usr/bin/npm']]
    Debug: Unzip path: /usr/bin/unzip
    Debug: Resetting disk state manager.
    Debug: Checking if Frontend port: 3000 is in use.
    Debug: Checking if Frontend port: 3000 is in use for families: [<AddressFamily.AF_INET: 2>, <AddressFamily.AF_INET6: 10>].
    Debug: Frontend port: 3000 is not in use.
    Debug: Checking if Backend port: 8000 is in use.
    Debug: Checking if Backend port: 8000 is in use for families: [<AddressFamily.AF_INET: 2>, <AddressFamily.AF_INET6: 10>].
    Debug: Backend port: 8000 is not in use.
    Debug: Overriding config value frontend_port with env var FRONTEND_PORT=3000
    Debug: Overriding config value backend_port with env var BACKEND_PORT=8000
    ───────────────────────────────────────────────────── Starting Reflex App ──────────────────────────────────────────────────────
    Debug: Checking for the latest version of reflex.../
    Debug: Sending HTTPS request to https://pypi.org/pypi/reflex/json
    Debug: Received response from https://pypi.org/pypi/reflex/json in 0.082 seconds
    Debug: Latest version of reflex: 0.8.14.post1
    Warning: Using Uvicorn for backend as it is installed. This behavior will change in 0.8.0 to use Granian by default.
    2025-10-13 16:43:26.746171: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
    2025-10-13 16:43:26.819119: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
    To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
    2025-10-13 16:43:28.464729: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
    [16:43:29] Compiling: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0% 0/21 0:00:00Debug: Evaluating page: index
    Debug: Evaluating page: 404
    Debug: Slowest pages:
    index: 39.1ms
    404: 0.5ms
    Debug: [timing] Evaluate Pages (Frontend): 0.05s
    Debug: [timing] Collect all imports and app wraps: 0.00s
    Debug: [timing] Auto-memoize StatefulComponents: 0.01s
    [16:43:29] Compiling: ━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━ 57% 12/21 0:00:00Debug: [timing] Copy assets: 0.00s
    [16:43:29] Compiling: ━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━ 57% 12/21 0:00:00Debug: [timing] Compile to Javascript: 0.15s
    [16:43:29] Compiling: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 21/21 0:00:00
    Debug: Running command: ['.../.local/share/reflex/bun/bin/bun', 'install', '--legacy-peer-deps']
    Debug: Installing base frontend packages
    Debug: bun install v1.2.20 (6ad208bc)
    Debug:
    Debug: Checked 388 installs across 425 packages (no changes) [28.00ms]
    Debug: Running command: ['.../.local/share/reflex/bun/bin/bun', 'add', '--legacy-peer-deps', '-d',
    '@tailwindcss/[email protected]', '@tailwindcss/[email protected]', '[email protected]']
    Debug: Installing frontend development dependencies
    Debug: bun add v1.2.20 (6ad208bc)
    Debug: Saved lockfile
    Debug:
    Debug: installed @tailwindcss/[email protected]
    Debug: installed @tailwindcss/[email protected]
    Debug: installed [email protected]
    Debug:
    Debug: [33.00ms] done
    Debug: Running command: ['.../.local/share/reflex/bun/bin/bun', 'add', '--legacy-peer-deps', '[email protected]',
    '[email protected]', '[email protected]', '[email protected]', '@radix-ui/[email protected]', '[email protected]']
    Debug: Installing frontend packages from config and components
    Debug: bun add v1.2.20 (6ad208bc)
    Debug: Saved lockfile
    Debug:
    Debug: installed [email protected]
    Debug: installed [email protected]
    Debug: installed [email protected]
    Debug: installed [email protected]
    Debug: installed @radix-ui/[email protected]
    Debug: installed [email protected]
    Debug:
    Debug: [26.00ms] done
    Debug: [timing] Install Frontend Packages: 0.14s
    Debug: [timing] Write to Disk: 0.00s
    ───────────────────────────────────────────────────────── App Running ──────────────────────────────────────────────────────────
    Debug: Running command: ['.../.local/share/reflex/bun/bin/bun', 'run', 'dev']
    Debug: Starting frontend
    Debug: $ react-router dev --host
    Debug: Reload paths: ['.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/README.md',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/requirements.txt',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/app.py',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/main.py',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/push_data.py',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/setup.py',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/src',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/logs',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/P_1_Real_IPO_Listing_Price_Prediction.egg-in
    fo', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/Artifacts',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/catboost_info',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/mlruns',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/assets',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/rxconfig.py',
    '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/Frontend']
    INFO: Will watch for changes in these directories: ['.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/Artifacts', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/Frontend', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/P_1_Real_IPO_Listing_Price_Prediction.egg-info', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/assets', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/catboost_info', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/logs', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/mlruns', '.../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/src']
    INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
    INFO: Started reloader process [34495] using WatchFiles
    Debug: ➜ Local: http://localhost:3000/
    App running at: http://localhost:3000/
    Backend running at: http://0.0.0.0:8000
    Debug: ➜ Network: http://192.168.31.135:3000/
    Debug: ➜ press h + enter to show help
    2025-10-13 16:43:35.500263: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
    2025-10-13 16:43:35.601877: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
    To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
    2025-10-13 16:43:38.258051: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
    Debug: Overriding config value loglevel with env var LOGLEVEL=debug
    Debug: Overriding config value frontend_port with env var FRONTEND_PORT=3000
    Debug: Overriding config value backend_port with env var BACKEND_PORT=8000
    INFO: Started server process [34564]
    INFO: Waiting for application startup.
    ERROR: Traceback (most recent call last):
    File ".../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/.venv/lib/python3.11/site-packages/starlette/routing.py", line 694, in lifespan
    async with self.lifespan_context(app) as maybe_state:
    File ".../.local/share/uv/python/cpython-3.11.11-linux-x86_64-gnu/lib/python3.11/contextlib.py", line 210, in aenter
    return await anext(self.gen)
    ^^^^^^^^^^^^^^^^^^^^^
    File ".../Coding/DS_ML_AI_Projects/P-1_Real_IPO-Listing-Price-Prediction/.venv/lib/python3.11/site-packages/reflex/app_mixins/lifespan.py", line 35, in _run_lifespan_tasks
    for task in self.lifespan_tasks:
    TypeError: 'BaseStateMeta' object is not iterable

    ERROR: Application startup failed. Exiting.`

  4. reopened this on Oct 20, 2026
  5. linear-code commented on Oct 20, 2026

    @linear-code
  6. added a commit that references this issue on Oct 28, 2026
    1191a38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions