Skip to content

fix: handle SIGTERM in serve command to clean up temp site_dir#4162

Open
Mubashir78 wants to merge 1 commit into
mkdocs:masterfrom
Mubashir78:fix/sigterm-cleanup-serve
Open

fix: handle SIGTERM in serve command to clean up temp site_dir#4162
Mubashir78 wants to merge 1 commit into
mkdocs:masterfrom
Mubashir78:fix/sigterm-cleanup-serve

Conversation

@Mubashir78

Copy link
Copy Markdown

Fix #3905

Problem

When mkdocs serve receives SIGTERM (e.g., when a Docker container is stopped), the process is killed immediately without running cleanup code. The temporary site_dir created in /tmp is never deleted, causing directories to accumulate.

Only KeyboardInterrupt (SIGINT / Ctrl+C) was handled, which triggered the cleanup path.

Fix

Register a SIGTERM signal handler that raises KeyboardInterrupt. This triggers the existing cleanup path in the except KeyboardInterrupt / finally blocks, which calls server.shutdown() and shutil.rmtree(site_dir).

After the first SIGTERM is caught, the handler is restored to SIG_DFL so a second SIGTERM during cleanup will force-kill the process.

Verification

  • All 725 existing tests pass (same pre-existing failures in localization tests)
  • Source inspection confirms signal.signal(signal.SIGTERM, ...) is registered with the _sigterm_handler that raises KeyboardInterrupt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"mkdocs serve" does not cleanup after a SIGTERM

1 participant