Skip to content

Commit 02cd3b8

Browse files
yuhan6665nobodymmmray
authored
Fix SplitHTTP race condition when creating new sessions (#3533)
Co-authored-by: nobody <[email protected]> Co-authored-by: mmmray <[email protected]>
1 parent a7e198e commit 02cd3b8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

transport/internet/splithttp/hub.go

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type requestHandler struct {
2727
host string
2828
path string
2929
ln *Listener
30+
sessionMu *sync.Mutex
3031
sessions sync.Map
3132
localAddr gonet.TCPAddr
3233
}
@@ -56,11 +57,21 @@ func (h *requestHandler) maybeReapSession(isFullyConnected *done.Instance, sessi
5657
}
5758

5859
func (h *requestHandler) upsertSession(sessionId string) *httpSession {
60+
// fast path
5961
currentSessionAny, ok := h.sessions.Load(sessionId)
6062
if ok {
6163
return currentSessionAny.(*httpSession)
6264
}
6365

66+
// slow path
67+
h.sessionMu.Lock()
68+
defer h.sessionMu.Unlock()
69+
70+
currentSessionAny, ok = h.sessions.Load(sessionId)
71+
if ok {
72+
return currentSessionAny.(*httpSession)
73+
}
74+
6475
s := &httpSession{
6576
uploadQueue: NewUploadQueue(int(2 * h.ln.config.GetNormalizedMaxConcurrentUploads())),
6677
isFullyConnected: done.New(),
@@ -277,6 +288,7 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet
277288
host: shSettings.Host,
278289
path: shSettings.GetNormalizedPath(),
279290
ln: l,
291+
sessionMu: &sync.Mutex{},
280292
sessions: sync.Map{},
281293
localAddr: localAddr,
282294
}

transport/internet/splithttp/splithttp_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func Test_listenSHAndDial(t *testing.T) {
6363
}
6464

6565
common.Must(conn.Close())
66-
<-time.After(time.Second * 5)
6766
conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
67+
6868
common.Must(err)
6969
_, err = conn.Write([]byte("Test connection 2"))
7070
common.Must(err)

0 commit comments

Comments
 (0)