Repository navigation
fix(state): a listing's order comes from the state, not from Go's map seed (#865) - #871
Merged
Merged
Conversation
… seed (#865) MemoryStateManager.List ranged the namespace map and returned without sorting, and StateManager's doc comment stated no ordering contract, so no caller was wrong to assume none. Go randomizes map iteration per process, so every operation that renders those keys in order answered differently from one run to the next — and two identical calls in one run could differ. The sort goes in the one non-test implementation, and the interface documents the guarantee, rather than at the 119 call sites. The same defect had already been fixed five separate times at five individual sites (#764, aws:TagKeys, CreateSnapshots' snapshotSet, DeleteSnapshot's image-ID tie-break, and #862's four tag merge helpers), each with its own rationale and each leaving every other caller exposed. Verified operation by operation, the affected set is S3 ListBuckets, ListMultipartUploads and ListObjectVersions; four ELBv2 describes; five RDS describes; and fifteen EC2 describes. Worst are the ones paging over the keys with a cursor — RDS's Marker, ELBv2's Marker/PageSize — where an unstable order can omit or repeat a resource across pages rather than merely reorder them. Two operations needed more than the shared sort: - ListMultipartUploads violated a documented order. API_ListMultipartUploads publishes ascending object key, then ascending initiation time among uploads sharing a key. It now sorts on the initiation instant rather than its RFC 3339 rendering, with upload ID as a last-resort tie-break — substrate's own, because two uploads of one key can share an instant under a controlled clock. - DescribeInstances buckets instances into a map[string]*reservationItem and ranges it to build reservationSet, which the source sort cannot reach. It now sorts by reservation ID; AWS documents no order there, so that is substrate's reading. RGTA GetResources and EC2 DescribeTags were never affected — both already sort before paginating — and their sorts stay, because neither wants the order of the state keys their records were read from. Both are corrected on #865, whose body named GetResources as the worst case. docs/services.md gains a cross-cutting section recording the guarantee with its three provenance tiers: documented (ListMultipartUploads), cursor-implied (ListObjectVersions, RDS's Marker, ELBv2's Marker/PageSize), and substrate's reading (ListBuckets, DescribeRules, reservationSet). Changing the interface contract made two in-code comments false, and both are fixed here: ec2SortTagDescriptions asserted List documents no ordering, and cfnRegionalStampKinds still described ECS's service and task definition as blocked by their writer, which v0.115.0 fixed. The same self-contradiction in docs/services.md — one passage describing tagging behaviour v0.115.0 removed, another enumerating the reachable set correctly — is corrected too (#867). Closes #865. Closes #867.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MemoryStateManager.Listranged the namespace map and returned without sorting, andStateManager's doc comment stated no ordering contract, so no caller was wrong to assume none. Gorandomizes map iteration per process, so every operation that renders those keys in order answered
differently from one run to the next — and two identical calls in one run could differ. That is a
correctness defect against the replay guarantee, not untidiness: a recorded run cannot be replayed
byte-for-byte if a response body reorders itself between two reads of unchanged state.
The sort goes in the one non-test implementation, and the interface documents the guarantee, rather
than at the 119 call sites. The project had already ruled on this five times, at five individual
sites, each with its own written rationale and each leaving every other caller exposed: CFN stack
tags (#764),
aws:TagKeys,CreateSnapshots'snapshotSet,DeleteSnapshot's image-ID tie-break,and #862's four tag merge helpers.
computeStateHashwas deterministic only by accident, becauseSnapshotmarshals a map andencoding/jsonsorts map keys on the way out.Verified operation by operation, the affected set is S3
ListBuckets,ListMultipartUploadsandListObjectVersions; four ELBv2 describes; five RDS describes; and fifteen EC2 describes. Worst arethe ones paging over the keys with a cursor — RDS's
Marker, ELBv2'sMarker/PageSize— where anunstable order can omit or repeat a resource across pages rather than merely reorder them.
Two operations needed more than the shared sort
ListMultipartUploadswas violating a documented order.API_ListMultipartUploadspublishes a"Sorting of multipart uploads in response" section: ascending object key, then ascending initiation
time among uploads sharing a key. It now sorts on the initiation instant rather than its RFC 3339
rendering, with upload ID as a last-resort tie-break — substrate's own, because two uploads of one
key can share an instant under a controlled clock where they could not under a real one, and
key-marker's description admits an upload "for a key equal to the key-marker" only when its"upload ID [is] lexicographically greater than the specified
upload-id-marker".DescribeInstanceswas only half-fixed by the source sort. It buckets instances into amap[string]*reservationItemand ranges that map to buildreservationSet, so the shared sort madethe instances within a reservation deterministic while leaving the reservations themselves in map
order. It now sorts by reservation ID. AWS documents no order there, so that is substrate's reading.
ListObjectVersionsneeded no code change: its keys are lexicographic fromListand its versionsnewest-first from the prepend-built stored list. It gains a doc comment recording that, and the
NextKeyMarker/CommonPrefixescursor semantics that make the key order load-bearing.Two premises in #865 were wrong, and are corrected on the record
Recorded in a comment on the issue
rather than left for this diff to contradict:
GetResourceswas never affected, though state: MemoryStateManager.List returns keys in Go map order, so 111 call sites answer differently run to run #865 called it the worst case. It sorts itsentire scan by ARN before paginating (
tagging_plugin.go:110-113), and all seventeenscan*helpers feed that one sort. The planned
ResourcesPerPage=1regression test was dropped ratherthan written, because it would have passed before this change. EC2
DescribeTagslikewise —it sorts by resource ID, then type, then key. Both sorts stay and are still load-bearing, because
neither wants the order of the state keys their records were read from.
sort.nearby" is not a publishable number. It came from a proximityheuristic that overstates twice over — the sort can live in the caller (which is exactly how both
cases above were missed), and most
Listcallers are single-key lookups or first-match mutationswhose order never reaches a response body. A verified per-operation list replaces it, in the code
comment and in the docs. Roughly sixty sites in IAM, ElastiCache, API Gateway, CloudWatch, Lambda
and SSM were not individually audited; they inherit the sort either way, and are claimed neither as
fixed nor as unaffected.
Doc-comment staleness the contract change surfaced (#867)
Changing an interface contract made two in-code comments false, and one was already false:
ec2SortTagDescriptionsasserted "StateManager.Listdocuments no ordering either" — stale themoment
types.gochanged. Replaced with why its own sort is still needed:DescribeTagsspansevery resource type at once, and a tag's state key does not sort by resource ID.
cfnRegionalStampKinds' header described ECS's service and task definition as blocked by theirwriter. v0.115.0 fixed that; what they now lack is a table entry, which is a different reason.
docs/services.mdcontradicted itself — one passage described tagging behaviour v0.115.0 removed("a
TagResourceon an ECS task definition answers200and writes nothing"), another enumeratedthe reachable set correctly.
Provenance
API_ListMultipartUploads' key-then-initiation-time sort.ListObjectVersions'KeyMarker, RDS'sMarker,ELBv2's
Marker/PageSize. AWS states no order in prose for any of them.API_ListBucketssays nothing at all about bucket order, nor doesAPI_DescribeRules— andascending reservation ID for
reservationSet. Both rest on the replay guarantee and on the fivein-tree precedents.
Verification
docs/services.mdgains a cross-cutting section, "The order a listing returns its members in",recording the contract, the three provenance tiers with AWS's own language quoted, the verified
affected list, and the two never-affected listings.
The new
emulator/list_order_test.gocovers six cases through the wire rather than reading stateback, since the defect is what a caller observes. Every test either seeds its input in the reverse
of the expected output or asserts two successive calls agree byte-for-byte, so map order cannot be
mistaken for the contract holding. Their negativity was checked directly: with both sorts disabled,
all five order assertions fail on essentially every run.
Full gate green —
gofmt/build/vet,golangci-lint(0 issues),make testandGOTOOLCHAIN=go1.26.6 make test, coverage (83.9% emulator / 83.3% total, at or above the83.9%/83.2% baseline), the four docs checks, the docs site build, and the e2e suite. Sorting
Listchanged observable order across the tree and broke zero tests — the risk this change carried —
so nothing was silently depending on map order.
Compatibility
Every affected listing's member order changes. An assertion that happened to pass on Go's map order
may now fail, and a paginated RDS or ELBv2 walk stops omitting and repeating.
ListMultipartUploadsnow matches its documented sort, and
DescribeInstances'reservationSetarrives in ascendingreservation ID.
Closes #865.
Closes #867.