Repository navigation
fix: reject a negative --concurrency level - #2351
qianeric-backup wants to merge 1 commit into
Conversation
`--concurrency` is documented as `concurrency level`, and every command hands it
straight to the copy graph, which treats a limit of zero or less as "no limit"
(errgroup semantics). A negative value is therefore accepted and removes the
limit instead of lowering it:
$ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
Pushed [oci-layout] /tmp/layout:test
Digest: sha256:...
$ echo $?
0
$ oras pull --oci-layout /tmp/layout:test -o /tmp/out --concurrency -2
Pulled [oci-layout] /tmp/layout:test
Digest: sha256:...
$ echo $?
0
That is the opposite of what someone writing a small number wants, and it is
invisible: the push or pull succeeds, just with unbounded parallelism against
the registry. `oras discover` already rejects the equivalent mistake for its own
level flag:
$ oras discover --oci-layout /tmp/layout:test --depth -1
Error: depth value should be at least 1
## Fix
The root command rejects a negative `--concurrency` before any command runs, so
push, pull, cp, tag, attach, backup and restore are all covered:
$ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
Error: `--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)
$ echo $?
1
Unchanged: the default level, `--concurrency 0` (documented in the message as the
way to disable the limit) and any positive level.
| invocation | before | after |
|---|---|---|
| `--concurrency -3` (push, pull, ...) | runs with no limit, exit 0 | error, exit 1 |
| `--concurrency 0` | no limit | no limit |
| `--concurrency 7` / default | as before | as before |
## Tests
Test_validateConcurrency covers an unused flag, a positive level, zero and a
negative level; it fails on the current tree with `undefined:
validateConcurrency`. `go test ./cmd/oras/root/` and `golangci-lint run
./cmd/oras/root/` are green.
If you would rather keep passing negative values through to the graph, the
alternative is to document `0` as "no limit" in the flag help and leave the
value unchecked — say which you prefer.
TerryHowe
left a comment
There was a problem hiding this comment.
Thanks for the PR! The DCO check is failing, because this commit is missing a Signed-off-by trailer:
- 0ca2838 fix: reject a negative --concurrency level
To fix it, sign off the commit and force-push the branch:
git checkout fix/reject-negative-concurrency
git commit --amend --no-edit --signoff
git push --force-with-lease origin fix/reject-negative-concurrencyFor future commits, use git commit -s.
| return err | ||
| } | ||
| if level < 0 { | ||
| return fmt.Errorf("`--concurrency %d` is invalid: concurrency level must not be negative (use 0 for no limit)", level) |
There was a problem hiding this comment.
| return fmt.Errorf("`--concurrency %d` is invalid: concurrency level must not be negative (use 0 for no limit)", level) | |
| return fmt.Errorf("`--concurrency %d` is invalid: concurrency level must not be negative", level) |
oras-go copy/tag paths reset <= 0 to their default limit, so 0 is not "no limit".
| // validateConcurrency rejects a negative --concurrency level. The copy graph treats a limit of | ||
| // zero as "no limit" (errgroup semantics), and a negative limit is passed through to it just the | ||
| // same, so a negative value silently removes the limit instead of lowering it. |
There was a problem hiding this comment.
| // validateConcurrency rejects a negative --concurrency level. The copy graph treats a limit of | |
| // zero as "no limit" (errgroup semantics), and a negative limit is passed through to it just the | |
| // same, so a negative value silently removes the limit instead of lowering it. | |
| // validateConcurrency rejects a negative --concurrency level. oras-go resets a limit <= 0 to its | |
| // default, while internal/graph.FindPredecessors treats a non-positive limit as unbounded, so a | |
| // negative value is never a meaningful level. |
The comment's errgroup claim only holds for FindPredecessors, not the copy graph.
| if err == nil { | ||
| t.Fatal("expected an error, got nil") | ||
| } | ||
| want := "`--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)" |
There was a problem hiding this comment.
| want := "`--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)" | |
| want := "`--concurrency -3` is invalid: concurrency level must not be negative" |
Keeps the test in sync with the corrected error message.
| cmd := &cobra.Command{ | ||
| Use: "oras [command]", | ||
| SilenceUsage: true, | ||
| PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { |
There was a problem hiding this comment.
A subcommand's own PersistentPreRunE would silently skip this; validate in each command's PreRunE or an option.Concurrency.Parse.
|
Thanks for reporting this. Closing because the PR has been inactive since review (DCO is still failing, and the review asked for validation per command instead of in the root |
--concurrencyis documented asconcurrency level, and every command hands it straight to the copy graph, which treats a limit of zero or less as "no limit" (errgroup semantics). A negative value is therefore accepted and removes the limit instead of lowering it:That is the opposite of what someone writing a small number wants, and it is invisible: the push or pull succeeds, just with unbounded parallelism against the registry.
oras discoveralready rejects the equivalent mistake for its own level flag:Fix
The root command rejects a negative
--concurrencybefore any command runs, sopush,pull,cp,tag,attach,backupandrestoreare all covered:--concurrency -3(push, pull, …)--concurrency 0--concurrency 7/ defaultThe validation only runs when the flag was actually set, so the per-command defaults (3 or 5) are untouched.
Tests
Test_validateConcurrencycovers an unused flag, a positive level, zero, and a negative level. It fails on the current tree withundefined: validateConcurrency.go test ./cmd/oras/root/andgolangci-lint run ./cmd/oras/root/are green.If you would rather keep passing negative values through to the graph, the alternative is documenting
0as "no limit" in the flag help and leaving the value unchecked — happy to switch to whatever you prefer.