-
-
Notifications
You must be signed in to change notification settings - Fork 854
fix: don't mutate shared matrix AST when resolving ref: rows
#2894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amitmishra11
wants to merge
2
commits into
go-task:main
Choose a base branch
from
amitmishra11:fix/matrix-ref-race-2890
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package task | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/go-task/task/v3/internal/templater" | ||
| "github.com/go-task/task/v3/taskfile/ast" | ||
| ) | ||
|
|
||
| // TestResolveMatrixRefsDoesNotMutateSharedMatrix is a regression test for | ||
| // #2890. The *ast.Matrix passed to resolveMatrixRefs is part of the shared, | ||
| // cached Task AST, and concurrent invocations of the same task (e.g. via | ||
| // parallel deps) all call resolveMatrixRefs with the very same *ast.Matrix. | ||
| // If resolveMatrixRefs mutates that matrix in place, concurrent callers race | ||
| // on the mutation and can observe a value resolved for a different caller | ||
| // (cross-contamination), or trip the race detector. | ||
| // | ||
| // Run with `go test -race` to catch the race deterministically; it is not | ||
| // guaranteed to corrupt output on every unsynchronized run. | ||
| func TestResolveMatrixRefsDoesNotMutateSharedMatrix(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| sharedMatrix := ast.NewMatrix( | ||
| &ast.MatrixElement{Key: "ARCH", Value: &ast.MatrixRow{Ref: ".ARCH_VAR"}}, | ||
| ) | ||
|
|
||
| const n = 200 | ||
| var wg sync.WaitGroup | ||
| results := make([]string, n) | ||
| errs := make([]error, n) | ||
|
|
||
| for i := 0; i < n; i++ { | ||
| wg.Add(1) | ||
| go func(i int) { | ||
| defer wg.Done() | ||
|
|
||
| want := "amd64" | ||
| if i%2 != 0 { | ||
| want = "arm64" | ||
| } | ||
|
|
||
| vars := ast.NewVars() | ||
| vars.Set("ARCH_VAR", ast.Var{Value: []any{want}}) | ||
| cache := &templater.Cache{Vars: vars} | ||
|
|
||
| resolved, err := resolveMatrixRefs(sharedMatrix, cache) | ||
| if err != nil { | ||
| errs[i] = err | ||
| return | ||
| } | ||
| row, ok := resolved.Get("ARCH") | ||
| if !ok { | ||
| errs[i] = fmt.Errorf("call %d: ARCH row missing from resolved matrix", i) | ||
| return | ||
| } | ||
| if len(row.Value) != 1 { | ||
| errs[i] = fmt.Errorf("call %d: ARCH.Value = %v, want a single-element slice", i, row.Value) | ||
| return | ||
| } | ||
| got, _ := row.Value[0].(string) | ||
| results[i] = got | ||
| }(i) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| for i, err := range errs { | ||
| if err != nil { | ||
| t.Fatalf("call %d: unexpected error: %v", i, err) | ||
| } | ||
| want := "amd64" | ||
| if i%2 != 0 { | ||
| want = "arm64" | ||
| } | ||
| if results[i] != want { | ||
| t.Errorf("call %d: got ARCH=%q, want %q (cross-contamination between concurrent callers)", i, results[i], want) | ||
| } | ||
| } | ||
|
|
||
| // The original, shared matrix must remain unresolved: resolveMatrixRefs | ||
| // must operate on a copy, not the input. | ||
| origRow, ok := sharedMatrix.Get("ARCH") | ||
| if !ok { | ||
| t.Fatal("ARCH row missing from original matrix") | ||
| } | ||
| if origRow.Value != nil { | ||
| t.Errorf("shared input matrix was mutated: ARCH.Value = %v, want nil (Ref rows must be resolved into a copy)", origRow.Value) | ||
| } | ||
| if origRow.Ref != ".ARCH_VAR" { | ||
| t.Errorf("shared input matrix Ref was altered: got %q, want %q", origRow.Ref, ".ARCH_VAR") | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.