Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7aa3aa9

Browse files
committed
PR review fixes
1 parent fda6968 commit 7aa3aa9

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

agent/immortalstreams/backedpipe/backed_pipe.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (bp *BackedPipe) Close() error {
199199
func (bp *BackedPipe) Connected() bool {
200200
bp.mu.RLock()
201201
defer bp.mu.RUnlock()
202-
return bp.state == connected
202+
return bp.state == connected && bp.reader.Connected() && bp.writer.Connected()
203203
}
204204

205205
// reconnectLocked handles the reconnection logic. Must be called with write lock held.
@@ -223,47 +223,40 @@ func (bp *BackedPipe) reconnectLocked() error {
223223
bp.conn = nil
224224
}
225225

226-
// Gather local sequence numbers:
227-
// - readerSeqNum: how many bytes we've successfully read from remote (send to remote)
228-
// - writerSeqNum: how many bytes we've written so far (used for validation)
229-
readerSeqNum := bp.reader.SequenceNum()
230-
writerSeqNum := bp.writer.SequenceNum()
231-
232226
// Increment the generation and update both reader and writer.
233227
// We do it now to track even the connections that fail during
234228
// Reconnect.
235229
bp.connGen++
236230
bp.reader.SetGeneration(bp.connGen)
237231
bp.writer.SetGeneration(bp.connGen)
238232

239-
// Send our reader sequence number to the remote and receive its reader sequence
240-
// number. We will replay our outbound data from that point and backed pipe on the other
241-
// side will replay from our reader sequence number.
242-
conn, remoteReaderSeqNum, err := bp.reconnector.Reconnect(bp.ctx, readerSeqNum)
243-
if err != nil {
244-
return ErrReconnectFailed
245-
}
246-
247-
// Validate sequence numbers
248-
if remoteReaderSeqNum > writerSeqNum {
249-
_ = conn.Close()
250-
return ErrInvalidSequenceNumber
251-
}
252-
253233
// Reconnect reader and writer
254234
seqNum := make(chan uint64, 1)
255235
newR := make(chan io.Reader, 1)
256236

257237
go bp.reader.Reconnect(seqNum, newR)
258238

259-
// Get sequence number and send new reader
260-
<-seqNum
239+
// Get the precise reader sequence number from the reader while it holds its lock
240+
readerSeqNum, ok := <-seqNum
241+
if !ok {
242+
// Reader was closed during reconnection
243+
return ErrReconnectFailed
244+
}
245+
246+
// Perform reconnect using the exact sequence number we just received
247+
conn, remoteReaderSeqNum, err := bp.reconnector.Reconnect(bp.ctx, readerSeqNum)
248+
if err != nil {
249+
// Unblock reader reconnect
250+
newR <- nil
251+
return ErrReconnectFailed
252+
}
253+
254+
// Provide the new connection to the reader (reader still holds its lock)
261255
newR <- conn
262256

263257
// Replay our outbound data from the remote's reader sequence number
264-
err = bp.writer.Reconnect(remoteReaderSeqNum, conn)
265-
if err != nil {
266-
_ = conn.Close()
258+
writerReconnectErr := bp.writer.Reconnect(remoteReaderSeqNum, conn)
259+
if writerReconnectErr != nil {
267260
return ErrReconnectWriterFailed
268261
}
269262

0 commit comments

Comments
 (0)