-
Notifications
You must be signed in to change notification settings - Fork 782
Make Core.shutdown idempotent and safe to call concurrently #6891
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
Draft
agners
wants to merge
1
commit into
main
Choose a base branch
from
core-shutdown-reentrant
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.
Draft
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -233,3 +233,98 @@ async def test_setup_unhandled_exception_captured( | |
| capture_mock.assert_called_once() | ||
| assert "Fatal error happening on load Task" in caplog.text | ||
| assert UnhealthyReason.SETUP in coresys.resolution.unhealthy | ||
|
|
||
|
|
||
| async def test_shutdown_reentrant_waits(coresys: CoreSys): | ||
| """Concurrent shutdown() calls await the in-flight shutdown rather than re-running.""" | ||
| call_count = 0 | ||
| shutdown_started = asyncio.Event() | ||
| proceed = asyncio.Event() | ||
|
|
||
| original_shutdown = coresys.apps.shutdown | ||
|
|
||
| async def slow_app_shutdown(startup): | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| shutdown_started.set() | ||
| await proceed.wait() | ||
| return await original_shutdown(startup) | ||
|
|
||
| await coresys.core.set_state(CoreState.RUNNING) | ||
|
|
||
| with patch.object(coresys.apps, "shutdown", side_effect=slow_app_shutdown): | ||
| task1 = asyncio.create_task(coresys.core.shutdown()) | ||
| await shutdown_started.wait() | ||
|
|
||
| # Second call should wait, not start a new shutdown | ||
| task2 = asyncio.create_task(coresys.core.shutdown()) | ||
| await asyncio.sleep(0.05) | ||
|
|
||
| proceed.set() | ||
| await asyncio.gather(task1, task2) | ||
|
|
||
| # AppStartup has 4 levels (APPLICATION/SERVICES/SYSTEM/INITIALIZE); a single | ||
| # shutdown call iterates them. A re-entered shutdown would double the count. | ||
| assert call_count == 4 | ||
| assert coresys.core._shutdown_event.is_set() | ||
|
|
||
|
|
||
| async def test_shutdown_event_reset_between_cycles(coresys: CoreSys): | ||
| """Repeated shutdown cycles (e.g. backup restore) work because the event is reset.""" | ||
| await coresys.core.set_state(CoreState.RUNNING) | ||
|
|
||
| await coresys.core.shutdown() | ||
| assert coresys.core._shutdown_event.is_set() | ||
|
|
||
| # Simulate restore returning to RUNNING and shutting down again | ||
| await coresys.core.set_state(CoreState.RUNNING) | ||
|
|
||
| second_entered = False | ||
| original_shutdown = coresys.apps.shutdown | ||
|
|
||
| async def track_app_shutdown(startup): | ||
| nonlocal second_entered | ||
| second_entered = True | ||
| return await original_shutdown(startup) | ||
|
|
||
| with patch.object(coresys.apps, "shutdown", side_effect=track_app_shutdown): | ||
| await coresys.core.shutdown() | ||
|
|
||
| assert second_entered | ||
| assert coresys.core._shutdown_event.is_set() | ||
|
Comment on lines
+272
to
+294
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean this is very much a fabricated use case, it can't ever happen in production. |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "state", [CoreState.STOPPING, CoreState.CLOSE], ids=["stopping", "close"] | ||
| ) | ||
| async def test_shutdown_ignored_during_stop( | ||
| coresys: CoreSys, caplog: pytest.LogCaptureFixture, state: CoreState | ||
| ): | ||
| """Shutdown is ignored when Supervisor is already stopping.""" | ||
| await coresys.core.set_state(state) | ||
|
|
||
| with patch.object(coresys.apps, "shutdown") as mock_app_shutdown: | ||
| await coresys.core.shutdown() | ||
|
|
||
| mock_app_shutdown.assert_not_called() | ||
| assert "Ignoring shutdown request, Supervisor is already stopping" in caplog.text | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "state", | ||
| [CoreState.INITIALIZE, CoreState.STARTUP, CoreState.SETUP], | ||
| ids=["initialize", "startup", "setup"], | ||
| ) | ||
| async def test_shutdown_skipped_during_startup( | ||
| coresys: CoreSys, caplog: pytest.LogCaptureFixture, state: CoreState | ||
| ): | ||
| """Shutdown returns early when Supervisor has not finished starting yet.""" | ||
| await coresys.core.set_state(state) | ||
|
|
||
| with patch.object(coresys.apps, "shutdown") as mock_app_shutdown: | ||
| await coresys.core.shutdown() | ||
|
|
||
| mock_app_shutdown.assert_not_called() | ||
| assert ( | ||
| "Ignoring shutdown request, Supervisor has not finished starting" in caplog.text | ||
| ) | ||
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.
The concept of repeated use doesn't really make sense here. Shutdown eventually leads to a stop in the python process which obviously clears the event. And there's no way to go back to
CoreState.RUNNINGfrom those closing states. So I'm not really sure when this would have any effect? At the very least the comment should probably be adjusted since its example isn't a real use case.