You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2017/07/01 14:02:28 UTC
[1/2] thrift git commit: THRIFT-4240 Fix use of sync.Once in Go
TSimpleServer Stop() method Client: Go Patch: Zachary Wasserman
Repository: thrift
Updated Branches:
refs/heads/master c0e353500 -> c1794358b
THRIFT-4240 Fix use of sync.Once in Go TSimpleServer Stop() method
Client: Go
Patch: Zachary Wasserman <za...@gmail.com>
This closes #1301
Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/b771cbed
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/b771cbed
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/b771cbed
Branch: refs/heads/master
Commit: b771cbedd6e7af4b2886eabea4bbc47aa860449a
Parents: c0e3535
Author: Zachary Wasserman <za...@gmail.com>
Authored: Tue Jun 27 12:03:39 2017 -0700
Committer: Jens Geyer <je...@apache.org>
Committed: Sat Jul 1 16:00:02 2017 +0200
----------------------------------------------------------------------
lib/go/thrift/simple_server.go | 5 +-
lib/go/thrift/simple_server_test.go | 98 ++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/thrift/blob/b771cbed/lib/go/thrift/simple_server.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/simple_server.go b/lib/go/thrift/simple_server.go
index be031e8..3381e5b 100644
--- a/lib/go/thrift/simple_server.go
+++ b/lib/go/thrift/simple_server.go
@@ -32,6 +32,7 @@ import (
*/
type TSimpleServer struct {
quit chan struct{}
+ once sync.Once
processorFactory TProcessorFactory
serverTransport TServerTransport
@@ -155,15 +156,13 @@ func (p *TSimpleServer) Serve() error {
return nil
}
-var once sync.Once
-
func (p *TSimpleServer) Stop() error {
q := func() {
close(p.quit)
p.serverTransport.Interrupt()
p.Wait()
}
- once.Do(q)
+ p.once.Do(q)
return nil
}
http://git-wip-us.apache.org/repos/asf/thrift/blob/b771cbed/lib/go/thrift/simple_server_test.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/simple_server_test.go b/lib/go/thrift/simple_server_test.go
new file mode 100644
index 0000000..068f3cc
--- /dev/null
+++ b/lib/go/thrift/simple_server_test.go
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package thrift
+
+import (
+ "testing"
+)
+
+type mockProcessor struct {
+ ProcessFunc func(in, out TProtocol) (bool, TException)
+}
+
+func (m *mockProcessor) Process(in, out TProtocol) (bool, TException) {
+ return m.ProcessFunc(in, out)
+}
+
+type mockServerTransport struct {
+ ListenFunc func() error
+ AcceptFunc func() (TTransport, error)
+ CloseFunc func() error
+ InterruptFunc func() error
+}
+
+func (m *mockServerTransport) Listen() error {
+ return m.ListenFunc()
+}
+
+func (m *mockServerTransport) Accept() (TTransport, error) {
+ return m.AcceptFunc()
+}
+
+func (m *mockServerTransport) Close() error {
+ return m.CloseFunc()
+}
+
+func (m *mockServerTransport) Interrupt() error {
+ return m.InterruptFunc()
+}
+
+func TestMultipleStop(t *testing.T) {
+ proc := &mockProcessor{
+ ProcessFunc: func(in, out TProtocol) (bool, TException) {
+ return false, nil
+ },
+ }
+
+ var interruptCalled bool
+ c := make(chan struct{})
+ trans := &mockServerTransport{
+ ListenFunc: func() error {
+ return nil
+ },
+ AcceptFunc: func() (TTransport, error) {
+ <-c
+ return nil, nil
+ },
+ CloseFunc: func() error {
+ c <- struct{}{}
+ return nil
+ },
+ InterruptFunc: func() error {
+ interruptCalled = true
+ return nil
+ },
+ }
+
+ serv := NewTSimpleServer2(proc, trans)
+ go serv.Serve()
+ serv.Stop()
+ if !interruptCalled {
+ t.Error("first server transport should have been interrupted")
+ }
+
+ serv = NewTSimpleServer2(proc, trans)
+ interruptCalled = false
+ go serv.Serve()
+ serv.Stop()
+ if !interruptCalled {
+ t.Error("second server transport should have been interrupted")
+ }
+}
[2/2] thrift git commit: THRIFT-4243 Fix Go TSimpleServer race on
wait in Stop() method Client: Go Patch: Zachary Wasserman
Posted by je...@apache.org.
THRIFT-4243 Fix Go TSimpleServer race on wait in Stop() method
Client: Go
Patch: Zachary Wasserman <za...@gmail.com>
This closes #1302
Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/c1794358
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/c1794358
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/c1794358
Branch: refs/heads/master
Commit: c1794358bbf755f7bccacefb6a325804aa195d65
Parents: b771cbe
Author: Zachary Wasserman <za...@gmail.com>
Authored: Thu Jun 29 17:15:01 2017 -0700
Committer: Jens Geyer <je...@apache.org>
Committed: Sat Jul 1 16:01:12 2017 +0200
----------------------------------------------------------------------
lib/go/thrift/simple_server.go | 39 ++++++++++++++++----------------
lib/go/thrift/simple_server_test.go | 37 ++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/thrift/blob/c1794358/lib/go/thrift/simple_server.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/simple_server.go b/lib/go/thrift/simple_server.go
index 3381e5b..72541b6 100644
--- a/lib/go/thrift/simple_server.go
+++ b/lib/go/thrift/simple_server.go
@@ -23,6 +23,7 @@ import (
"log"
"runtime/debug"
"sync"
+ "sync/atomic"
)
/*
@@ -31,8 +32,9 @@ import (
* This will work if golang user implements a conn-pool like thing in client side.
*/
type TSimpleServer struct {
- quit chan struct{}
- once sync.Once
+ closed int32
+ wg sync.WaitGroup
+ mu sync.Mutex
processorFactory TProcessorFactory
serverTransport TServerTransport
@@ -40,7 +42,6 @@ type TSimpleServer struct {
outputTransportFactory TTransportFactory
inputProtocolFactory TProtocolFactory
outputProtocolFactory TProtocolFactory
- sync.WaitGroup
}
func NewTSimpleServer2(processor TProcessor, serverTransport TServerTransport) *TSimpleServer {
@@ -93,7 +94,6 @@ func NewTSimpleServerFactory6(processorFactory TProcessorFactory, serverTranspor
outputTransportFactory: outputTransportFactory,
inputProtocolFactory: inputProtocolFactory,
outputProtocolFactory: outputProtocolFactory,
- quit: make(chan struct{}, 1),
}
}
@@ -128,22 +128,23 @@ func (p *TSimpleServer) Listen() error {
func (p *TSimpleServer) AcceptLoop() error {
for {
client, err := p.serverTransport.Accept()
+ p.mu.Lock()
+ if atomic.LoadInt32(&p.closed) != 0 {
+ return nil
+ }
if err != nil {
- select {
- case <-p.quit:
- return nil
- default:
- }
return err
}
if client != nil {
- p.Add(1)
+ p.wg.Add(1)
go func() {
+ defer p.wg.Done()
if err := p.processRequests(client); err != nil {
log.Println("error processing request:", err)
}
}()
}
+ p.mu.Unlock()
}
}
@@ -157,18 +158,18 @@ func (p *TSimpleServer) Serve() error {
}
func (p *TSimpleServer) Stop() error {
- q := func() {
- close(p.quit)
- p.serverTransport.Interrupt()
- p.Wait()
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ if atomic.LoadInt32(&p.closed) != 0 {
+ return nil
}
- p.once.Do(q)
+ atomic.StoreInt32(&p.closed, 1)
+ p.serverTransport.Interrupt()
+ p.wg.Wait()
return nil
}
func (p *TSimpleServer) processRequests(client TTransport) error {
- defer p.Done()
-
processor := p.processorFactory.GetProcessor(client)
inputTransport, err := p.inputTransportFactory.GetTransport(client)
if err != nil {
@@ -193,10 +194,8 @@ func (p *TSimpleServer) processRequests(client TTransport) error {
defer outputTransport.Close()
}
for {
- select {
- case <-p.quit:
+ if atomic.LoadInt32(&p.closed) != 0 {
return nil
- default:
}
ok, err := processor.Process(inputProtocol, outputProtocol)
http://git-wip-us.apache.org/repos/asf/thrift/blob/c1794358/lib/go/thrift/simple_server_test.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/simple_server_test.go b/lib/go/thrift/simple_server_test.go
index 068f3cc..8763a3b 100644
--- a/lib/go/thrift/simple_server_test.go
+++ b/lib/go/thrift/simple_server_test.go
@@ -21,6 +21,7 @@ package thrift
import (
"testing"
+ "time"
)
type mockProcessor struct {
@@ -54,6 +55,14 @@ func (m *mockServerTransport) Interrupt() error {
return m.InterruptFunc()
}
+type mockTTransport struct {
+ TTransport
+}
+
+func (m *mockTTransport) Close() error {
+ return nil
+}
+
func TestMultipleStop(t *testing.T) {
proc := &mockProcessor{
ProcessFunc: func(in, out TProtocol) (bool, TException) {
@@ -96,3 +105,31 @@ func TestMultipleStop(t *testing.T) {
t.Error("second server transport should have been interrupted")
}
}
+
+func TestWaitRace(t *testing.T) {
+ proc := &mockProcessor{
+ ProcessFunc: func(in, out TProtocol) (bool, TException) {
+ return false, nil
+ },
+ }
+
+ trans := &mockServerTransport{
+ ListenFunc: func() error {
+ return nil
+ },
+ AcceptFunc: func() (TTransport, error) {
+ return &mockTTransport{}, nil
+ },
+ CloseFunc: func() error {
+ return nil
+ },
+ InterruptFunc: func() error {
+ return nil
+ },
+ }
+
+ serv := NewTSimpleServer2(proc, trans)
+ go serv.Serve()
+ time.Sleep(1)
+ serv.Stop()
+}