Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SIGTERM handling and update typer dependency #4548

Merged
merged 5 commits into from
Nov 13, 2024
Merged

Conversation

ogabrielluiz
Copy link
Contributor

@ogabrielluiz ogabrielluiz commented Nov 12, 2024

This pull request includes several updates to the src/backend/base/langflow/__main__.py file to improve error handling and code readability, as well as a dependency update in pyproject.toml.

Improvements to error handling:

  • Updated the KeyboardInterrupt and Exception handling in the run function to use typer.Exit for cleaner exits and added handling for SystemExit to ensure the process is terminated gracefully.
  • Added a try-except block around the main function call to log exceptions and exit gracefully using typer.Exit.

Code readability improvements:

  • Combined import statements from langflow.services.database.models.folder.utils and langflow.services.database.models.api_key.crud into single lines. [1] [2]

Dependency updates:

  • Updated typer dependency version from 0.12.0 to 0.13.0 in pyproject.toml.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request labels Nov 12, 2024
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Nov 12, 2024
- Introduce signal handlers to manage SIGTERM and SIGINT for graceful server shutdown.
- Update exception handling to ensure processes terminate properly and log shutdown events.
- Modify server run logic to support signal-based shutdowns, improving reliability.
Copy link

codspeed-hq bot commented Nov 12, 2024

CodSpeed Performance Report

Merging #4548 will degrade performances by 17.02%

Comparing update-typer (bb03866) with main (335b649)

Summary

⚡ 1 improvements
❌ 1 regressions
✅ 13 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main update-typer Change
test_invalid_run_with_input_type_chat 16.5 ms 13.8 ms +19.35%
test_successful_run_with_output_type_debug 378.3 ms 455.9 ms -17.02%

Copy link
Collaborator

@jordanrfrazier jordanrfrazier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Built image and ran docker stop <container> and see shutdown:

~/Documents/langflow/langflow (update-typer ✗) docker run --init --rm -p 7860:7860 test:latest
Starting Langflow v1.0.19.post2...
/app/.venv/lib/python3.12/site-packages/pydantic/_internal/_config.py:291: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.9/migration/
  warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
/app/.venv/lib/python3.12/site-packages/langchain_community/retrievers/kendra.py:379: PydanticDeprecatedSince20: Pydantic V1 style `@validator` validators are deprecated. You should migrate to Pydantic V2 style `@field_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.9/migration/
  @validator("top_k")
╭───────────────────────────────────────────────────────────────────╮
│ Welcome to ⛓ Langflow                                             │
│                                                                   │
│                                                                   │
│ Collaborate, and contribute at our GitHub Repo 🌟                 │
│                                                                   │
│ We collect anonymous usage data to improve Langflow.              │
│ You can opt-out by setting DO_NOT_TRACK=true in your environment. │
│                                                                   │
│ Access http://0.0.0.0:7860                                        │
╰───────────────────────────────────────────────────────────────────╯

Langflow shutdown complete
[11/12/24 21:14:41] ERROR    2024-11-12 21:14:41 - ERROR    -   __init__.py:1762
                             __init__ - Worker (pid:30) was                     
                             sent SIGTERM!      

Though, sending SIGTERM to the uv process from make backend just terminates a local process. I think that's okay (though, not what I'd expect?)

(.venv) ~/Documents/langflow/langflow (update-typer ✗) make backend
Installing backend dependencies
Audited 452 packages in 0.67ms
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Running backend respecting the .env file
uv run uvicorn \
		--factory langflow.main:create_app \
		--host 0.0.0.0 \
		--port 7860 \
		 --reload \
		--env-file .env \
		--loop asyncio \
		--workers 1
INFO:     Will watch for changes in these directories: ['/Users/jordan.frazier/Documents/Langflow/langflow']
INFO:     Loading environment from '.env'
INFO:     Uvicorn running on http://0.0.0.0:7860 (Press CTRL+C to quit)
INFO:     Started reloader process [78236] using WatchFiles
Starting Langflow v1.0.19.post2...
/Users/jordan.frazier/Documents/Langflow/langflow/.venv/lib/python3.11/site-packages/pydantic/_internal/_config.py:291: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.9/migration/
  warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
/Users/jordan.frazier/Documents/Langflow/langflow/.venv/lib/python3.11/site-packages/pydantic/_internal/_config.py:341: UserWarning: Valid config keys have changed in V2:
* 'schema_extra' has been renamed to 'json_schema_extra'
  warnings.warn(message, UserWarning)
/Users/jordan.frazier/Documents/Langflow/langflow/.venv/lib/python3.11/site-packages/composio/client/http.py:20: DeprecationWarning: Inheritance class AsyncHttpClient from ClientSession is discouraged
  class AsyncHttpClient(AsyncSession, logging.WithLogger):
make: *** [backend] Terminated: 15

edit: ^ is probably due to makefile process on top of uv

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 12, 2024
@NadirJ NadirJ merged commit f8f9b7c into main Nov 13, 2024
29 checks passed
@NadirJ NadirJ deleted the update-typer branch November 13, 2024 00:42
diogocabral pushed a commit to headlinevc/langflow that referenced this pull request Nov 26, 2024
…#4548)

* Update `typer` dependency to version 0.13.0 in `pyproject.toml`

* refactor: Simplify exception handling in the CLI

* Enhance lifespan function with clean shutdown and logging improvements

* Add graceful shutdown handling for SIGTERM and SIGINT signals

- Introduce signal handlers to manage SIGTERM and SIGINT for graceful server shutdown.
- Update exception handling to ensure processes terminate properly and log shutdown events.
- Modify server run logic to support signal-based shutdowns, improving reliability.

---------

Co-authored-by: Nadir J <31660040+NadirJ@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants