Skip to content

Fix web UI session files not being deleted on shutdown#2542

Merged
Silverarmor merged 4 commits intospotDL:devfrom
HarshitR2004:fix/web-ui-session-cleanup
Nov 15, 2025
Merged

Fix web UI session files not being deleted on shutdown#2542
Silverarmor merged 4 commits intospotDL:devfrom
HarshitR2004:fix/web-ui-session-cleanup

Conversation

@HarshitR2004
Copy link

Description

The session cleanup was not working after shutting down the web UI, even when the configuration had:

  • web_use_output_dir = False
  • keep_sessions = False

This issue was caused by the deprecated @app.on_event("shutdown") in FastAPI (FastAPI Startup and Shutdown Events Documentation)

Solution

# Add signal handlers for graceful shutdown
def handle_shutdown(signum, frame):
    app_state.server.should_exit = True

signal.signal(signal.SIGINT, handle_shutdown)
signal.signal(signal.SIGTERM, handle_shutdown)

# Start the web server
try:
    app_state.loop.run_until_complete(app_state.server.serve())
finally:
    if (
        not app_state.web_settings["keep_sessions"]
        and not app_state.web_settings["web_use_output_dir"]
    ):
        sessions_dir = Path(get_spotdl_path() / "web/sessions")
        logger.info("Removing sessions directories")
        if sessions_dir.exists():
            shutil.rmtree(sessions_dir)
  1. Signal-based shutdown: Handles SIGINT and SIGTERM to trigger a graceful server exit.
  2. Server run: Starts the Uvicorn server asynchronously.
  3. Guaranteed cleanup: Uses a finally block to always delete the web/sessions directory if keep_sessions and web_use_output_dir are False.

Note: The existing shutdown method (@router.on_event("shutdown")) has been retained for backward compatibility.

Related Issue

Closes #2272

Testing

Steps:

  1. Started the server and launched the web UI with:
    • web_use_output_dir = False
    • keep_sessions = False
  2. Downloaded a song and verified its existence in the web/sessions directory.
  3. Shut down the server using Ctrl + C.
  4. Confirmed the log message: "Removing sessions directories".
  5. Verified that all session folders were deleted.

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have read the CONTRIBUTING document

@Silverarmor Silverarmor changed the base branch from master to dev October 19, 2025 23:57
@Silverarmor Silverarmor requested a review from Copilot October 19, 2025 23:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

logger.info("Starting web server \n")

# Add signal handlers for graceful shutdown
def handle_shutdown(signum, frame):
Copy link

Copilot AI Oct 19, 2025

Choose a reason for hiding this comment

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

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.

Suggested change
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.
"""

Copilot uses AI. Check for mistakes.
@Silverarmor
Copy link
Member

Ready for merge EXCEPT isort is failing. Please run all linting as instructed in CONTRIBUTING.md.

@Silverarmor Silverarmor merged commit b20a46b into spotDL:dev Nov 15, 2025
1 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session files in web UI arent deleted on shutdown

3 participants