Skip to content

Fix: lfx serve asyncio event loop error#10887

Merged
HzaRashid merged 3 commits into
mainfrom
fix/lfx-serve
Dec 4, 2025
Merged

Fix: lfx serve asyncio event loop error#10887
HzaRashid merged 3 commits into
mainfrom
fix/lfx-serve

Conversation

@HzaRashid
Copy link
Copy Markdown
Collaborator

@HzaRashid HzaRashid commented Dec 4, 2025

lfx serve [flow-json-file-path] ...] throws the following exception:

✗ Failed to start server: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'Server.serve' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

because the serve_command handler is wrapped in a syncify decorator which creates its own event loop, while uvicorn.run() tries to create another event loop inside of it. This is fixed by starting the server as a coroutine inside the syncify event loop by calling await server.serve() directly

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 4, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The PR refactors the server startup mechanism in the CLI by replacing a direct uvicorn.run() invocation with explicit uvicorn.Server API usage. It constructs a Config object, instantiates a Server, and awaits its serve() method while maintaining configuration parameters and error handling.

Changes

Cohort / File(s) Summary
Server startup refactoring
src/lfx/src/lfx/cli/commands.py
Replaced uvicorn.run() call with explicit Server API: creates uvicorn.Config, instantiates uvicorn.Server(config), and awaits server.serve(). Host, port, and log_level configuration preserved.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify that all necessary parameters (host, port, log_level) are correctly passed to the Config object
  • Confirm error handling remains intact in the new async/await pattern
  • Ensure the Server lifecycle (initialization and serve) is properly managed

Pre-merge checks and finishing touches

Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error, 1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Test Coverage For New Implementations ❌ Error Test file test_serve.py still mocks uvicorn.run() instead of uvicorn.Server API and lacks asyncio fix regression tests. Update test_serve.py to mock uvicorn.Server and add regression tests verifying the asyncio event loop fix works correctly.
Test Quality And Coverage ⚠️ Warning PR fixes critical asyncio event loop error in serve_command by refactoring uvicorn initialization but includes zero test coverage for the async functionality change. Add comprehensive tests: (1) async test with pytest.mark.asyncio for uvicorn.Server initialization, (2) CliRunner test verifying no event loop errors, (3) error scenario tests, (4) validation that original issue is resolved.
Test File Naming And Structure ❓ Inconclusive PR modifies only implementation file (src/lfx/src/lfx/cli/commands.py) with no test files added or modified according to the provided summary. Clarify whether test files were added/modified in this PR. If tests exist, verify they follow naming conventions (test_*.py for Python, *.test.ts/tsx for TypeScript) and proper structure.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix: lfx serve asyncio event loop error' directly and specifically describes the main change: resolving an asyncio event loop conflict in the lfx serve command.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Excessive Mock Usage Warning ✅ Passed PR modifies only CLI command handling; no test files altered, so no excessive mock usage to evaluate.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 4, 2025

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 15%
15.45% (4251/27504) 8.62% (1812/21011) 9.7% (588/6059)

Unit Test Results

Tests Skipped Failures Errors Time
1671 0 💤 0 ❌ 0 🔥 21.309s ⏱️

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 32.54%. Comparing base (21e257a) to head (68daae8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #10887      +/-   ##
==========================================
- Coverage   32.54%   32.54%   -0.01%     
==========================================
  Files        1370     1370              
  Lines       63543    63544       +1     
  Branches     9394     9393       -1     
==========================================
  Hits        20680    20680              
  Misses      41824    41824              
- Partials     1039     1040       +1     
Flag Coverage Δ
backend 51.54% <ø> (-0.03%) ⬇️
frontend 14.30% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/lfx/src/lfx/cli/commands.py 52.90% <ø> (-0.31%) ⬇️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added the lgtm This PR has been approved by a maintainer label Dec 4, 2025
@HzaRashid HzaRashid added this pull request to the merge queue Dec 4, 2025
Merged via the queue into main with commit 8aec1f3 Dec 4, 2025
23 checks passed
@HzaRashid HzaRashid deleted the fix/lfx-serve branch December 4, 2025 22:16
Wallgau pushed a commit that referenced this pull request Dec 16, 2025
* fix lfx serve asyncio event loop error

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants