Skip to content

Fix: shutdown()がderegister後にexitせず終了処理が非決定的になる問題を修正 - #9

Merged
matchan26 merged 1 commit into
mainfrom
fix/ssm-agent-shutdown-exit
Jul 21, 2026
Merged

Fix: shutdown()がderegister後にexitせず終了処理が非決定的になる問題を修正#9
matchan26 merged 1 commit into
mainfrom
fix/ssm-agent-shutdown-exit

Conversation

@claude

@claude claude Bot commented Jul 21, 2026

Copy link
Copy Markdown

Requested by Jutaro Numata · Slack thread

プルリクエストの目的・概要

注意: このリポジトリは、Public Repositoryです。機密情報は含めないでください。

ssm-agent.shshutdown() 関数を修正し、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(修正後)

  • registrationファイルが存在し、かつパースした 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.shshutdown() 関数のみを変更(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-dockerfilesssm-bastion/ssm-agent.sh など)からコピーされて使われています。同じ修正を globis-org/infra-dockerfiles 側にも入れていますが、テンプレート元であるこちらも修正することで、今後新規にbastionを作成した際に同じバグが再発しないようにします。

- 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
matchan26 merged commit 071eca3 into main Jul 21, 2026
2 checks passed
@matchan26
matchan26 deleted the fix/ssm-agent-shutdown-exit branch July 21, 2026 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants