syncbase: Fix memory leak in runRemoteOp.

The runRemoteOp function takes a context and creates a child context.
If the rpc operation is successful, the child context is not cancelled,
and the caller is implicity required to cancel the parent.

In the clock code path, the context passed to runRemoteOp is never
cancelled, and so the child contexts accumulate.

This CL makes the clock create a new context with cancel, and cancels it
in a defer.

Change-Id: I853cb4eb4bd9de35e7be6f15c75cd0940fbfc24c
diff --git a/services/syncbase/vsync/clock.go b/services/syncbase/vsync/clock.go
index 96b904f..7598895 100644
--- a/services/syncbase/vsync/clock.go
+++ b/services/syncbase/vsync/clock.go
@@ -61,7 +61,7 @@
 // syncVClock syncs the local Syncbase vclock with peer's Syncbase vclock.
 // Works by treating the peer as an NTP server, obtaining a single sample, and
 // then updating the local VClockData as appropriate.
-func (s *syncService) syncVClock(ctx *context.T, peer connInfo) error {
+func (s *syncService) syncVClock(ctxIn *context.T, peer connInfo) error {
 	vlog.VI(2).Infof("sync: syncVClock: begin: contacting peer %v", peer)
 	defer vlog.VI(2).Infof("sync: syncVClock: end: contacting peer %v", peer)
 
@@ -73,6 +73,9 @@
 		return err
 	}
 
+	ctx, cancel := context.WithCancel(ctxIn)
+	defer cancel()
+
 	// TODO(sadovsky): Propagate the returned connInfo back to syncer so that we
 	// leverage what we learned here about available mount tables and endpoints
 	// when we continue on to the next initiation phase.
diff --git a/services/syncbase/vsync/initiator.go b/services/syncbase/vsync/initiator.go
index c49f428..1e42c4d 100644
--- a/services/syncbase/vsync/initiator.go
+++ b/services/syncbase/vsync/initiator.go
@@ -1258,6 +1258,8 @@
 	return updPeer, nil, verror.New(interfaces.ErrConnFail, ctx, "couldn't connect to peer", updPeer.relName, updPeer.addrs)
 }
 
+// runRemoteOp runs the remoteOp on the server specified by absName.
+// It is the caller's responsibility to cancel the ctx argument.
 func runRemoteOp(ctxIn *context.T, absName string, op remoteOp) (interface{}, error) {
 	vlog.VI(4).Infof("sync: runRemoteOp: begin for %v", absName)
 
@@ -1280,9 +1282,6 @@
 		err = verror.New(interfaces.ErrConnFail, ctx, "couldn't connect to peer", absName)
 	}
 
-	// When the RPC is successful, cancelling the parent context will take
-	// care of cancelling the child context. When the err is non-nil, we
-	// cancel the context here.
 	cancel()
 	return nil, err
 }