-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Fixed directory cleanup after terminated by signal (#3905) #3913
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
Open
alvaro-osvaldo-tm
wants to merge
7
commits into
mkdocs:master
Choose a base branch
from
alvaro-osvaldo-tm:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−9
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bef904
fix: Fixed temporary directory cleanup after terminate signal (#3905)
alvaro-osvaldo-tm ceb0445
style: Removed unused import
alvaro-osvaldo-tm dcf7368
refactor: Moved 'strsignal' import to global scope
alvaro-osvaldo-tm c2337ad
Merge branch 'mkdocs:master' into master
alvaro-osvaldo-tm ef152a9
tests: Implemented test for 'shutdown by signal' feature
alvaro-osvaldo-tm 43b9da4
tests: Fixed failures for windows runtime
alvaro-osvaldo-tm c459a10
style: Fixed code style
alvaro-osvaldo-tm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from subprocess import Popen | ||
| from tempfile import TemporaryDirectory | ||
| from time import sleep | ||
|
|
||
|
|
||
| @dataclass | ||
| class Sample_Repository: | ||
| handler: TemporaryDirectory | ||
| site_dir: str | ||
| signature: str | ||
|
|
||
| def __del__(self): | ||
| from sys import platform | ||
|
|
||
| if platform == "linux": | ||
| self.handler.cleanup() | ||
|
|
||
|
|
||
| class Shutdown_by_signal_tests(unittest.TestCase): | ||
| SLEEPING_TIME_WAITING_FOR_START = 2 | ||
| SLEEPING_TIME_WAITING_FOR_SHUTDOWN = 2 | ||
| SLEEPING_TIME_WAITING_FOR_PROCESS = 6 | ||
| TRIES_TO_CHECK_FOR_DIRECTORY_CLEANUP = 4 | ||
|
|
||
| def _create_sample_repository(self) -> Sample_Repository: | ||
| from os import mkdir | ||
| from uuid import uuid4 | ||
|
|
||
| from yaml import dump | ||
|
|
||
| handler = TemporaryDirectory(prefix='mkdocs_test-') | ||
| site_dir = handler.name | ||
|
|
||
| signature = uuid4().hex | ||
|
|
||
| configuration = {'site_name': 'Testing case', 'docs_dir': 'docs'} | ||
|
|
||
| docs_dir = f"{site_dir}/{configuration.get('docs_dir')}" | ||
| mkdir(docs_dir) | ||
|
|
||
| Path(site_dir, "mkdocs.yml").write_text(dump(configuration)) | ||
| Path(docs_dir, "index.md").write_text("# Index File") | ||
| Path(docs_dir, "signature.md").write_text(f"# {signature} ") | ||
|
|
||
| return Sample_Repository( | ||
| handler=handler, | ||
| site_dir=site_dir, | ||
| signature=signature, | ||
| ) | ||
|
|
||
| def _execute_mkdocs_as_liveserver(self, site_dir: str) -> Popen: | ||
| from os import chdir, getcwd | ||
| from subprocess import DEVNULL | ||
| from sys import platform | ||
|
|
||
| current_working_dir = getcwd() | ||
| chdir(site_dir) | ||
|
|
||
| if platform == "linux": | ||
| from errno import EADDRINUSE | ||
| from socket import AF_INET, SOCK_STREAM, socket | ||
|
|
||
| port_testing = socket(AF_INET, SOCK_STREAM) | ||
|
|
||
| try: | ||
| port_testing.bind(("127.0.0.1", 8000)) | ||
| except OSError as exception: | ||
| if exception.errno == EADDRINUSE: | ||
| Popen('killall mkdocs'.split(' ')).wait( | ||
| timeout=self.SLEEPING_TIME_WAITING_FOR_PROCESS | ||
| ) | ||
|
|
||
| port_testing.close() | ||
|
|
||
| mkdocs = Popen('mkdocs serve'.split(' '), stdout=DEVNULL, stderr=DEVNULL, shell=False) | ||
| sleep(self.SLEEPING_TIME_WAITING_FOR_START) | ||
|
|
||
| chdir(current_working_dir) | ||
|
|
||
| return mkdocs | ||
|
|
||
| def _locate_mkdocs_directory(self, signature: str) -> Path | None: | ||
| from pathlib import Path | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| temporary_directory_probe = TemporaryDirectory() | ||
| temporary_directory = Path(f"{temporary_directory_probe.name}").parent | ||
|
|
||
| for mkdocs_temporary_directory in temporary_directory.glob('mkdocs_*'): | ||
| mkdocs_temporary_path = Path(mkdocs_temporary_directory) | ||
| mkdocs_signature_path = Path(f"{mkdocs_temporary_path}/signature/index.html") | ||
|
|
||
| if not mkdocs_temporary_path.exists(): | ||
| continue | ||
|
|
||
| if not mkdocs_temporary_path.is_dir(): | ||
| continue | ||
|
|
||
| if not mkdocs_signature_path.exists(): | ||
| continue | ||
|
|
||
| with mkdocs_signature_path.open('r') as fp: | ||
| signature_found = fp.read() | ||
|
|
||
| if signature_found.find(signature) == -1: | ||
| continue | ||
|
|
||
| temporary_directory_probe.cleanup() | ||
|
|
||
| return mkdocs_temporary_path | ||
|
|
||
| return None | ||
|
|
||
| def _is_active(self, mkdocs: Popen) -> bool: | ||
| from errno import ESRCH | ||
| from os import kill | ||
| from signal import SIG_BLOCK | ||
|
|
||
| if mkdocs.returncode is not None: | ||
| return False | ||
|
|
||
| try: | ||
| kill(mkdocs.pid, SIG_BLOCK) | ||
| except OSError as exception: | ||
| if exception.errno == ESRCH: | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| def _wait_for_shutdown(self, mkdocs: Popen, signal: int): | ||
| from os import kill | ||
|
|
||
| kill(mkdocs.pid, signal) | ||
|
|
||
| while self._is_active(mkdocs): | ||
| sleep(self.SLEEPING_TIME_WAITING_FOR_SHUTDOWN) | ||
|
|
||
| def _was_directory_cleaned(self, path: Path) -> bool: | ||
| for _ in range(self.TRIES_TO_CHECK_FOR_DIRECTORY_CLEANUP): | ||
| if not path.exists(): | ||
| return True | ||
|
|
||
| sleep(self.SLEEPING_TIME_WAITING_FOR_PROCESS) | ||
|
|
||
| return False | ||
|
|
||
| def test_shutdown_with_signal(self): | ||
| from signal import SIGINT, SIGTERM, strsignal | ||
|
|
||
| repository = self._create_sample_repository() | ||
|
|
||
| for signal in [SIGINT, SIGTERM]: | ||
| mkdocs = self._execute_mkdocs_as_liveserver(repository.site_dir) | ||
| mkdocs_temporary_path = self._locate_mkdocs_directory(signature=repository.signature) | ||
|
|
||
| self.assertTrue( | ||
| mkdocs_temporary_path is not None, "Unable to locate the live server directory" | ||
| ) | ||
|
|
||
| self._wait_for_shutdown(mkdocs, signal) | ||
|
|
||
| was_cleaned = self._was_directory_cleaned(mkdocs_temporary_path) | ||
|
|
||
| self.assertTrue( | ||
| was_cleaned, | ||
| f"The Mkdocs '{mkdocs_temporary_path}' was not cleaned " | ||
| f"with signal '{strsignal(signal)}'", | ||
| ) | ||
|
|
||
| # This is a flaw in 'liveserver' shutdown process | ||
| # it seems mkdocs become zombie when testing. | ||
| while mkdocs.returncode is None: | ||
| mkdocs.wait(timeout=self.SLEEPING_TIME_WAITING_FOR_PROCESS) | ||
|
|
||
| del mkdocs | ||
|
|
||
| del repository | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| Shutdown_by_signal_tests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.