mirror of
https://github.com/fatedier/frp.git
synced 2026-10-15 11:55:56 +08:00
fix(msg): close control connection on write failure (#5550)
* fix(msg): close control connection on write failure * fix(msg): handle shutdown enqueue race and preserve constructor API
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
// Copyright 2026 The frp Authors
|
||||
//
|
||||
// Licensed 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 msg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Preserve the original exported constructor function type.
|
||||
var _ func(ReadWriter) *Dispatcher = NewDispatcher
|
||||
|
||||
func TestDispatcherEnqueueRejectsStoppedDispatcher(t *testing.T) {
|
||||
c := newRegressionControl(t)
|
||||
d := NewDispatcher(c)
|
||||
d.stop()
|
||||
|
||||
// Model shutdown after Send's initial check: the stop and buffered send
|
||||
// cases are both ready. A send selected here must still return EOF.
|
||||
enqueued := false
|
||||
for range 100 {
|
||||
if err := d.enqueue(&ReqWorkConn{}); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("enqueue racing with shutdown: %v, want EOF", err)
|
||||
}
|
||||
if len(d.sendCh) > 0 {
|
||||
enqueued = true
|
||||
<-d.sendCh
|
||||
}
|
||||
}
|
||||
if !enqueued {
|
||||
t.Fatal("did not exercise the ready send case")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcherAcceptsReadWriterWithoutClose(t *testing.T) {
|
||||
t.Run("codec factory", func(t *testing.T) {
|
||||
d := NewDispatcher(NewV1ReadWriter(&bytes.Buffer{}))
|
||||
d.Run()
|
||||
regressionAwait(t, d.Done(), "non-closable codec EOF")
|
||||
})
|
||||
|
||||
t.Run("write failure", func(t *testing.T) {
|
||||
c := newRegressionControl(t)
|
||||
regressionWarm(t, c)
|
||||
// Hide Close to model an existing caller implementing only ReadWriter.
|
||||
d := NewDispatcher(struct{ ReadWriter }{c})
|
||||
d.Run()
|
||||
t.Cleanup(func() {
|
||||
c.transport.close()
|
||||
regressionAwait(t, d.Done(), "externally interrupted reader")
|
||||
})
|
||||
|
||||
c.transport.arm()
|
||||
if err := d.Send(&ReqWorkConn{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := regressionReceiveWrite(t, c); !errors.Is(err, errRegressionWriteTimeout) {
|
||||
t.Fatalf("injected write failure: %v", err)
|
||||
}
|
||||
regressionAwait(t, d.stopCh, "write failure stops sends")
|
||||
if err := d.Send(&ReqWorkConn{}); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("Send after write failure: %v, want EOF", err)
|
||||
}
|
||||
if c.closeCalls.Load() != 0 {
|
||||
t.Fatal("dispatcher closed a connection hidden by ReadWriter")
|
||||
}
|
||||
select {
|
||||
case <-d.Done():
|
||||
t.Fatal("Done closed before the non-closable reader returned")
|
||||
default:
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user