Repository navigation
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR introduces a per-reader PendingCheckpoints registry for SqsIO to support finalizing restored checkpoints, catching up unfinalized earlier checkpoints, finalizing resumed checkpoints on reader creation, and timing out unfinalized checkpoints when closing the reader. However, there are a couple of thread-safety and concurrency issues around sqsClient lifecycle management during concurrent finalization/close timeouts and inFlight map access during delete().
🔍 General Feedback
- Synchronizing
maybeCloseClient()and introducing the 60s close timeout creates a race condition with concurrentfinalizeCheckpoint()calls, wheresqsClientcan be closed and set tonulljust before or duringpending.reader.delete(...). - Since
finalizeCheckpoint()can now be invoked fromSqsUnboundedSource.createReader()or asynchronously on restored checkpoints while the original reader or another thread is active, unsynchronized access to theLinkedHashMapinFlightindelete()alongside mutations inadvance()risksConcurrentModificationExceptionor corrupted internal map state.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
| } | ||
| } | ||
| try { | ||
| pending.reader.delete(new ArrayList<>(toDelete)); |
There was a problem hiding this comment.
🟠 High: pending.reader.delete(...) is called outside closeLock and before numInFlightCheckpoints is decremented, while the close timeout task in SqsUnboundedReader.close() (or a concurrent finalizeCheckpoint() after timeout) can concurrently decrement numInFlightCheckpoints to <= 0 and call maybeCloseClient(), setting sqsClient = null and closing the client. If a late finalizeCheckpoint() races with the close timeout:
finalizeCheckpoint()callspending.removeUpTo(sequence)and gets the checkpoints.- The close timeout task fires, calls
pendingCheckpoints.removeAll()(now empty), and callsmaybeCloseClient(). - If another checkpoint finishes or if the timeout task closes
sqsClient,pending.reader.delete(...)will throw aNullPointerExceptionwhen dereferencingsqsClientindeleteBatch(or use a closed client).
Even simpler: if checkpoint 1 is removed by removeUpTo(1) right before the timeout task runs, and checkpoint 2 was still in pendingCheckpoints, the timeout task removes checkpoint 2, decrements numInFlightCheckpoints by 1, and then when checkpoint 1 finishes delete() and enters finally, clearAndDecrement() decrements numInFlightCheckpoints to 0 and maybeCloseClient() closes sqsClient. Conversely, if only checkpoint 1 existed, removeUpTo(1) empties pendingCheckpoints, then the timeout task runs removeAll() (returns 0 items) and calls maybeCloseClient() (does not close yet because numInFlightCheckpoints is still 1), OR if abandon in the timeout task clears checkpoints without coordinating with in-progress delete() calls, clearAndDecrement() on an abandoned checkpoint that was concurrently removed would double-decrement numInFlightCheckpoints if not careful. Moreover, in SqsUnboundedReader.close(), if the timeout fires while no removeUpTo is running, it calls pendingCheckpoints.removeAll(), decrements numInFlightCheckpoints to 0, and closes sqsClient, but does NOT clear safeToDeleteIds / reader on the abandoned SqsCheckpointMark instances! If a direct reference to one of those SqsCheckpointMark instances has pendingCheckpoints == null (or if clearAndDecrement is later called), or if removeAll() raced with finalizeCheckpoint(), sqsClient may already be null.
Consider clearing/abandoning each checkpoint mark inside the timeout task (via a synchronized method on SqsCheckpointMark similar to JmsCheckpointMark.abandon()) and guarding delete() / client closure so delete() safely skips or aborts if the reader client is already closed.
| closeTimeoutFuture = | ||
| executorService.schedule( | ||
| () -> { | ||
| List<SqsCheckpointMark> abandoned = pendingCheckpoints.removeAll(); |
There was a problem hiding this comment.
🟠 High: When the close timeout expires and pendingCheckpoints.removeAll() is called, the abandoned SqsCheckpointMark instances themselves are not marked as cleared/abandoned (safeToDeleteIds and reader remain non-null on them). Although pendingCheckpoints is non-null on the in-memory mark (so a subsequent finalizeCheckpoint() on it will see pending.removeUpTo(sequence) return empty), clearAndDecrement() is never called on the abandoned marks, leaving their reader (Optional<SqsUnboundedReader>) and safeToDeleteIds references retained in memory if the runner holds onto the CheckpointMark instance, and risking a negative numInFlightCheckpoints (IllegalStateException: Miscounted in-flight checkpoints) if clearAndDecrement() is ever invoked on them. Calling checkpoint.clearAndDecrement() for each checkpoint in abandoned (instead of manually doing numInFlightCheckpoints.addAndGet(-abandoned.size())) will atomically decrement the counter once per checkpoint and release references.
|
Assigning reviewers: R: @ahmedabu98 for label java. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
Reminder, please take a look at this pr: @ahmedabu98 |
Similar to the recent JmsIO fixes (#40605, #41013),
SqsCheckpointMarkpreviously keptprivate transient Optional<SqsUnboundedReader> reader:SerializableCoder, thetransientreaderfield becomesnull, sofinalizeCheckpoint()throwsNullPointerExceptiononreader.isPresent()and never deletes the messages from SQS or decrementsnumInFlightCheckpoints.getCheckpointMark()incrementsnumInFlightCheckpoints, which is only decremented infinalizeCheckpoint(). Any skipped/unfinalized checkpoint (or checkpoint finalized on a restored copy) preventsnumInFlightCheckpointsfrom reaching 0, leakingsqsClientonclose(), andSqsUnboundedSource.createReader(..., checkpointMark)did not finalize the committed checkpoint on same-JVM resume.Changes
PendingCheckpointsregistry (readerId+ monotonicsequenceonSqsCheckpointMark, withserialVersionUIDpinned for backward compatibility).finalizeCheckpoint()resolves the live reader'sPendingCheckpoints(even from a restored copy), deletessafeToDeleteIdsfor this checkpoint and all earlier unfinalized checkpoints of the same reader, decrementsnumInFlightCheckpointsfor all of them, and triggersmaybeCloseClient(). If the originating reader is no longer in this JVM, it returns cleanly instead of throwing.SqsUnboundedSource.createReader(options, checkpointMark)finalizes the resumed checkpoint when its originating reader lives in this JVM.SqsUnboundedReader.close()schedules a 60s fallback timeout to abandon any unfinalized checkpoints and closesqsClientif finalization never arrives.SqsUnboundedReaderTest.testRestoredCheckpointDeletesMessagesAndCatchesUpEarlierCheckpoints.