Skip to content

do not replay a failed MultiDbPipeline batch on close - #4825

Open
insaf021 wants to merge 1 commit into
redis:masterfrom
insaf021:multidb-pipeline-no-replay
Open

insaf021 wants to merge 1 commit into
redis:masterfrom
insaf021:multidb-pipeline-no-replay

Conversation

@insaf021

@insaf021 insaf021 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Scripted RESP server that accepts the pipeline batch on the first connection and closes the socket before replying; MultiDbClient.pipelined() in try-with-resources, INCR + RPUSH, then sync() (handshake lines trimmed):

pipeline failed: redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.
incr.get() -> 1
--- commands received by the server ---
conn#1 PING
conn#1 INCR orders:next-id
conn#1 RPUSH orders:queue order-42
conn#2 PING
conn#2 INCR orders:next-id
conn#2 RPUSH orders:queue order-42

sync() threw, but close() calls sync() again and the buffered commands were still queued, so the batch went out a second time on a fresh connection, from whichever database is active by then, and incr.get() reported the replay's result. Against a real server the first batch was already applied, so INCR ran twice and nothing told the caller. Where nothing re-sent the batch, the responses stayed unset and get() threw IllegalStateException: Please close pipeline or multi block before calling this method.

Root cause: MultiDbPipeline.sync() only polls its queue after every reply has been read, so a failure in sendCommand or getMany leaves it intact. Pipeline and ClusterPipeline already complete their buffered responses with the failure and drop them (#4761); this does the same here once the connection has been obtained, so sync() and Response.get() rethrow the failure and a later sync() or close() sends nothing. A failure while borrowing the connection, before anything is written, still leaves the batch buffered. Response.completeExceptionally is package-private, so AbstractPipeline gets a protected helper for pipelines in other packages.

Two Mockito regression tests in MultiDbPipelineUnitTest fail on master and pass here. Release-notes entry added under behaviour changes, next to the #4761 one.


Note

Medium Risk
Changes error-handling semantics for experimental MultiDbPipeline; apps that relied on implicit replay via try-with-resources may see failures instead of duplicate commands, but avoids silent double-apply on partial batches.

Overview
MultiDbPipeline.sync() now matches Pipeline and ClusterPipeline (#4761): if sending the batch or reading replies fails after a connection is obtained, every buffered Response is completed with that failure, the command queue is cleared, and a later sync() or close() does not resend the batch (avoiding duplicate execution on another DB). Failures while borrowing the connection still leave the batch buffered for retry.

A protected AbstractPipeline.completeExceptionally helper exposes package-private Response.completeExceptionally to MultiDbPipeline. Release notes document the behaviour change and required caller handling. Two unit tests assert no replay on write/read failure.

Reviewed by Cursor Bugbot for commit 21dab52. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T17:05:58.224422Z 21dab52 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@atakavci

atakavci commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

hey @insaf021 ,,

thank you for reporting this. At a glance, looks like we have a bug here.
Will check in detail, thanks.

MultiDbPipeline.sync() polled its buffered responses only after every
reply had been read, so when sendCommand or getMany threw the queue was
left intact and the responses stayed unset. close() calls sync(), so a
pipeline in try-with-resources, or any later sync(), borrowed a fresh
connection from whichever database was active by then and sent the whole
batch again: non-idempotent writes ran twice, and each Response reported
the replay's result as if the command had run once. Where nothing re-sent
the batch, Response.get() threw IllegalStateException.

Complete the buffered responses with the failure and drop the commands
once the connection has been obtained, as Pipeline and ClusterPipeline do
since redis#4761, so sync() and Response.get() rethrow the failure and a later
sync() or close() sends nothing. A failure while borrowing the connection
still leaves the batch buffered. Response.completeExceptionally is
package-private, so AbstractPipeline exposes a protected helper for
pipelines in other packages.
@insaf021
insaf021 force-pushed the multidb-pipeline-no-replay branch from 3bdeecd to 21dab52 Compare November 25, 2026 17:02
@insaf021

Copy link
Copy Markdown
Contributor Author

Rebased onto master while you look. Nothing changed in the fix itself; since 8.1.0 shipped, the release-notes entry moved to docs/release-notes/8.2.0.md and the new AbstractPipeline helper is @since 8.2.

I've also got the scripted-RESP-server reproducer that showed the batch going out twice, if a runnable repro would help the detailed read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants