Skip to content

Commit

Permalink
fix(langflow): handle KeyboardInterrupt and terminate process in run …
Browse files Browse the repository at this point in the history
…function for graceful shutdown (#2551)

* fix(langflow): handle KeyboardInterrupt and terminate process in run function for graceful shutdown

* refactor(langflow): remove unnecessary try-except block and sys.exit calls in run_langflow function

* fix(langflow): handle exceptions properly and exit with appropriate status codes

* refactor: update multiprocess imports in langflow/__main__.py

Adds type ignore
  • Loading branch information
ogabrielluiz authored Jul 5, 2024
1 parent 4954b3f commit e536272
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/backend/base/langflow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import httpx
import typer
from dotenv import dotenv_values, load_dotenv
from multiprocess import Process, cpu_count # type: ignore
from multiprocess import cpu_count # type: ignore
from multiprocess.context import Process # type: ignore
from packaging import version as pkg_version
from rich import box
from rich import print as rprint
Expand Down Expand Up @@ -181,6 +182,7 @@ def run(
# Define an env variable to know if we are just testing the server
if "pytest" in sys.modules:
return
process: Process | None = None
try:
if platform.system() in ["Windows"]:
# Run using uvicorn on MacOS and Windows
Expand All @@ -195,7 +197,12 @@ def run(
if process:
process.join()
except KeyboardInterrupt:
pass
if process is not None:
process.terminate()
sys.exit(0)
except Exception as e:
logger.exception(e)
sys.exit(1)


def wait_for_server_ready(host, port):
Expand Down Expand Up @@ -417,29 +424,24 @@ def run_langflow(host, port, log_level, options, app):
"""
Run Langflow server on localhost
"""
try:
if platform.system() in ["Windows"]:
# Run using uvicorn on MacOS and Windows
# Windows doesn't support gunicorn
# MacOS requires an env variable to be set to use gunicorn
import uvicorn

uvicorn.run(
app,
host=host,
port=port,
log_level=log_level.lower(),
loop="asyncio",
)
else:
from langflow.server import LangflowApplication

LangflowApplication(app, options).run()
except KeyboardInterrupt:
pass
except Exception as e:
logger.exception(e)
sys.exit(1)
if platform.system() in ["Windows"]:
# Run using uvicorn on MacOS and Windows
# Windows doesn't support gunicorn
# MacOS requires an env variable to be set to use gunicorn
import uvicorn

uvicorn.run(
app,
host=host,
port=port,
log_level=log_level.lower(),
loop="asyncio",
)
else:
from langflow.server import LangflowApplication

LangflowApplication(app, options).run()


@app.command()
Expand Down

0 comments on commit e536272

Please sign in to comment.