1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00

http transport data race issue (#2436)

* [fix] #2431 http transport data race issue
This commit is contained in:
Johnson C 2022-02-23 11:55:18 +08:00 committed by GitHub
parent 21bac54ad2
commit 5a2f37c718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 4 deletions

View File

@ -74,10 +74,10 @@ func (r *rpcStream) Send(msg interface{}) error {
func (r *rpcStream) Recv(msg interface{}) error { func (r *rpcStream) Recv(msg interface{}) error {
r.Lock() r.Lock()
defer r.Unlock()
if r.isClosed() { if r.isClosed() {
r.err = errShutdown r.err = errShutdown
r.Unlock()
return errShutdown return errShutdown
} }
@ -89,9 +89,11 @@ func (r *rpcStream) Recv(msg interface{}) error {
if err != nil { if err != nil {
if err == io.EOF && !r.isClosed() { if err == io.EOF && !r.isClosed() {
r.err = io.ErrUnexpectedEOF r.err = io.ErrUnexpectedEOF
r.Unlock()
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
r.err = err r.err = err
r.Unlock()
return err return err
} }
@ -120,6 +122,7 @@ func (r *rpcStream) Recv(msg interface{}) error {
} }
} }
r.Unlock()
return r.err return r.err
} }

View File

@ -35,9 +35,10 @@ type httpTransportClient struct {
sync.RWMutex sync.RWMutex
// request must be stored for response processing // request must be stored for response processing
r chan *http.Request r chan *http.Request
bl []*http.Request bl []*http.Request
buff *bufio.Reader buff *bufio.Reader
closed bool
// local/remote ip // local/remote ip
local string local string
@ -138,7 +139,12 @@ func (h *httpTransportClient) Recv(m *Message) error {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Timeout)) h.conn.SetDeadline(time.Now().Add(h.ht.opts.Timeout))
} }
h.Lock()
if h.closed {
return io.EOF
}
rsp, err := http.ReadResponse(h.buff, r) rsp, err := http.ReadResponse(h.buff, r)
h.Unlock()
if err != nil { if err != nil {
return err return err
} }
@ -174,6 +180,7 @@ func (h *httpTransportClient) Close() error {
h.once.Do(func() { h.once.Do(func() {
h.Lock() h.Lock()
h.buff.Reset(nil) h.buff.Reset(nil)
h.closed = true
h.Unlock() h.Unlock()
close(h.r) close(h.r)
}) })

View File

@ -1,8 +1,10 @@
package transport package transport
import ( import (
"fmt"
"io" "io"
"net" "net"
"sync"
"testing" "testing"
"time" "time"
) )
@ -244,3 +246,77 @@ func TestHTTPTransportTimeout(t *testing.T) {
<-done <-done
} }
func TestHTTPTransportCloseWhenRecv(t *testing.T) {
tr := NewHTTPTransport()
l, err := tr.Listen("127.0.0.1:0")
if err != nil {
t.Errorf("Unexpected listen err: %v", err)
}
defer l.Close()
fn := func(sock Socket) {
defer sock.Close()
for {
var m Message
if err := sock.Recv(&m); err != nil {
return
}
if err := sock.Send(&m); err != nil {
return
}
}
}
done := make(chan bool)
go func() {
if err := l.Accept(fn); err != nil {
select {
case <-done:
default:
t.Errorf("Unexpected accept err: %v", err)
}
}
}()
c, err := tr.Dial(l.Addr())
if err != nil {
t.Errorf("Unexpected dial err: %v", err)
}
defer c.Close()
m := Message{
Header: map[string]string{
"Content-Type": "application/json",
},
Body: []byte(`{"message": "Hello World"}`),
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
var rm Message
if err := c.Recv(&rm); err != nil {
if err == io.EOF {
return
}
t.Errorf("Unexpected recv err: %v", err)
}
fmt.Println("aa")
}
}()
for i := 1; i < 3; i++ {
if err := c.Send(&m); err != nil {
t.Errorf("Unexpected send err: %v", err)
}
}
close(done)
c.Close()
wg.Wait()
}