Skip to content

Commit aaa2085

Browse files
committed
source-postgres: Fix watchdog timeout error reporting
The intent of this logic was that if and only if the replication streaming is interrupted by the "we really should have reached the established fence by now" watchdog timeout, we ought to report an appropriate error. But I was checking the cancellation cause on the wrong context, so that wouldn't have worked as intended, and also it turns out that `cancel(nil)` sets the cause to `Canceled` so a bit more logic is required to turn that into a nil error return from the streaming operation anyway.
1 parent 36c3e02 commit aaa2085

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

source-postgres/replication.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ func (s *replicationStream) StreamToFence(ctx context.Context, fenceAfter time.D
348348

349349
// Stream replication events until the fence is reached or the watchdog timeout hits.
350350
var relayCtx, cancelRelayCtx = context.WithCancelCause(ctx)
351+
var fenceReached = errors.New("fenced reached")
351352
defer cancelRelayCtx(nil)
352353

353354
// Given that the early-exit fast path was not taken, there must be further data for
@@ -387,12 +388,15 @@ func (s *replicationStream) StreamToFence(ctx context.Context, fenceAfter time.D
387388
if eventLSN >= fenceLSN {
388389
logrus.WithField("cursor", eventLSN.String()).Debug("finished fenced streaming phase")
389390
s.previousFenceLSN = eventLSN
390-
cancelRelayCtx(nil) // Stop the relay loop so we can exit cleanly. A nil cause means no error.
391+
cancelRelayCtx(fenceReached) // Stop the relay loop so we can exit cleanly.
391392
}
392393
}
393394
return nil
394395
}); errors.Is(err, context.Canceled) {
395-
return context.Cause(ctx)
396+
if cause := context.Cause(relayCtx); !errors.Is(cause, fenceReached) {
397+
return cause
398+
}
399+
return nil
396400
} else if err != nil {
397401
return err
398402
}

0 commit comments

Comments
 (0)