Fix: shutdown()がderegister後にexitせず終了処理が非決定的になる問題を修正 - #9
Merged
Conversation
- Only read the registration file and call `aws ssm deregister-managed-instance` when the file exists and the parsed ManagedInstanceID is non-empty/non-null, avoiding a deregister call with an empty --instance-id. - Add `exit 0` at the end of shutdown() so the SIGTERM trap completes deterministically instead of falling back into `while true; do wait; done` and idling until kubelet's SIGKILL at the end of the grace period.
matchan26
approved these changes
Jul 21, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by Jutaro Numata · Slack thread
プルリクエストの目的・概要
注意: このリポジトリは、Public Repositoryです。機密情報は含めないでください。
ssm-agent.shのshutdown()関数を修正し、SIGTERM受信時の終了処理を決定的にします。Before(修正前)
shutdown()はaws ssm deregister-managed-instanceを実行した後、関数から抜けるだけでexitしていませんでした。そのためtrap shutdown TERMの実行後、スクリプトは元のwhile true; do wait; doneに戻ってしまい、そのままkubeletのgrace period終了までアイドルし、最終的にSIGKILLで強制終了されていました。この間、
aws ssm deregister-managed-instanceの呼び出しに一時的な失敗やレイテンシがあっても、リトライや完了を待つ機会がなく、SIGKILLで処理が打ち切られ、SSMのmanaged instance登録が孤立(deregisterされないまま)してしまう可能性がありました。また、/var/lib/amazon/ssm/registrationが存在しない、またはManagedInstanceIDが空/nullの場合でも--instance-idを空で呼び出してしまう問題もありました。After(修正後)
ManagedInstanceIDが空/null でない場合のみaws ssm deregister-managed-instanceを呼び出すようにガードを追加。shutdown()の最後にexit 0を追加し、クリーンアップ完了後に確実にプロセスを終了するように修正。関連するIssue
globis-org/core-infra#4188(SSM Advanced Instance Tier の調査から、本PRで修正した ssm-agent.sh の deregister バグが発見されました)
なし(Slackスレッドでの報告に基づく修正)
変更内容
ssm-agent.shのshutdown()関数のみを変更(Dockerfileやその他の処理は変更なし、リトライ/バックオフ等の追加なし)How
function shutdown(){ echo "start shutdown process ..." - instance_id=$(cat /var/lib/amazon/ssm/registration | jq -r .ManagedInstanceID) - aws ssm deregister-managed-instance --instance-id $instance_id + if [ -f /var/lib/amazon/ssm/registration ]; then + instance_id=$(cat /var/lib/amazon/ssm/registration | jq -r .ManagedInstanceID) + if [ -n "${instance_id}" ] && [ "${instance_id}" != "null" ]; then + aws ssm deregister-managed-instance --instance-id $instance_id + fi + fi kill $(pgrep amazon-ssm) echo "shutdown process completed." + exit 0 }このリポジトリはPublicなサンプル/テンプレートとして他リポジトリ(
globis-org/infra-dockerfilesのssm-bastion/ssm-agent.shなど)からコピーされて使われています。同じ修正をglobis-org/infra-dockerfiles側にも入れていますが、テンプレート元であるこちらも修正することで、今後新規にbastionを作成した際に同じバグが再発しないようにします。