buildlet: ensure bytes are drained before network connection

This change ensures the bytes are drained before reading from the
network connection on the buildlet. This is a problem when repeated
connections are made to the buildlet and the buffer isn't always
read to completion.

Change-Id: I8e0ee3d5c7c036a62bae1b8083410d9d05847bf2
Reviewed-on: https://go-review.googlesource.com/c/build/+/815560
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/buildlet/buildletclient.go b/buildlet/buildletclient.go
index 433332b..c57e874 100644
--- a/buildlet/buildletclient.go
+++ b/buildlet/buildletclient.go
@@ -857,7 +857,16 @@
 		return nil, fmt.Errorf("unexpected /connect-ssh response: %v, %s", res.Status, slurp)
 	}
 	conn.SetDeadline(time.Time{})
-	return conn, nil
+	return &bufConn{Conn: conn, r: bufr}, nil
+}
+
+type bufConn struct {
+	net.Conn
+	r io.Reader
+}
+
+func (c *bufConn) Read(p []byte) (int, error) {
+	return c.r.Read(p)
 }
 
 func condRun(fn func()) {