From 4c23e6fa5577a722b985ecc1fae8be68a13f5752 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Tue, 2 Apr 2019 21:34:21 +0100 Subject: [PATCH] rpc: Avoid using Pool since it conflicts with http2 (#7467) A race is detected between a bytes.Buffer generated with cmd/rpc.Pool and http2 module. An issue is raised in golang (https://github.com/golang/go/issues/31192). Meanwhile, this commit disables Pool in RPC code and it generates a new 1kb of bytes.Buffer for each RPC call. --- cmd/rpc/client.go | 7 +++---- cmd/rpc/server.go | 6 ++---- cmd/rpc/server_test.go | 3 +-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cmd/rpc/client.go b/cmd/rpc/client.go index cc61d21b3..5de84bf31 100644 --- a/cmd/rpc/client.go +++ b/cmd/rpc/client.go @@ -17,6 +17,7 @@ package rpc import ( + "bytes" "context" "crypto/tls" "encoding/gob" @@ -49,8 +50,7 @@ func (client *Client) Call(serviceMethod string, args, reply interface{}) error return fmt.Errorf("rpc reply must be a pointer type, but found %v", replyKind) } - argBuf := bufPool.Get() - defer bufPool.Put(argBuf) + argBuf := bytes.NewBuffer(make([]byte, 0, 1024)) if err := gobEncodeBuf(args, argBuf); err != nil { return err @@ -61,8 +61,7 @@ func (client *Client) Call(serviceMethod string, args, reply interface{}) error ArgBytes: argBuf.Bytes(), } - reqBuf := bufPool.Get() - defer bufPool.Put(reqBuf) + reqBuf := bytes.NewBuffer(make([]byte, 0, 1024)) if err := gob.NewEncoder(reqBuf).Encode(callRequest); err != nil { return err } diff --git a/cmd/rpc/server.go b/cmd/rpc/server.go index ba6c98107..5e7329684 100644 --- a/cmd/rpc/server.go +++ b/cmd/rpc/server.go @@ -40,8 +40,6 @@ var errorType = reflect.TypeOf((*error)(nil)).Elem() // reflect.Type of Authenticator interface. var authenticatorType = reflect.TypeOf((*Authenticator)(nil)).Elem() -var bufPool = NewPool() - func gobEncodeBuf(e interface{}, buf *bytes.Buffer) error { return gob.NewEncoder(buf).Encode(e) } @@ -239,8 +237,8 @@ func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { } callResponse := CallResponse{} - buf := bufPool.Get() - defer bufPool.Put(buf) + + buf := bytes.NewBuffer(make([]byte, 0, 1024)) if err := server.call(callRequest.Method, callRequest.ArgBytes, buf); err != nil { callResponse.Error = err.Error() diff --git a/cmd/rpc/server_test.go b/cmd/rpc/server_test.go index f2912d104..2a4fb5cea 100644 --- a/cmd/rpc/server_test.go +++ b/cmd/rpc/server_test.go @@ -253,8 +253,7 @@ func TestServerCall(t *testing.T) { } for i, testCase := range testCases { - buf := bufPool.Get() - defer bufPool.Put(buf) + buf := bytes.NewBuffer([]byte{}) err := testCase.server.call(testCase.serviceMethod, testCase.argBytes, buf) expectErr := (err != nil)