Переглянути джерело

v3client: wrap watch ctxs with blank ctx

Printing the values in ctx.String() will data race if the value
is mutable and doesn't implement String(), which seems to be common.
Instead, just return a fixed string instead of computing it; v3client
watches don't need as much flexibility for creating separate strings,
so separate ctx strings probably aren't necessary at this point.

Fixes #7811
Anthony Romano 8 роки тому
батько
коміт
3ce31acda4
1 змінених файлів з 18 додано та 2 видалено
  1. 18 2
      etcdserver/api/v3client/v3client.go

+ 18 - 2
etcdserver/api/v3client/v3client.go

@@ -15,13 +15,14 @@
 package v3client
 package v3client
 
 
 import (
 import (
-	"context"
 	"time"
 	"time"
 
 
 	"github.com/coreos/etcd/clientv3"
 	"github.com/coreos/etcd/clientv3"
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver/api/v3rpc"
 	"github.com/coreos/etcd/etcdserver/api/v3rpc"
 	"github.com/coreos/etcd/proxy/grpcproxy/adapter"
 	"github.com/coreos/etcd/proxy/grpcproxy/adapter"
+
+	"golang.org/x/net/context"
 )
 )
 
 
 // New creates a clientv3 client that wraps an in-process EtcdServer. Instead
 // New creates a clientv3 client that wraps an in-process EtcdServer. Instead
@@ -37,7 +38,7 @@ func New(s *etcdserver.EtcdServer) *clientv3.Client {
 	c.Lease = clientv3.NewLeaseFromLeaseClient(lc, time.Second)
 	c.Lease = clientv3.NewLeaseFromLeaseClient(lc, time.Second)
 
 
 	wc := adapter.WatchServerToWatchClient(v3rpc.NewWatchServer(s))
 	wc := adapter.WatchServerToWatchClient(v3rpc.NewWatchServer(s))
-	c.Watcher = clientv3.NewWatchFromWatchClient(wc)
+	c.Watcher = &watchWrapper{clientv3.NewWatchFromWatchClient(wc)}
 
 
 	mc := adapter.MaintenanceServerToMaintenanceClient(v3rpc.NewMaintenanceServer(s))
 	mc := adapter.MaintenanceServerToMaintenanceClient(v3rpc.NewMaintenanceServer(s))
 	c.Maintenance = clientv3.NewMaintenanceFromMaintenanceClient(mc)
 	c.Maintenance = clientv3.NewMaintenanceFromMaintenanceClient(mc)
@@ -49,3 +50,18 @@ func New(s *etcdserver.EtcdServer) *clientv3.Client {
 
 
 	return c
 	return c
 }
 }
+
+// BlankContext implements Stringer on a context so the ctx string doesn't
+// depend on the context's WithValue data, which tends to be unsynchronized
+// (e.g., x/net/trace), causing ctx.String() to throw data races.
+type blankContext struct{ context.Context }
+
+func (*blankContext) String() string { return "(blankCtx)" }
+
+// watchWrapper wraps clientv3 watch calls to blank out the context
+// to avoid races on trace data.
+type watchWrapper struct{ clientv3.Watcher }
+
+func (ww *watchWrapper) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
+	return ww.Watcher.Watch(&blankContext{ctx}, key, opts...)
+}