RPC: Don't allocate a new context in flow.SetDeadlineContext if
the flow is already closed.  This could cause a leak.

Change-Id: Ibd0ef2a7df02a4dd0fe368e1cf7e3fe0d4d207a1
diff --git a/runtime/internal/flow/conn/flow.go b/runtime/internal/flow/conn/flow.go
index f7107cf..6ab0f21 100644
--- a/runtime/internal/flow/conn/flow.go
+++ b/runtime/internal/flow/conn/flow.go
@@ -50,6 +50,8 @@
 	// release from the remote end.  This is always false for accepted flows.
 	borrowing bool
 
+	closed bool
+
 	writerList
 }
 
@@ -355,6 +357,12 @@
 	defer f.conn.mu.Unlock()
 	f.conn.mu.Lock()
 
+	if f.closed {
+		// If the flow is already closed, don't allocate a new
+		// context, we might end up leaking it.
+		return ctx
+	}
+
 	if f.cancel != nil {
 		f.cancel()
 	}
@@ -426,8 +434,11 @@
 func (f *flw) close(ctx *context.T, closedRemotely bool, err error) {
 	f.conn.mu.Lock()
 	cancel := f.cancel
+	closed := f.closed
+	f.closed = true
 	f.conn.mu.Unlock()
-	if f.q.close(ctx) {
+	if !closed {
+		f.q.close(ctx)
 		if f.ctx.V(2) {
 			ctx.Infof("closing %d(%p): %v", f.id, f, err)
 		}