Merge cloudbots codes from TOT to all release branches
BUG=b:336556314

[cloudbots][cft] Add IsAlive check when reboot through cloudbots

BUG=b:325553505
TEST=cros_sdk cros-workon --host start cros-dut, cros_sdk sudo emerge cros-dut, bniche-test cros-tool-runner on cloudbot VM

Change-Id: Id4b738e61d104295c7bd162c28f066caa1ce557e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/5388456
Tested-by: Bryan Ni <bniche@google.com>
Reviewed-by: Azizur Rahman <azrahman@google.com>
Reviewed-by: Sergey Fetisov <sfetisov@google.com>
Reviewed-by: Derek Beckett <dbeckett@chromium.org>
Commit-Queue: Bryan Ni <bniche@google.com>
(cherry picked from commit a7464ae6717a6d4a9ad7161c5e981494656842b9)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/5513107
diff --git a/src/chromiumos/test/dut/cmd/cros-dut/dutserver.go b/src/chromiumos/test/dut/cmd/cros-dut/dutserver.go
index 1fe01af..c26f6d7 100644
--- a/src/chromiumos/test/dut/cmd/cros-dut/dutserver.go
+++ b/src/chromiumos/test/dut/cmd/cros-dut/dutserver.go
@@ -279,6 +279,33 @@
 	// by waiting for the client connection to shutdown or a timeout.
 	s.logger.Printf("Waiting for reboot to complete.")
 
+	// On cloudbots, the Wait() never returned even after the dut has
+	// already been rebooted. This caused timeout and test failures.
+	// By checking connection IsAlive() periodically, the Wait() does return and
+	// test can continue as expected.
+	isCloudbot := false
+	if id, found := os.LookupEnv("SWARMING_BOT_ID"); found && strings.HasPrefix(id, "cloudbots-") {
+		isCloudbot = true
+	}
+	if isCloudbot {
+		go func() {
+			t := time.NewTimer(15 * time.Second)
+			for {
+				select {
+				case <-t.C:
+					s.logger.Printf("IsConnectionAlive=%v", s.connection.IsAlive())
+					t.Reset(10 * time.Second)
+					return
+				case <-ctx.Done():
+					if !t.Stop() {
+						<-t.C
+					}
+					return
+				}
+			}
+		}()
+	}
+
 	wait := make(chan interface{})
 	go func() {
 		s.logger.Printf("Waiting for reboot: Connection wait.")