From 2f2b5683a2029f8f8fe1deda9d2aa7bd0d4a3029 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Wed, 26 Aug 2020 13:53:08 -0400 Subject: [PATCH] amazon/ssm_driver: Update log polling logic This change checks for closed iochans related to the log polling function and will break out of the loop when both channels are closed. Which is an indicator that the SSM session has been terminated by some external process. This fixes an issue where Packer hangs, due to the infinite loop, when an SSM session is killed outside of Packer. Related to #9442 --- builder/amazon/common/ssm_driver.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/builder/amazon/common/ssm_driver.go b/builder/amazon/common/ssm_driver.go index 5ba73c8bd..563fa7eab 100644 --- a/builder/amazon/common/ssm_driver.go +++ b/builder/amazon/common/ssm_driver.go @@ -46,7 +46,7 @@ func NewSSMDriver(config SSMDriverConfig) *SSMDriver { // not wish to manage the session manually calling StopSession on a instance of this driver will terminate the active session // created from calling StartSession. func (d *SSMDriver) StartSession(ctx context.Context, input ssm.StartSessionInput) (*ssm.StartSessionOutput, error) { - log.Printf("Starting PortForwarding session to instance %q with following params %v", aws.StringValue(input.Target), input.Parameters) + log.Printf("Starting PortForwarding session to instance %q", aws.StringValue(input.Target)) var output *ssm.StartSessionOutput err := retry.Config{ @@ -110,15 +110,30 @@ func (d *SSMDriver) openTunnelForSession(ctx context.Context) error { select { case <-ctx.Done(): return - case output := <-stderrCh: + case output, ok := <-stderrCh: + if !ok { + stderrCh = nil + break + } + if output != "" { log.Printf("[ERROR] %s: %s", prefix, output) } - case output := <-stdoutCh: + case output, ok := <-stdoutCh: + if !ok { + stdoutCh = nil + break + } + if output != "" { log.Printf("[DEBUG] %s: %s", prefix, output) } } + + if stdoutCh == nil && stderrCh == nil { + log.Printf("[DEBUG] %s: %s", prefix, "active session has been terminated; stopping all log polling processes.") + return + } } }(ctx, sessionManagerPluginName)