Repository navigation
[Bugfix] Fail fast when the Python HTTP metadata server cannot bind its port #2924
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
toffee-desuwa
wants to merge
2
commits into
kvcache-ai:main
Choose a base branch
from
toffee-desuwa:fix/python-http-metadata-startup
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
116 changes: 116 additions & 0 deletions
116
mooncake-wheel/tests/test_http_metadata_server_startup.py
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,116 @@ | ||
| #!/usr/bin/env python3 | ||
| import asyncio | ||
| import logging | ||
| import socket | ||
| import sys | ||
| import unittest | ||
| from concurrent.futures import Future | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | ||
|
|
||
| from mooncake.http_metadata_server import KVBootstrapServer, KVPoll | ||
|
|
||
|
|
||
| class HttpMetadataServerStartupTest(unittest.TestCase): | ||
| def _close_and_join(self, server): | ||
| server.close() | ||
| thread = server.thread | ||
| if thread is None: | ||
| return | ||
|
|
||
| if thread.is_alive(): | ||
| loop = server._loop | ||
| if loop is not None and not loop.is_closed(): | ||
| try: | ||
| loop.call_soon_threadsafe(loop.stop) | ||
| except RuntimeError: | ||
| pass | ||
| thread.join(timeout=2) | ||
|
|
||
| def test_run_propagates_bind_failure_without_logging_success(self): | ||
| host = "127.0.0.1" | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as reservation: | ||
| reservation.bind((host, 0)) | ||
| reservation.listen(1) | ||
| server = KVBootstrapServer( | ||
| port=reservation.getsockname()[1], host=host | ||
| ) | ||
|
|
||
| try: | ||
| with self.assertLogs(level=logging.INFO) as captured_logs: | ||
| with self.assertRaises(OSError): | ||
| server.run() | ||
|
|
||
| thread = server.thread | ||
| self.assertIsNotNone(thread) | ||
| thread.join(timeout=2) | ||
| self.assertFalse(thread.is_alive()) | ||
| self.assertFalse( | ||
| any( | ||
| "HTTP Metadata Server started" in message | ||
| for message in captured_logs.output | ||
| ) | ||
| ) | ||
| finally: | ||
| self._close_and_join(server) | ||
|
|
||
| def test_run_reports_ready_server_and_close_stops_thread(self): | ||
| server = KVBootstrapServer(port=0, host="127.0.0.1") | ||
| thread = None | ||
|
|
||
| try: | ||
| with self.assertLogs(level=logging.INFO) as captured_logs: | ||
| thread = server.run() | ||
|
|
||
| self.assertTrue(thread.is_alive()) | ||
| self.assertEqual(server.poll(), KVPoll.Success) | ||
| self.assertTrue( | ||
| any( | ||
| "HTTP Metadata Server started" in message | ||
| for message in captured_logs.output | ||
| ) | ||
| ) | ||
| finally: | ||
| self._close_and_join(server) | ||
|
|
||
| self.assertIsNotNone(thread) | ||
| self.assertFalse(thread.is_alive()) | ||
|
|
||
| def test_runtime_base_exception_is_not_swallowed(self): | ||
| server = KVBootstrapServer(port=0, host="127.0.0.1") | ||
| startup = Future() | ||
| loop = asyncio.new_event_loop() | ||
| run_forever = loop.run_forever | ||
|
|
||
| # Stop the loop as soon as the server signals readiness (the callback | ||
| # runs in the loop thread when notify_started sets the result), so the | ||
| # wrapper below can raise once startup has completed. Keying off the | ||
| # startup future keeps this independent of how many times | ||
| # run_until_complete internally invokes run_forever. | ||
| startup.add_done_callback(lambda _: loop.call_soon(loop.stop)) | ||
|
|
||
| def interrupt_after_readiness(): | ||
| run_forever() | ||
| if startup.done(): | ||
| # Restore the real run_forever so cleanup in _run_server's | ||
| # finally block is not interrupted a second time. | ||
| loop.run_forever = run_forever | ||
| raise KeyboardInterrupt | ||
|
|
||
| loop.run_forever = interrupt_after_readiness | ||
|
|
||
| try: | ||
| with patch.object(asyncio, "new_event_loop", return_value=loop): | ||
| with self.assertRaises(KeyboardInterrupt): | ||
| server._run_server(startup) | ||
| finally: | ||
| asyncio.set_event_loop(None) | ||
|
|
||
| self.assertIsNone(startup.result()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent the test suite from hanging indefinitely in CI if the test fails or the background task gets stuck, it is safer to specify a timeout when calling
startup.result().