avoid deadlock

This commit is contained in:
chrislu
2025-10-14 07:51:06 -07:00
parent fe9e0161d5
commit 73ebc69a82

View File

@@ -288,6 +288,11 @@ func (bc *BrokerClient) ReadRecordsFromOffset(ctx context.Context, session *Brok
} }
} }
// CRITICAL: Get the current offset atomically before making recreation decision
// We need to unlock first (lock acquired at line 257) then re-acquire for atomic read
currentStartOffset := session.StartOffset
session.mu.Unlock()
// CRITICAL FIX for Schema Registry: Keep subscriber alive across multiple fetch requests // CRITICAL FIX for Schema Registry: Keep subscriber alive across multiple fetch requests
// Schema Registry expects to make multiple poll() calls on the same consumer connection // Schema Registry expects to make multiple poll() calls on the same consumer connection
// //
@@ -299,12 +304,6 @@ func (bc *BrokerClient) ReadRecordsFromOffset(ctx context.Context, session *Brok
// The session will naturally advance as records are consumed, so we should NOT // The session will naturally advance as records are consumed, so we should NOT
// recreate it just because requestedOffset != session.StartOffset // recreate it just because requestedOffset != session.StartOffset
// CRITICAL: Re-check the offset under session lock to prevent race conditions
// Another thread might be reading from this session right now and advancing the offset
session.mu.Lock()
currentStartOffset := session.StartOffset
session.mu.Unlock()
if requestedOffset < currentStartOffset { if requestedOffset < currentStartOffset {
// Need to seek backward - close old session and create a fresh subscriber // Need to seek backward - close old session and create a fresh subscriber
// Restarting an existing stream doesn't work reliably because the broker may still // Restarting an existing stream doesn't work reliably because the broker may still