mirror of
https://github.com/SagerNet/sing-mux.git
synced 2025-04-05 21:07:44 +03:00
Fix h2 conn race
This commit is contained in:
parent
9836fc9b05
commit
1ebe6bb266
1 changed files with 10 additions and 8 deletions
18
h2mux.go
18
h2mux.go
|
@ -106,8 +106,9 @@ func (s *h2MuxServerSession) CanTakeNewRequest() bool {
|
||||||
type h2MuxConnWrapper struct {
|
type h2MuxConnWrapper struct {
|
||||||
N.ExtendedConn
|
N.ExtendedConn
|
||||||
flusher http.Flusher
|
flusher http.Flusher
|
||||||
done chan struct{}
|
|
||||||
access sync.Mutex
|
access sync.Mutex
|
||||||
|
closed bool
|
||||||
|
done chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHTTP2Wrapper(conn net.Conn, flusher http.Flusher) *h2MuxConnWrapper {
|
func newHTTP2Wrapper(conn net.Conn, flusher http.Flusher) *h2MuxConnWrapper {
|
||||||
|
@ -119,10 +120,10 @@ func newHTTP2Wrapper(conn net.Conn, flusher http.Flusher) *h2MuxConnWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *h2MuxConnWrapper) Write(p []byte) (n int, err error) {
|
func (w *h2MuxConnWrapper) Write(p []byte) (n int, err error) {
|
||||||
select {
|
w.access.Lock()
|
||||||
case <-w.done:
|
defer w.access.Unlock()
|
||||||
|
if w.closed {
|
||||||
return 0, net.ErrClosed
|
return 0, net.ErrClosed
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
n, err = w.ExtendedConn.Write(p)
|
n, err = w.ExtendedConn.Write(p)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -132,10 +133,10 @@ func (w *h2MuxConnWrapper) Write(p []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *h2MuxConnWrapper) WriteBuffer(buffer *buf.Buffer) error {
|
func (w *h2MuxConnWrapper) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
select {
|
w.access.Lock()
|
||||||
case <-w.done:
|
defer w.access.Unlock()
|
||||||
|
if w.closed {
|
||||||
return net.ErrClosed
|
return net.ErrClosed
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
err := w.ExtendedConn.WriteBuffer(buffer)
|
err := w.ExtendedConn.WriteBuffer(buffer)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -146,12 +147,13 @@ func (w *h2MuxConnWrapper) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
|
|
||||||
func (w *h2MuxConnWrapper) Close() error {
|
func (w *h2MuxConnWrapper) Close() error {
|
||||||
w.access.Lock()
|
w.access.Lock()
|
||||||
defer w.access.Unlock()
|
|
||||||
select {
|
select {
|
||||||
case <-w.done:
|
case <-w.done:
|
||||||
default:
|
default:
|
||||||
close(w.done)
|
close(w.done)
|
||||||
}
|
}
|
||||||
|
w.closed = true
|
||||||
|
w.access.Unlock()
|
||||||
return w.ExtendedConn.Close()
|
return w.ExtendedConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue