Skip to content

Commit bb42edf

Browse files
authored
fix AssertError for newer but lower message idx (#194)
1 parent 588b191 commit bb42edf

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

pysyncobj/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class FAIL_REASON:
55
MISSING_LEADER = 2 #: Leader is currently missing (leader election in progress, or no connection)
66
DISCARDED = 3 #: Command discarded (cause of new leader elected and another command was applied instead)
77
NOT_LEADER = 4 #: Leader has changed, old leader did not have time to commit command.
8-
LEADER_CHANGED = 5 #: Simmilar to NOT_LEADER - leader has changed without command commit.
8+
LEADER_CHANGED = 5 #: Similar to NOT_LEADER - leader has changed without command commit.
99
REQUEST_DENIED = 6 #: Command denied
10+
UNKNOWN_OUTCOME = 7 #: Unknown outcome - Unknown outcome, might happen if a older command is tried to apply
1011

1112
class SERIALIZER_STATE:
1213
NOT_SERIALIZING = 0 #: Serialization not started or already finished.

pysyncobj/syncobj.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,15 @@ def __onMessageReceived(self, node, message):
974974
else:
975975
idx = message['log_idx']
976976
term = message['log_term']
977-
assert idx > self.__raftLastApplied
978-
self.__commandsWaitingCommit[idx].append((term, callback))
977+
if idx <= self.__raftLastApplied:
978+
# Stale apply_command_response:
979+
# The log position was already applied (e.g. via snapshot installed after leader reconnect)
980+
# before this response was processed. Or the leader was restarted without notice (and remains leader).
981+
# The command outcome cannot be tracked; signal the caller so it can retry rather than hanging
982+
# forever.
983+
callback(None, FAIL_REASON.UNKNOWN_OUTCOME)
984+
else:
985+
self.__commandsWaitingCommit[idx].append((term, callback))
979986

980987
if self.__raftState == _RAFT_STATE.CANDIDATE:
981988
if message['type'] == 'response_vote' and message['term'] == self.__raftCurrentTerm:

0 commit comments

Comments
 (0)