Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(kinesis): preserve shard iterators across GetRecords retries (#333) (#334)

Nahuel Nucera88748ac960bf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 54a75ec8..bc31ff73 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,13 @@ Versioning follows [Semantic Versioning](https://semver.org/).
---
+## [1.2.15] — 2026-04-15
+
+### Fixed
+- **Kinesis `GetRecords` iterator handling** — shard iterators are no longer consumed (popped) on use, matching real AWS behavior where iterators remain valid until their 5-minute TTL expires. Previously, calling `GetRecords` immediately invalidated the iterator, causing `ExpiredIteratorException` on client retries. Polling consumers like Apache Camel that retry on transient failures would fail with "Iterator has expired or is invalid". Reported by @markwimpory
+
+---
+
## [1.2.14] — 2026-04-15
### Added
diff --git a/ministack/services/kinesis.py b/ministack/services/kinesis.py
index bc572d4f..3759b9e5 100644
--- a/ministack/services/kinesis.py
+++ b/ministack/services/kinesis.py
@@ -520,10 +520,11 @@ def _get_records(data):
iterator = data.get("ShardIterator")
limit = min(data.get("Limit", 10000), 10000)
- state = _shard_iterators.pop(iterator, None)
+ state = _shard_iterators.get(iterator)
if not state:
return error_response_json("ExpiredIteratorException", "Iterator has expired or is invalid", 400)
if time.time() - state["created_at"] > ITERATOR_EXPIRY_SECONDS:
+ del _shard_iterators[iterator]
return error_response_json("ExpiredIteratorException", "Iterator has expired", 400)
stream = _streams.get(state["stream"])
@@ -551,6 +552,10 @@ def _get_records(data):
if shard["records"] and new_pos < len(shard["records"]):
millis_behind = max(0, int((time.time() - shard["records"][new_pos]["ApproximateArrivalTimestamp"]) * 1000))
+ # Retire the current iterator and issue a new one with advanced position,
+ # matching AWS behavior: each GetRecords call returns a NextShardIterator.
+ # The old iterator remains valid until it expires naturally (5 min TTL),
+ # allowing client retries to succeed.
next_token = new_uuid()
_shard_iterators[next_token] = {
"stream": state["stream"],
diff --git a/pyproject.toml b/pyproject.toml
index b91b8eb9..aa827541 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ministack"
-version = "1.2.14"
+version = "1.2.15"
description = "Free, open-source local AWS emulator — drop-in LocalStack replacement"
readme = "README.md"
license = { file = "LICENSE" }
diff --git a/tests/test_kinesis.py b/tests/test_kinesis.py
index 9ac892fa..0cd09211 100644
--- a/tests/test_kinesis.py
+++ b/tests/test_kinesis.py
@@ -323,6 +323,32 @@ def test_kinesis_esm_creates_and_lists(lam, kin):
lam.delete_function(FunctionName="esm-kin-fn")
+def test_kinesis_iterator_reuse_on_retry(kin):
+ """Same shard iterator can be used multiple times (client retry), matching AWS behavior."""
+ kin.create_stream(StreamName="kin-iter-retry", ShardCount=1)
+ kin.put_record(StreamName="kin-iter-retry", Data=b"rec1", PartitionKey="pk1")
+ kin.put_record(StreamName="kin-iter-retry", Data=b"rec2", PartitionKey="pk2")
+
+ desc = kin.describe_stream(StreamName="kin-iter-retry")
+ shard_id = desc["StreamDescription"]["Shards"][0]["ShardId"]
+ it = kin.get_shard_iterator(
+ StreamName="kin-iter-retry", ShardId=shard_id, ShardIteratorType="TRIM_HORIZON"
+ )["ShardIterator"]
+
+ # First call with iterator
+ resp1 = kin.get_records(ShardIterator=it)
+ assert len(resp1["Records"]) == 2
+
+ # Retry with the same iterator — should succeed and return identical data
+ resp2 = kin.get_records(ShardIterator=it)
+ assert len(resp2["Records"]) == 2
+ assert resp2["Records"][0]["Data"] == resp1["Records"][0]["Data"]
+
+ # NextShardIterator from first call should advance past existing records
+ resp3 = kin.get_records(ShardIterator=resp1["NextShardIterator"])
+ assert len(resp3["Records"]) == 0
+
+
def test_kinesis_cbor_put_record(kin):
"""Java SDK sends CBOR-encoded PutRecord; ministack must decode it."""
import cbor2