diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index e85fddeb7..9ef3bdc1a 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Removed + +- BREAKING: Revert [#1265]. `wait_for_termination` in `COMMON_BASH_TRAP_FUNCTIONS` always returns `0` again, + a SIGTERM before the child process ID is known is forwarded to the child once it is started, and + `product_logging::framework::manage_vector_shutdown_file_command` is removed. `remove_vector_shutdown_file_command` + is no longer deprecated and no longer installs an `EXIT` trap, so scripts must end with + `create_vector_shutdown_file_command` again ([#1350]). + +[#1350]: https://github.com/stackabletech/operator-rs/pull/1350 + ## [0.123.0] - 2026-12-02 ### Added diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap index 9e98acbf1..2e40a670c 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap @@ -22,15 +22,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -38,22 +39,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers @@ -107,15 +99,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -123,22 +116,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers @@ -197,15 +181,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -213,22 +198,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap index a7e9b5de5..54bdf700d 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap @@ -22,15 +22,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -38,22 +39,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap index 45bff4240..8eef27100 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap @@ -22,15 +22,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -38,22 +39,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap index 624508786..2bbf3926b 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap @@ -22,15 +22,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -38,22 +39,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers @@ -107,15 +99,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -123,22 +116,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers @@ -197,15 +181,16 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -213,22 +198,13 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } prepare_signal_handlers diff --git a/crates/stackable-operator/src/product_logging/framework.rs b/crates/stackable-operator/src/product_logging/framework.rs index 4cb0b0d2e..a3a894266 100644 --- a/crates/stackable-operator/src/product_logging/framework.rs +++ b/crates/stackable-operator/src/product_logging/framework.rs @@ -149,8 +149,7 @@ stackable_wait_for_tee() { /// background process which still has stdout or stderr open delays the exit by that long. This /// has the following consequences for the caller: /// -/// - The caller must not set its own EXIT trap, because it would replace this one. That includes -/// the trap of [`manage_vector_shutdown_file_command`] and [`remove_vector_shutdown_file_command`]. +/// - The caller must not set its own EXIT trap, because it would replace this one. /// - With the EXIT trap, Bash handles SIGTERM even as PID 1 of the container. A SIGTERM that no /// TERM trap handles, for example one before `prepare_signal_handlers` of /// [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS), exits the script @@ -1404,7 +1403,7 @@ sinks: /// meta::ObjectMetaBuilder, /// pod::{container::ContainerBuilder, resources::ResourceRequirementsBuilder, PodBuilder}, /// }, -/// product_logging::{self, framework::manage_vector_shutdown_file_command}, +/// product_logging::{self, framework:: {create_vector_shutdown_file_command, remove_vector_shutdown_file_command}}, /// utils::COMMON_BASH_TRAP_FUNCTIONS, /// }; /// use k8s_openapi::apimachinery::pkg::api::resource::Quantity; @@ -1450,13 +1449,16 @@ sinks: /// .args(vec![format!( /// "\ /// {COMMON_BASH_TRAP_FUNCTIONS} -/// {manage_vector_shutdown_file_command} +/// {remove_vector_shutdown_file_command} /// prepare_signal_handlers /// my-application start & /// wait_for_termination $! +/// {create_vector_shutdown_file_command} /// ", -/// manage_vector_shutdown_file_command = -/// manage_vector_shutdown_file_command(STACKABLE_LOG_DIR), +/// remove_vector_shutdown_file_command = +/// remove_vector_shutdown_file_command(STACKABLE_LOG_DIR), +/// create_vector_shutdown_file_command = +/// create_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// )]) /// .build(), /// ); @@ -1533,8 +1535,7 @@ kill $vector_pid } /// Command to create a shutdown file for the vector container. -/// Please delete it before starting your application using [`manage_vector_shutdown_file_command`], -/// which also creates it again when the script exits. +/// Please delete it before starting your application using [`remove_vector_shutdown_file_command`]. /// /// # Example /// @@ -1544,7 +1545,7 @@ kill $vector_pid /// const STACKABLE_LOG_DIR: &str = "/stackable/log"; /// /// let args = vec![ -/// product_logging::framework::manage_vector_shutdown_file_command(STACKABLE_LOG_DIR), +/// product_logging::framework::remove_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// "echo Perform some tasks ...".into(), /// product_logging::framework::create_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// ]; @@ -1565,150 +1566,21 @@ touch {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}" ) } -/// Use this command to bind the lifetime of the Vector container to the script that runs your -/// application. Execute it before starting your application. -/// -/// The command removes the shutdown file (if it exists) created by -/// [`create_vector_shutdown_file_command`] and installs an `EXIT` trap which creates the shutdown -/// file again, so the Vector container is shut down however the script ends: after a regular -/// exit, after `set -e` aborts the script because `wait_for_termination` returned the non-zero -/// exit status of the application, or after an explicit `exit`. The exit status of the script is -/// not changed by the trap. A trailing [`create_vector_shutdown_file_command`] is therefore not -/// needed. -/// -/// With an `EXIT` trap, bash handles SIGTERM even as PID 1 of the container. A SIGTERM before -/// `prepare_signal_handlers` of [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS) -/// therefore exits the script with status `143` at once instead of being ignored until the -/// termination grace period expires. Commands still running at that point, such as a database -/// migration, are killed with the container. -/// -/// The command must be executed in the top-level shell of the script which owns the lifetime of -/// the Vector container. In a subshell, the trap would fire when the subshell exits and shut down -/// Vector too early. The script must not install another `EXIT` trap, because it would replace -/// this one. -pub fn manage_vector_shutdown_file_command(stackable_log_dir: &str) -> String { - format!( - "rm -f {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE} && \ -trap '{create_shutdown_file_command}' EXIT", - create_shutdown_file_command = create_vector_shutdown_file_command(stackable_log_dir), - ) -} - -/// Alias of [`manage_vector_shutdown_file_command`], including its `EXIT` trap. -#[deprecated( - note = "renamed to stackable_operator::product_logging::framework::manage_vector_shutdown_file_command", - since = "0.123.0" -)] +/// Use this command to remove the shutdown file (if it exists) created by [`create_vector_shutdown_file_command`]. +/// You should execute this command before starting your application. pub fn remove_vector_shutdown_file_command(stackable_log_dir: &str) -> String { - manage_vector_shutdown_file_command(stackable_log_dir) + format!("rm -f {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}") } #[cfg(test)] mod tests { use std::collections::BTreeMap; - use indoc::{formatdoc, indoc}; + use indoc::indoc; use rstest::rstest; use super::*; - use crate::{ - product_logging::spec::{AppenderConfig, LoggerConfig}, - utils::{COMMON_BASH_TRAP_FUNCTIONS, bash::run_container_script}, - }; - - /// Runs the given application commands like a product container does and returns the exit - /// status of the script and whether the Vector shutdown file exists afterwards. - /// - /// With `trailing_create_command`, the script ends with an explicit - /// [`create_vector_shutdown_file_command`] like operator scripts written before the `EXIT` - /// trap existed. - fn run_application_script( - application_commands: &str, - trailing_create_command: bool, - ) -> (i32, bool) { - let stackable_log_dir = - tempfile::tempdir().expect("a temporary directory should be creatable"); - let stackable_log_dir_path = stackable_log_dir - .path() - .to_str() - .expect("the temporary directory path should be valid UTF-8"); - - let trailing_commands = if trailing_create_command { - create_vector_shutdown_file_command(stackable_log_dir_path) - } else { - String::new() - }; - let script = formatdoc! {" - {COMMON_BASH_TRAP_FUNCTIONS} - {manage_vector_shutdown_file_command} - prepare_signal_handlers - {application_commands} - {trailing_commands}", - manage_vector_shutdown_file_command = - manage_vector_shutdown_file_command(stackable_log_dir_path), - }; - - let (exit_code, _) = run_container_script(&script); - let shutdown_file_exists = stackable_log_dir - .path() - .join(VECTOR_LOG_DIR) - .join(SHUTDOWN_FILE) - .exists(); - - (exit_code, shutdown_file_exists) - } - - #[rstest] - #[case::application_crashes( - indoc! {" - bash -c 'exit 42' & - wait_for_termination $! - "}, - 42 - )] - #[case::application_exits_successfully( - indoc! {" - bash -c 'exit 0' & - wait_for_termination $! - "}, - 0 - )] - #[case::application_is_killed( - indoc! {" - bash -c 'kill -KILL $$' & - wait_for_termination $! - "}, - 137 - )] - #[case::application_shuts_down_with_failure_on_sigterm( - indoc! {" - bash -c 'trap \"exit 143\" TERM; sleep 10 >/dev/null 2>&1 & wait $!' & - application_pid=$! - (sleep 0.2; kill -TERM $$) & - wait_for_termination $application_pid - "}, - 143 - )] - #[case::application_shuts_down_successfully_on_sigterm( - indoc! {" - bash -c 'trap \"exit 0\" TERM; sleep 10 >/dev/null 2>&1 & wait $!' & - application_pid=$! - (sleep 0.2; kill -TERM $$) & - wait_for_termination $application_pid - "}, - 0 - )] - fn vector_shutdown_file_is_created_on_every_exit( - #[case] application_commands: &str, - #[case] expected_exit_code: i32, - #[values(false, true)] trailing_create_command: bool, - ) { - let (exit_code, shutdown_file_exists) = - run_application_script(application_commands, trailing_create_command); - - assert_eq!(expected_exit_code, exit_code); - assert!(shutdown_file_exists); - } + use crate::product_logging::spec::{AppenderConfig, LoggerConfig}; #[rstest] #[case("0Mi", &[])] diff --git a/crates/stackable-operator/src/utils/bash.rs b/crates/stackable-operator/src/utils/bash.rs index 6bec385e0..b93b77173 100644 --- a/crates/stackable-operator/src/utils/bash.rs +++ b/crates/stackable-operator/src/utils/bash.rs @@ -1,20 +1,7 @@ /// This is a bash snippet, which adds two functions out of interest: /// /// 1. `prepare_signal_handlers` call this first to set up the needed traps -/// 2. `wait_for_termination` waits for the PID you passed as the first argument to terminate and -/// returns its exit status -/// -/// A SIGTERM which arrives after `prepare_signal_handlers` but before `wait_for_termination` is -/// called exits the script with status `143`. A product already started in that window is killed -/// with the container instead of shut down gracefully. -/// -/// A SIGTERM before `prepare_signal_handlers` depends on whether the script has set an `EXIT` -/// trap, for example with -/// [`manage_vector_shutdown_file_command`](crate::product_logging::framework::manage_vector_shutdown_file_command). -/// Without one, the script runs as PID 1 of the container and ignores the signal, so the commands -/// before `prepare_signal_handlers` keep running until the termination grace period expires. With -/// one, bash handles the signal: the script runs the `EXIT` trap and exits with status `143` at -/// once, and the commands still running are killed with the container. +/// 2. `wait_for_termination` waits for the PID you passed as the first argument to terminate /// /// An example use could be /// ```text @@ -22,45 +9,23 @@ /// echo "Run before startup" /// prepare_signal_handlers /// {hadoop_home}/bin/hdfs {role} & -/// product_exit_code=0 -/// wait_for_termination $! || product_exit_code=$? +/// wait_for_termination $! /// echo "Run after termination" -/// exit "${product_exit_code}" /// ``` -// A `wait` status above 128 can mean two things: -// 1. bash reports a signal death as `128 + signal` -// 2. an in-progress `wait` aborts with such a status when a trapped signal arrives -// -// In case 2 the child is still running, so we wait again as long as it exists. The TERM trap -// stays installed until the child is gone: a further SIGTERM is forwarded to the child instead of -// terminating the script while the child shuts down. Resetting the trap does not help here, -// because a script with an `EXIT` trap still handles SIGTERM, even as PID 1. -// -// Once the child is gone, we wait a final time to get its actual status, which immediately -// returns the status again in case 1. This also covers a child which terminated before the -// `kill -0` check, while its status was not reported yet. -// -// In POSIX mode bash forgets the status of a child once `wait` reported it, so the final `wait` -// returns `127` in case 1. We then keep the status of the previous `wait`. -// -// See https://www.gnu.org/software/bash/manual/html_node/Signals.html -// -// A SIGTERM before the child PID is known exits the script instead of being forwarded once the -// child is started: bash 5.1 (UBI 9) regularly loses a SIGTERM sent to a child right after it -// was forked, and the child then runs until the termination grace period expires. pub const COMMON_BASH_TRAP_FUNCTIONS: &str = r#" prepare_signal_handlers() { unset term_child_pid + unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ -n "${term_child_pid:-}" ]; then + if [ "${term_child_pid}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - exit 143 + term_kill_needed="yes" fi } @@ -68,113 +33,12 @@ wait_for_termination() { set +e term_child_pid=$1 + if [[ -v term_kill_needed ]]; then + kill -TERM "${term_child_pid}" 2>/dev/null + fi wait ${term_child_pid} 2>/dev/null - term_child_status=$? - while [ "${term_child_status}" -gt 128 ] && kill -0 "${term_child_pid}" 2>/dev/null; do - wait ${term_child_pid} 2>/dev/null - term_child_status=$? - done trap - TERM - if [ "${term_child_status}" -gt 128 ]; then - wait ${term_child_pid} 2>/dev/null - term_child_final_status=$? - if [ "${term_child_final_status}" -ne 127 ]; then - term_child_status=${term_child_final_status} - fi - fi + wait ${term_child_pid} 2>/dev/null set -e - return ${term_child_status} } "#; - -/// Runs the given script with the shell options of a product container and returns its exit -/// status and stdout. -#[cfg(test)] -pub(crate) fn run_container_script(script: &str) -> (i32, String) { - let output = std::process::Command::new("bash") - .args(["-euo", "pipefail", "-c", script]) - .output() - .expect("bash should be executable in the test environment"); - let exit_code = output - .status - .code() - .expect("the script should terminate regularly and not by a signal"); - let stdout = String::from_utf8(output.stdout).expect("the script should only print UTF-8"); - - (exit_code, stdout) -} - -#[cfg(test)] -mod tests { - use indoc::formatdoc; - - use super::*; - - #[test] - fn crash_with_command_after_the_call() { - let (exit_code, stdout) = run_container_script(&formatdoc! {" - {COMMON_BASH_TRAP_FUNCTIONS} - prepare_signal_handlers - bash -c 'exit 42' & - product_exit_code=0 - wait_for_termination $! || product_exit_code=$? - echo tail-ran - exit \"${{product_exit_code}}\"" - }); - - assert_eq!(42, exit_code); - assert!(stdout.contains("tail-ran\n")); - } - - #[test] - fn second_sigterm_while_child_shuts_down() { - let (exit_code, stdout) = run_container_script(&formatdoc! {" - {COMMON_BASH_TRAP_FUNCTIONS} - trap 'echo exit-trap-ran' EXIT - prepare_signal_handlers - (sleep 0.5; kill -TERM $$; sleep 0.3; kill -TERM $$) & - bash -c 'trap \"sleep 1; echo child-stopped; exit 0\" TERM; sleep 10 >/dev/null 2>&1 & wait' & - product_exit_code=0 - wait_for_termination $! || product_exit_code=$? - echo tail-ran - exit \"${{product_exit_code}}\"" - }); - - assert_eq!(0, exit_code); - assert!(stdout.contains("child-stopped\n")); - assert!(stdout.contains("tail-ran\n")); - assert!(stdout.contains("exit-trap-ran\n")); - } - - #[test] - fn exit_status_above_128_in_posix_mode() { - let (exit_code, stdout) = run_container_script(&formatdoc! {" - set -o posix - {COMMON_BASH_TRAP_FUNCTIONS} - prepare_signal_handlers - bash -c 'exit 137' & - product_exit_code=0 - wait_for_termination $! || product_exit_code=$? - echo tail-ran - exit \"${{product_exit_code}}\"" - }); - - assert_eq!(137, exit_code); - assert!(stdout.contains("tail-ran\n")); - } - - #[test] - fn sigterm_before_child_pid_is_known() { - let (exit_code, stdout) = run_container_script(&formatdoc! {" - {COMMON_BASH_TRAP_FUNCTIONS} - prepare_signal_handlers - kill -TERM $$ - echo child-started - sleep 10 & - wait_for_termination $!" - }); - - assert_eq!(143, exit_code); - assert!(!stdout.contains("child-started")); - } -}