-
Notifications
You must be signed in to change notification settings - Fork 23
Gracefully shut down services on test failure and context cancellation #63
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
samschlegel
wants to merge
4
commits into
hermeticbuild:master
Choose a base branch
from
samschlegel:fix-graceful-shutdown-on-failure
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
08bff12
Add failing test for graceful shutdown on test failure
samschlegel 5d9065b
Gracefully shut down services on test failure
samschlegel 42f4485
simplified
samschlegel 35fad36
Prevent context cancellation from sending SIGKILL to services
samschlegel 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 @@ | ||
| int main() { return 1; } |
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,32 @@ | ||
| load("@rules_go//go:def.bzl", "go_binary") | ||
| load("@rules_itest//:itest.bzl", "itest_service", "service_test") | ||
| load(":check_graceful_shutdown.bzl", "check_graceful_shutdown_test") | ||
|
|
||
| go_binary( | ||
| name = "service", | ||
| srcs = ["main.go"], | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| itest_service( | ||
| name = "graceful_service", | ||
| args = ["$${PORT}"], | ||
| autoassign_port = True, | ||
| exe = ":service", | ||
| http_health_check_address = "http://127.0.0.1:$${PORT}", | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| service_test( | ||
| name = "_failing_test_with_service", | ||
| services = [ | ||
| ":graceful_service", | ||
| ], | ||
| tags = ["manual"], | ||
| test = "@rules_itest//:exit1", | ||
| ) | ||
|
|
||
| check_graceful_shutdown_test( | ||
| name = "graceful_shutdown_on_failure_test", | ||
| service_test = ":_failing_test_with_service", | ||
| ) |
68 changes: 68 additions & 0 deletions
68
tests/graceful_shutdown_on_failure/check_graceful_shutdown.bzl
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,68 @@ | ||
| """Rule to verify that services are gracefully shut down when a test fails.""" | ||
|
|
||
| load("@rules_shell//shell:sh_test.bzl", "sh_test") | ||
|
|
||
| def _gen_check_script_impl(ctx): | ||
| """Generates a shell script that exports RunEnvironmentInfo env vars and checks for graceful shutdown.""" | ||
| inner_env = ctx.attr.service_test[RunEnvironmentInfo].environment | ||
|
|
||
| script = ctx.actions.declare_file(ctx.label.name + ".sh") | ||
| env_exports = "\n".join([ | ||
| 'export %s="%s"' % (k, v) | ||
| for k, v in inner_env.items() | ||
| ]) | ||
|
|
||
| ctx.actions.write( | ||
| output = script, | ||
| content = """#!/bin/bash | ||
| set -uo pipefail | ||
|
|
||
| # Set the env vars that the service_test needs (from RunEnvironmentInfo). | ||
| {env_exports} | ||
|
|
||
| # Run the service_test binary ($1 is resolved via $(location) by Bazel). | ||
| # It is expected to fail because the inner test exits 1. | ||
| "$1" || true | ||
|
|
||
| # Poll for the shutdown marker with a timeout. | ||
| for i in $(seq 1 20); do | ||
| if [ -f "$TEST_TMPDIR/shutdown_marker" ]; then | ||
| echo "PASS: Service received SIGTERM and shut down gracefully" | ||
| exit 0 | ||
| fi | ||
| sleep 0.25 | ||
| done | ||
|
|
||
| echo "FAIL: Service did NOT receive SIGTERM — graceful shutdown was skipped" | ||
| exit 1 | ||
| """.format(env_exports = env_exports), | ||
| is_executable = True, | ||
| ) | ||
|
|
||
| return [DefaultInfo(files = depset([script]))] | ||
|
|
||
| _gen_check_script = rule( | ||
| implementation = _gen_check_script_impl, | ||
| attrs = { | ||
| "service_test": attr.label( | ||
| mandatory = True, | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| def check_graceful_shutdown_test(name, service_test, **kwargs): | ||
| """Verifies that services receive SIGTERM even when the inner test fails.""" | ||
| _gen_check_script( | ||
| name = name + "_script", | ||
| service_test = service_test, | ||
| testonly = True, | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| sh_test( | ||
| name = name, | ||
| srcs = [":" + name + "_script"], | ||
| args = ["$(location %s)" % service_test], | ||
| data = [service_test], | ||
| **kwargs | ||
| ) |
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,30 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func main() { | ||
| port := os.Args[1] | ||
|
|
||
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| }) | ||
|
|
||
| go http.ListenAndServe("127.0.0.1:"+port, nil) | ||
|
|
||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, syscall.SIGTERM) | ||
| <-sigCh | ||
|
|
||
| markerPath := os.Getenv("TEST_TMPDIR") + "/shutdown_marker" | ||
| if err := os.WriteFile(markerPath, []byte("shutdown"), 0644); err != nil { | ||
| fmt.Printf("Failed to write shutdown marker: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| fmt.Println("Graceful shutdown completed") | ||
| } |
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.
Setting
cmd.WaitDelaytoshutdown_timeoutmeanscmd.Wait()can now block for the full shutdown window when a service exits but leaves stdout/stderr pipes open (the exact orphaned-child case the previous 50ms delay handled). In that scenarioServiceInstance.StopWithSignalwaits ons.isDone()and times out at the same duration, so it can log a spurious graceful-shutdown failure and sendSIGKILL(or error whenenforce_forceful_shutdownis enabled) even though the main process already handledSIGTERM. This is a behavioral regression from the prior short wait-delay logic.Useful? React with 👍 / 👎.