Skip to content

Preserve slice order when removing environment variable #677

@mdryaan

Description

@mdryaan

Description

remove() in pkg/unikontainers/utils.go removes an element from a slice by swapping it with the last element before truncating. This is an O(1) unordered removal. It is used in handleQueueProxy to remove SERVING_READINESS_PROBE from spec.Process.Env and in unikontainers.go to remove VACCEL_RPC_ADDRESS. The swap reorders the remaining elements, which is surprising behavior for any caller that expects stable ordering. Some processes depend on environment variable ordering, and the reordering can cause subtle breakage in queue-proxy integration where the env slice is serialized to spec.json.

Steps to reproduce

Add a test that calls remove and checks the resulting order:

go test ./pkg/unikontainers/ -v -run TestRemovePreservesOrder

The test calls remove([]string{"a", "b", "c", "d"}, 0) and asserts the result is ["b", "c", "d"]. Before the fix, the result is ["d", "b", "c"] and the assertion fails.

Image

Expected behavior: remove() returns a slice with the target element removed and all other elements in their original order.

Current behavior: remove() swaps the target element with the last element before truncating, destroying the original ordering of remaining elements.

Proposed solution: Replace the swap-and-truncate implementation with an ordered removal that copies elements after the removed index one position forward.

Metadata

Metadata

Assignees

Labels

CoreRelated to urunc's internals

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions