Properly close gRPC clients in nearby share remote fixture

The last few remote tests that run are constantly failing due to SSH
connection failures. The issue appears to be that we are reaching the
limit for the # of open SSH connections on the DUT. This is caused by
leaving gRPC client connections open between fixtures, so we need to
properly close those when we are done with them.

BUG=b:220942040
TEST=tast run -companiondut=cd1:<dut1> <dut2> '("group:nearby-share-remote")'

Change-Id: I35f0a1bada2e908bf31361d93868527fd38ee9be
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/tast-tests/+/3522757
Tested-by: Kyle Shimabukuro <kyleshima@chromium.org>
Auto-Submit: Kyle Shimabukuro <kyleshima@chromium.org>
Reviewed-by: Kshitij Pancholi <panchok@google.com>
(cherry picked from commit 5f7271a3d8c25c7fef788e0f5f2c3e797d4e5d3b)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/tast-tests/+/3561115
Tested-by: David Haddock <dhaddock@chromium.org>
Reviewed-by: Kyle Shimabukuro <kyleshima@chromium.org>
Commit-Queue: David Haddock <dhaddock@chromium.org>
diff --git a/src/chromiumos/tast/remote/cros/nearbyshare/fixture.go b/src/chromiumos/tast/remote/cros/nearbyshare/fixture.go
index f4cce2b..bca6127 100644
--- a/src/chromiumos/tast/remote/cros/nearbyshare/fixture.go
+++ b/src/chromiumos/tast/remote/cros/nearbyshare/fixture.go
@@ -282,6 +282,11 @@
 
 	// Attributes for both chromebooks.
 	attributes []byte
+
+	// RPC connections to the chromebooks. They are initialized in SetUp,
+	// and must be closed in TearDown to free the underlying SSH connections.
+	senderRPCClient   *rpc.Client
+	receiverRPCClient *rpc.Client
 }
 
 // FixtData holds information made available to tests that specify this Fixture.
@@ -346,6 +351,7 @@
 	if err != nil {
 		s.Fatal("Failed to connect to the RPC service on the DUT: ", err)
 	}
+	f.senderRPCClient = cl1
 	const crosBaseName = "cros_test"
 	senderDisplayName := nearbycommon.RandomDeviceName(crosBaseName)
 	s.Log("Enabling Nearby Share on DUT1 (Sender). Name: ", senderDisplayName)
@@ -362,6 +368,7 @@
 	if err != nil {
 		s.Fatal("Failed to dial rpc service on DUT2: ", err)
 	}
+	f.receiverRPCClient = cl2
 	receiverDisplayName := nearbycommon.RandomDeviceName(crosBaseName)
 	s.Log("Enabling Nearby Share on DUT2 (Receiver). Name: ", receiverDisplayName)
 	receiverUsername := s.RequiredVar("nearbyshare.cros2_username")
@@ -447,6 +454,13 @@
 	if _, err := f.receiver.CloseChrome(ctx, &empty.Empty{}); err != nil {
 		s.Error("Failed to shutdown nearby share service connections for receiver: ", err)
 	}
+	// Close gRPC clients to free underlying SSH resources.
+	if err := f.senderRPCClient.Close(ctx); err != nil {
+		s.Error("Failed to close gRPC client for sender: ", err)
+	}
+	if err := f.receiverRPCClient.Close(ctx); err != nil {
+		s.Error("Failed to close gRPC client for receiver: ", err)
+	}
 }
 
 func (f *nearbyShareFixture) Reset(ctx context.Context) error { return nil }