v.io/x/ref/runtime/internal/rpc: call context.CancelFunc's when discarding rpc's typeCache

This change adds a close() method to rpc's typeCache so that the
context.CancelFunc calls stored within it can be called when the
typeCache is being discarded.  This change is primarily to
silence the context leak detector when running tests.

Change-Id: I3b932e21311d0f273e3f52555326be1573df2518
diff --git a/runtime/internal/rpc/client.go b/runtime/internal/rpc/client.go
index 3c5d528..aae918d 100644
--- a/runtime/internal/rpc/client.go
+++ b/runtime/internal/rpc/client.go
@@ -136,6 +136,7 @@
 		c.wg.Wait()
 		c.outstanding.close()
 		close(c.closed)
+		c.typeCache.close()
 	}()
 
 	return c
diff --git a/runtime/internal/rpc/server.go b/runtime/internal/rpc/server.go
index 907bd96..f694f16 100644
--- a/runtime/internal/rpc/server.go
+++ b/runtime/internal/rpc/server.go
@@ -268,6 +268,7 @@
 		s.state = rpc.ServerStopped
 		s.Unlock()
 		close(s.closed)
+		s.typeCache.close()
 	}()
 	return s.ctx, s, nil
 }
diff --git a/runtime/internal/rpc/typecache.go b/runtime/internal/rpc/typecache.go
index 1911543..318b4d1 100644
--- a/runtime/internal/rpc/typecache.go
+++ b/runtime/internal/rpc/typecache.go
@@ -119,3 +119,18 @@
 		tc.mu.Unlock()
 	}
 }
+
+func (tc *typeCache) close() {
+	tc.mu.Lock()
+	for _, tce := range tc.flows {
+		if tce != nil {
+			if tce.cancel != nil {
+				tce.cancel()
+			}
+			if tce.dec != nil {
+				tce.dec.Stop()
+			}
+		}
+	}
+	tc.mu.Unlock()
+}