Skip to content

DescribeInstances only reads Filter.1 and only honors instance-state-name — multi-filter and tag: filters silently dropped #305

Closed

Description

@scttfrdmn

Summary

EC2Plugin.describeInstances only parses a single filter (Filter.1.Name / Filter.1.Value.*) and only applies it if its key is instance-state-name. Any second-or-later filter (Filter.2+) is silently ignored, and tag:* filters are never applied at all. This makes DescribeInstances return instances the caller explicitly filtered out.

Where

ec2_plugin.go, describeInstances (~line 495):

filterNames := extractIndexedParams(req.Params, "Filter.1.Value")
filterKey := req.Params["Filter.1.Name"]
...
// Filter by state name.
if filterKey == "instance-state-name" && len(filterNames) > 0 && !containsStr(filterNames, inst.State.Name) {
    continue
}

Only Filter.1 is read, and the only key handled is instance-state-name.

Repro (real client behavior)

A client (e.g. spawn list --state running) issues DescribeInstances with two filters, tag first:

  1. Filter.1 = tag:spawn:managed = true
  2. Filter.2 = instance-state-name = running

Substrate reads only Filter.1 (tag:spawn:managed), which it doesn't know how to apply, so it applies nothing — and Filter.2 (the state filter) is never read. Result: a terminated instance (state code 48) is still returned for a running-only query.

Sequence:

  1. RunInstances → instance i-… running.
  2. TerminateInstances i-… → state correctly set to {Code:48, Name:"terminated"} and persisted (terminate is fine).
  3. DescribeInstances with Filter.1=tag:spawn:managed=true, Filter.2=instance-state-name=running → terminated instance still returned.

If the state filter happens to be Filter.1, it works; any other position fails. So this is order-dependent, which is its own correctness smell.

Root cause

describeInstances predates the general-purpose extractEC2Filters helper (line ~2297) and never adopted it. That helper already parses all Filter.N.Name / Filter.N.Value.M pairs correctly.

Proposed fix

Use extractEC2Filters(req.Params) and apply every returned filter, AND-combining them. At minimum support the filter keys real callers use:

  • instance-state-name (match inst.State.Name)
  • instance-id (match inst.InstanceID)
  • tag:<key> (match a tag whose Key == <key> and Value ∈ allowed values)
  • tag-key (instance has a tag with that key, any value)
  • instance-type, availability-zone / placement.availability-zone, vpc-id, subnet-id, key-name

Unknown filter keys: AWS errors with InvalidParameterValue; emulating "match nothing" (return empty) is acceptable and safer than silently ignoring. Whatever is chosen, it must not silently pass.

Impact

  • Breaks instance-state-name filtering whenever it isn't the first filter (common — clients usually lead with a tag: filter).
  • tag: filtering doesn't work at all, so callers relying on server-side tag filtering get over-broad result sets.
  • Surfaced while building spawn's Tier 0 CLI-against-substrate integration suite: TestTier0_TerminateRemovesFromList saw a terminated instance still listed as running.

Acceptance

  • All Filter.N parsed (not just Filter.1); filters AND-combined.
  • tag:<key>, instance-id, instance-state-name filtering all work regardless of filter order.
  • Unit test: launch + terminate, then DescribeInstances with [tag:spawn:managed=true, instance-state-name=running] returns the instance before terminate and excludes it after.
  • Existing EC2 suite stays green.

Activity

  1. added a commit that references this issue on Jun 3, 2026
  2. added
    type: bug
    and removed on Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions