Repository navigation
Description
Description
--concurrency accepts any integer. No command checks the value, so a negative level is accepted silently and the command exits 0:
$ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
Pushed [oci-layout] /tmp/layout:test
$ echo $?
0
What a non-positive level does depends on the code path:
- The oras-go copy/tag paths (
push,pull,cp,tag,attach,backup,restore,manifest push) reset a limit<= 0to oras-go's default. The user's value is ignored without a warning. internal/graph.FindPredecessorstreats a non-positive limit as unbounded (errgroup semantics).
Neither result is what someone who typed a negative number meant. 0 is also not a reliable "no limit".
For comparison, oras discover --depth -1 already fails with depth value should be at least 1.
Expected behavior
A --concurrency value below 1 fails with a clear error before the command runs.
Suggested fix
Add a shared option.Concurrency that registers the flag and validates >= 1 in its Parse, and use it in every command that has --concurrency. Don't validate in the root command's PersistentPreRunE, because a subcommand that defines its own PersistentPreRunE would skip that check without anyone noticing.
This also covers the flag-consistency item in #2280: defaults are currently 3 (cp, pull, backup) or 5 (push, attach, restore, tag, manifest push).
Related
- Tracking: CLI design review follow-ups #2280 (tracking: make flags consistent)
- fix: reject a negative --concurrency level #2351 (an earlier attempt that validated in the root command)
Evaluation
Decision needed: what about 0?
test/e2e/suite/command/cp.go runs oras cp -r --concurrency 0 in two tests (one titled "without concurrency limitation", added in #1122). On the cp -r referrer path, 0 currently means "no limit" in internal/graph. Rejecting values below 1, as proposed above, breaks that tested behavior and any scripts that pass 0. Options:
- Reject
< 1everywhere (recommended). Consistent withdiscover --depth, and0already behaves differently across commands. Update the two cp e2e tests and note the change in the release notes. - Reject only
< 0, and document a single meaning for0.
Feasibility
Every affected command already calls option.Parse(cmd, &opts) in its PreRunE, and option.Parse (cmd/oras/internal/option/parser.go) calls Parse on every embedded field that implements FlagParser. Embedding a new option.Concurrency in each command's options gets validation with no extra wiring. Defaults differ per command (3 or 5), so the option needs a per-command default. Unifying the defaults (#2280) changes behavior and is better done in a separate PR.
Effort: small to medium. The check is trivial, but it touches 8 commands plus tests.
Suggested approach
- Add
cmd/oras/internal/option/concurrency.gowith:ApplyFlags, which registers--concurrencywith the command's default.Parse, which fails withconcurrency value should be at least 1(same wording asdiscover).
- Replace the
concurrency intfield and theIntVarPregistration inattach,backup,cp,pull,push,restore,tag, andmanifest push. Keep today's defaults. - As defense in depth, change
internal/graph/graph.goFindPredecessorsfromif opts.Concurrency != 0to> 0, so a negative limit can never mean unbounded. - Tests:
- A table-driven unit test for
Concurrency.Parse(-1, 0, 1, default). - e2e error cases for the commands.
- Change the two cp e2e tests to use a positive value.
- A table-driven unit test for
Related code
- Flag registrations:
cmd/oras/root/{push,pull,cp,attach,tag,backup,restore}.go,cmd/oras/root/manifest/push.go cmd/oras/internal/option/terminal.go: model for theApplyFlags/Parsepatterncmd/oras/root/discover.go: the existing--depthcheckinternal/graph/graph.go:FindPredecessorslimit handlingtest/e2e/suite/command/cp.go: the--concurrency 0tests
The design is fully specified, so this is a good candidate for an outside contributor once the 0 question is settled.
Activity