Fix web UI session files not being deleted on shutdown#2542
Fix web UI session files not being deleted on shutdown#2542Silverarmor merged 4 commits intospotDL:devfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where session files were not being cleaned up when shutting down the web UI, despite having web_use_output_dir = False and keep_sessions = False configured. The issue stemmed from the deprecated FastAPI @app.on_event("shutdown") not executing properly.
Key Changes:
- Added signal handlers (SIGINT, SIGTERM) for graceful shutdown detection
- Wrapped server execution in try-finally block to guarantee cleanup
- Added explicit session directory removal logic in the finally block
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
spotdl/console/web.py
Outdated
| logger.info("Starting web server \n") | ||
|
|
||
| # Add signal handlers for graceful shutdown | ||
| def handle_shutdown(signum, frame): |
There was a problem hiding this comment.
The handle_shutdown function lacks a docstring explaining its purpose as a signal handler. Consider adding a docstring to document the parameters (signum, frame) and its role in graceful shutdown.
| def handle_shutdown(signum, frame): | |
| def handle_shutdown(signum, frame): | |
| """ | |
| Signal handler to gracefully shut down the web server. | |
| Args: | |
| signum (int): The signal number received (e.g., SIGINT, SIGTERM). | |
| frame (FrameType): The current stack frame (unused). | |
| Sets the server's should_exit flag to True, initiating a graceful shutdown. | |
| """ |
|
Ready for merge EXCEPT isort is failing. Please run all linting as instructed in CONTRIBUTING.md. |
Description
The session cleanup was not working after shutting down the web UI, even when the configuration had:
web_use_output_dir = Falsekeep_sessions = FalseThis issue was caused by the deprecated
@app.on_event("shutdown")in FastAPI (FastAPI Startup and Shutdown Events Documentation)Solution
SIGINTandSIGTERMto trigger a graceful server exit.finallyblock to always delete theweb/sessionsdirectory ifkeep_sessionsandweb_use_output_dirareFalse.Related Issue
Closes #2272
Testing
Steps:
web_use_output_dir = Falsekeep_sessions = Falseweb/sessionsdirectory.Ctrl + C.Types of Changes
Checklist