Files
predis/tests/Predis/Transaction/Strategy
Till Krüss 897b238c7e Fix transactions after lost connections, failed DISCARD and protocol errors (#1758)
* Fail a transaction whose connection was lost until it is discarded

A connection error released the connection holding the transaction, so a
caller that caught the error and kept using the same MultiExec sent the
next commands on a new connection, outside of the transaction. In CAS
mode, MULTI and EXEC then ran on a connection that watched nothing and
overwrote a change made after WATCH. The lost connection now stays held,
and every command fails until discard() or unwatch(). discard() sends
nothing to it, since closing the connection already ended the
transaction on the server.

discard() now always ends the transaction: the strategy closes the
connection when DISCARD fails, so that it cannot stay in MULTI, and
MultiExec resets its state even when the strategy throws. Before, a
failed DISCARD after a queue error reset MultiExec while the strategy
still held the lost connection, and every later transaction failed.
discard() also unwatches the keys when MULTI was never sent, for example
when the connection was lost between WATCH and MULTI.

Cluster transactions built with the fluent interface are aborted after a
cross-slot error too, instead of committing the remaining commands.

The StreamInitException handling is removed: a held connection that is
not open is never reconnected, so it cannot be thrown there. Also drops
the isAborted() check in unwatch(), the inline comments added in #1756,
and merges the changelog into a single Unreleased section.

* Merge overlapping transaction tests

The tests for a queue error with the fluent interface (discarded, not
reusable, unwatch() and watch() while aborted) become one test, and the
one for a failed DISCARD is covered by the recovery test, which now also
covers a connection lost between WATCH and MULTI. The strategy tests for
commands not being retried after WATCH or MULTI fold into the tests for
a lost connection, and the connected tests for a connection lost after
WATCH share one helper.

The persistence data provider is renamed, since it is not only used by
cluster tests.

* Report protocol errors in transactions on the node holding them

With replication, Sentinel or cluster, MultiExec passed the aggregate
connection to ProtocolException, which requires a node connection, so an
unexpected reply inside MULTI or an EXEC reply with the wrong number of
items threw a TypeError. The exception is now built with the node the
transaction's commands are routed to, which is also the connection that
gets closed.

* Remove unreachable branches from the transaction retry callback

A CommunicationException always carries a node connection, so the
callback only ever disconnected it: the branches removing the node from
a replication or handling timeouts never ran.

* Align the comment of MultiExecState::ABORTED with the other flags

* Only realign the comment of MultiExecState::ABORTED

* Shorten the transaction changelog entries

* Cover the remaining branches of the changed transaction methods

Adds tests for discard() in CAS mode before MULTI, a non-callable
argument to execute(), RESP3 replies to EXEC, and server errors from a
node or Relay connection in executeBypassingTransaction().

* Retry transaction commands after a failed connect

The retry callback only accepted a CommunicationException, so retrying
after a StreamInitException failed with a TypeError.

* Reset a transaction once Redis replies to EXEC

execute() kept the transaction state after EXEC, so a transaction that
Redis had already ended still looked open. discard() then sent DISCARD
outside of MULTI, and since a failed DISCARD closes the connection, it
dropped a healthy connection. Reusing the transaction sent the next
command without MULTI, outside of any transaction.

The state is now reset once EXEC replies, whether it succeeded, reported
a failed command, was aborted by a watched key or was rejected. It is
kept when EXEC fails on a lost connection, so that discard() still
releases that connection.

* Close the connection when Redis rejects EXEC but keeps MULTI open

Since MultiExec resets once EXEC replies, discard() no longer sends
DISCARD after a rejected EXEC. Redis 6.0.6 and later discard the
transaction when they reject EXEC and reply with EXECABORT, but older
versions reject it with BUSY, LOADING or MASTERDOWN and keep the
connection in MULTI, so later commands on it were queued.

The strategy now closes the connection when EXEC fails with any error
other than EXECABORT. Relay is excluded, since it reports a transaction
aborted by WATCH as an error.

* Skip UNWATCH on a transaction connection that was lost

unwatch() reconnected to send UNWATCH after the connection holding the
WATCH was lost, although closing that connection already dropped the
watched keys on the server. With the server still down, discard() of a
transaction in CAS mode, or of one that lost its connection between
WATCH and MULTI, then failed although there was nothing left to clean
up. unwatch() now releases a lost connection without sending anything,
as discard() already does.

* Keep a rejected command before MULTI from aborting a transaction

In CAS mode commands run on their own until multi(), so an error there
does not affect the transaction, as in Redis. A fluent cluster
transaction still treated a cross-slot error before multi() like a
rejected queued command, discarding the transaction and failing every
later command until discard(). Only errors after MULTI abort it now.

* Cover the Relay, replication and server-down transaction paths

No test failed when the Relay exception or the choice of connection in
the rejected EXEC handling was removed, or when discard() of a CAS
transaction failed while the server stayed down. New tests check that a
transaction aborted by WATCH under Relay keeps its connection, that a
rejected EXEC with replication closes only the node holding the
transaction, and that discard() succeeds while the server is down after
the connection was lost. The docblock now says that the connection is
closed on any EXEC error but EXECABORT, as the code does.

* Send nothing from unwatch() on an aborted transaction

Aborting the transaction already discarded it and released the
connection holding it, so the UNWATCH that unwatch() sent was a wasted
round trip. Restores the isAborted() check from #1755.
2026-11-18 01:23:57 -07:00
..