|
1 | 1 | package replication |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net" |
4 | 7 | "os" |
5 | 8 | "strings" |
| 9 | + "sync" |
6 | 10 | "testing" |
| 11 | + "time" |
7 | 12 |
|
8 | 13 | "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/go-mysql-org/go-mysql/client" |
| 16 | + "github.com/go-mysql-org/go-mysql/packet" |
9 | 17 | ) |
10 | 18 |
|
11 | 19 | func TestLocalHostname(t *testing.T) { |
@@ -48,3 +56,70 @@ func TestLocalHostname_os(t *testing.T) { |
48 | 56 | h, _ := os.Hostname() |
49 | 57 | require.Equal(t, h, b.localHostname()) |
50 | 58 | } |
| 59 | + |
| 60 | +// deadlinelessConn mimics the net.Conn surface of an ssh tunneled channel: |
| 61 | +// Read blocks until Close, and SetReadDeadline refuses with an error rather |
| 62 | +// than interrupting the parked Read. |
| 63 | +type deadlinelessConn struct { |
| 64 | + closeOnce sync.Once |
| 65 | + closed chan struct{} |
| 66 | +} |
| 67 | + |
| 68 | +func newDeadlinelessConn() *deadlinelessConn { |
| 69 | + return &deadlinelessConn{closed: make(chan struct{})} |
| 70 | +} |
| 71 | + |
| 72 | +func (c *deadlinelessConn) Read(b []byte) (int, error) { |
| 73 | + <-c.closed |
| 74 | + return 0, io.EOF |
| 75 | +} |
| 76 | + |
| 77 | +func (c *deadlinelessConn) Write(b []byte) (int, error) { return len(b), nil } |
| 78 | + |
| 79 | +func (c *deadlinelessConn) Close() error { |
| 80 | + c.closeOnce.Do(func() { close(c.closed) }) |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (*deadlinelessConn) LocalAddr() net.Addr { return &net.TCPAddr{} } |
| 85 | +func (*deadlinelessConn) RemoteAddr() net.Addr { return &net.TCPAddr{} } |
| 86 | +func (*deadlinelessConn) SetDeadline(time.Time) error { return errors.New("deadline not supported") } |
| 87 | +func (*deadlinelessConn) SetReadDeadline(time.Time) error { |
| 88 | + return errors.New("deadline not supported") |
| 89 | +} |
| 90 | +func (*deadlinelessConn) SetWriteDeadline(time.Time) error { |
| 91 | + return errors.New("deadline not supported") |
| 92 | +} |
| 93 | + |
| 94 | +// TestCloseUnblocksWhenSetReadDeadlineFails exercises the deadlock path where |
| 95 | +// SetReadDeadline cannot unblock the binlog reader (e.g. ssh tunnel) and KILL |
| 96 | +// also fails to reach the server (thread already reaped). Under the previous |
| 97 | +// behaviour close() parked indefinitely on wg.Wait. |
| 98 | +func TestCloseUnblocksWhenSetReadDeadlineFails(t *testing.T) { |
| 99 | + b := NewBinlogSyncer(BinlogSyncerConfig{ServerID: 1}) |
| 100 | + |
| 101 | + fake := newDeadlinelessConn() |
| 102 | + b.c = &client.Conn{Conn: packet.NewConn(fake)} |
| 103 | + b.running = true |
| 104 | + |
| 105 | + // Mimic onStream's parked ReadPacket: block until the underlying conn is |
| 106 | + // closed, then honour ctx cancellation before signalling wg.Done. |
| 107 | + b.wg.Add(1) |
| 108 | + go func() { |
| 109 | + defer b.wg.Done() |
| 110 | + _, _ = b.c.ReadPacket() |
| 111 | + <-b.ctx.Done() |
| 112 | + }() |
| 113 | + |
| 114 | + done := make(chan struct{}) |
| 115 | + go func() { |
| 116 | + b.Close() |
| 117 | + close(done) |
| 118 | + }() |
| 119 | + |
| 120 | + select { |
| 121 | + case <-done: |
| 122 | + case <-time.After(5 * time.Second): |
| 123 | + t.Fatal("Close hung when SetReadDeadline refused a deadline") |
| 124 | + } |
| 125 | +} |
0 commit comments