From 7842594f538ffcafba08f8c273818081059a33f2 Mon Sep 17 00:00:00 2001 From: Kota Kanbe Date: Thu, 20 Dec 2018 13:59:54 +0900 Subject: [PATCH] fix(scan): OS detection ssh timeout in first run #699 (#753) --- scan/serverapi.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/scan/serverapi.go b/scan/serverapi.go index 80267d78..1a0e4626 100644 --- a/scan/serverapi.go +++ b/scan/serverapi.go @@ -84,6 +84,30 @@ type osPackages struct { Kernel models.Kernel } +// Retry as it may stall on the first SSH connection +// https://github.com/future-architect/vuls/pull/753 +func detectDebianWithRetry(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err error) { + type Response struct { + itsMe bool + deb osTypeInterface + err error + } + resChan := make(chan Response, 1) + go func(c config.ServerInfo) { + itsMe, osType, fatalErr := detectDebian(c) + resChan <- Response{itsMe, osType, fatalErr} + }(c) + + timeout := time.After(time.Duration(3) * time.Second) + select { + case res := <-resChan: + return res.itsMe, res.deb, res.err + case <-timeout: + time.Sleep(100 * time.Millisecond) + return detectDebian(c) + } +} + func detectOS(c config.ServerInfo) (osType osTypeInterface) { var itsMe bool var fatalErr error @@ -93,7 +117,7 @@ func detectOS(c config.ServerInfo) (osType osTypeInterface) { return } - itsMe, osType, fatalErr = detectDebian(c) + itsMe, osType, fatalErr = detectDebianWithRetry(c) if fatalErr != nil { osType.setErrs([]error{ fmt.Errorf("Failed to detect OS: %s", fatalErr)})