Skip to content

Commit

Permalink
fix nilp error by #102 for tls conn (again) (#109)
Browse files Browse the repository at this point in the history
* fix nilp error by #102 for tls conn

* extend recv time for GOMAXPROCS=1 env
  • Loading branch information
t-yuki authored and luluzhao committed May 9, 2019
1 parent 2d95a62 commit 5db6c01
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions xray/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,49 @@ func TestRoundTripReuseDatarace(t *testing.T) {
wg.Wait()
}

func TestRoundTripReuseTLSDatarace(t *testing.T) {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := []byte(`200 - Nothing to see`)
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
defer ts.Close()

certpool := x509.NewCertPool()
certpool.AddCert(ts.Certificate())
tr := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certpool,
},
}
rt := &roundtripper{
Base: tr,
}

wg := sync.WaitGroup{}
n := 30
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
reader := strings.NewReader("")
ctx, root := BeginSegment(context.Background(), "Test")
req, _ := http.NewRequest("GET", ts.URL, reader)
req = req.WithContext(ctx)
res, err := rt.RoundTrip(req)
assert.NoError(t, err)
ioutil.ReadAll(res.Body)
res.Body.Close() // make net/http/transport.go connection reuse
root.Close(nil)
}()
}
for i := 0; i < n; i++ {
_, e := TestDaemon.Recv()
assert.NoError(t, e)
}
wg.Wait()
}

func TestRoundTripHttp2Datarace(t *testing.T) {
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := []byte(`200 - Nothing to see`)
Expand Down
2 changes: 1 addition & 1 deletion xray/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (xt *HTTPSubsegments) ConnectDone(network, addr string, err error) {
// TLSHandshakeStart begins a tls subsegment if the HTTP operation
// subsegment is still in progress.
func (xt *HTTPSubsegments) TLSHandshakeStart() {
if GetSegment(xt.opCtx).safeInProgress() {
if GetSegment(xt.opCtx).safeInProgress() && xt.connCtx != nil {
xt.tlsCtx, _ = BeginSubsegment(xt.connCtx, "tls")
}
}
Expand Down
2 changes: 1 addition & 1 deletion xray/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (td *Testdaemon) Run() {
}

func (td *Testdaemon) Recv() (*Segment, error) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
select {
case r := <-td.Channel:
Expand Down

0 comments on commit 5db6c01

Please sign in to comment.