Skip to content

Test flake: TestLegacy package dagger/test/internal/dagger is not in std #7900

Closed

Description

@sipsma

Started hitting a lot of flakes in the new TestLegacy suite, errors look like:

input: module.withSource.initialize resolve: failed to initialize module: failed to call module "test" to get functions: call constructor: process "go build -o /runtime ." did not complete successfully: exit code: 1
                            
Stderr:
main.go:3:8: package dagger/test/internal/dagger is not in std (/usr/local/go/src/dagger/test/internal/dagger)

Could be unrelated, but it smells awfully similar to #7662 in that both could be explain by dirs missing files or otherwise having unexpected contents.


Thankfully can repro locally, so made some progress in debugging:

On one occurrence of the error in TestLegacyNoExec, the contents are showing that the go.mod of the module starts with:

module dagger/bare

go 1.22.4

This is extremely wrong.... That go.mod is the one that should be used for TestLegacyExportAbsolutePath.... This test should have module dagger/test (which explains the error)

The other files in there like main.go and dagger.gen.go all look like the right ones for TestLegacyNoExec. At least in this case it's just go.mod which is wrong.

Haven't root caused it yet, but could run from Go SDK problems all the way down to buildkit solver problems.

  • All I can say is that the test case itself looks totally fine so far (and I can't imagine how this would ever be a test case problem anyways)

FTR, so far I've been debugging by running with this diff:

diff --git a/core/schema/sdk.go b/core/schema/sdk.go
index 8bbaed3e4..c031b15e1 100644
--- a/core/schema/sdk.go
+++ b/core/schema/sdk.go
@@ -364,9 +364,13 @@ func (sdk *goSDK) Runtime(
                                {
                                        Name: "args",
                                        Value: dagql.ArrayInput[dagql.String]{
+                                               "sh", "-c",
+                                               `go build -o /runtime . || (echo WTF && sleep 100000 && false)`,
+                                               /* TODO:
                                                "go", "build",
                                                "-o", goSDKRuntimePath,
                                                ".",
+                                               */
                                        },
                                },
                        },

Then running dagger -m dev call --source=.:default test custom --timeout=100h --run=TestLegacy --pkg=./core/integration --parallel=8 --count=100.

Wait for it to happen, find the sleep process pid with ps and do sudo nsenter -m -u -i -n -p -t <pid> sh to get inside the container.

Activity

  1. added
    kind/bug
    area/engine
    on Jul 12, 2024
  2. sipsma commented on Jul 12, 2024

    @sipsma
    ContributorAuthor

    Still working on root cause, just a few more significant things in meantime:

    I looked at the overlay layers for the /src mount where the problematic go.mod file is, I can see that at the bottom layer it's the same situation: incorrect go.mod from the other test but correct main.go.

    By itself, this is significant because it the bottom layer is the one created by the local import. So either:

    1. the original dir output by dagger init before this call was wrong to begin with
    2. the local import of the dir during dagger call got messed up and crossed wires with another one from the other test (which in turn could be due to a filesync bug or occurring during the copy-op we do for each local import)

    However even more interestingly, at least a few times I have seen the error getting hit on dagger call statements that have run after other dagger calls already ran successfully in the test. e.g. we've seen the error on this line specifically: https://github.com/sipsma/dagger/blob/05b530dbbbcc06519101ca2d471a5e05997f97f3/core/integration/legacy_test.go#L295-L295

    Since a previous dagger call chained off the same previous dagger init succeeded, either:

    1. The earlier dagger init is re-running incorrectly and also somehow outputting incorrect files (but only the second time)
      • possible, but requires multiple bugs chained together
    2. The local import (which is gonna happen on each dagger call) is indeed the place where this is getting messed up
      • just by gut, this feels more likely at this point

    Also, some breadcrumbs on the place where that problematic value in go.mod gets set:

    1. directly set here: https://github.com/sipsma/dagger/blob/05b530dbbbcc06519101ca2d471a5e05997f97f3/cmd/codegen/generator/go/generator.go#L195-L195
    2. from arg to codegen bin here https://github.com/sipsma/dagger/blob/05b530dbbbcc06519101ca2d471a5e05997f97f3/cmd/codegen/main.go#L69-L69
    3. which in turn is set from the module source config here https://github.com/sipsma/dagger/blob/05b530dbbbcc06519101ca2d471a5e05997f97f3/core/schema/sdk.go#L431-L431

    The significant thing here is that the ModuleName value used in go.mod is also used all over the place in codegen, so it's extremely difficult to imagine how this could be a codegen bug and probably lies somewhere else.


    Separately from all the above, at some point without changing anything I just suddenly stopped being able to repro 😢... Previously took a few attempts to hit, now I haven't in like an hour.

    • The only thing I can imagine that is mutating without having changed any code is the cache? But no clue what to make of this really.
  3. self-assigned this
    on Jul 12, 2024
  4. added this to the v0.12.1 milestone on Jul 12, 2024
  5. sipsma commented on Jul 12, 2024

    @sipsma
    ContributorAuthor

    Picking back up today, thankfully was able to eventually repro again (though it took a long time).

    Added this to the milestone because whatever is happening here seems deeply wrong, beyond just an annoying flake, and is worth getting to the bottom of ASAP.

  6. sipsma commented on Jul 12, 2024

    @sipsma
    ContributorAuthor

    Noticed that the behavior seems to generally be that once this get hit once, it's easy to hit again for a while. But it will go through long periods (literally hundreds of executions for me) of not getting hit.

    • I suspect this is related to caching; once it gets into a bad state it's reused repeatedly until pruned or invalidated

    I'm currently chasing down one hypothesis based on reading buildkit filesync code that:

    1. The local dir sync cache ref may be re-used between different nested clients in completely separate tests due to the fact that the nested client in the tests all use the same module source path (/work) and have the same hostname (just the default one when no explicit hostname was set). This is due to the fact that those are our inputs to the "shared key hint" that buildkit filesync uses.
    2. In theory that should be fine provided that
      • The "differ" that buildkit filesync uses always notices the difference in go.mod between the different clients
      • There's no race conditions in the time between the filesync is performed and the copy-op we do on top to get an immutable snapshot of it where the local sync cache ref is partially rewritten
    3. But if any of those points aren't aligning with reality, that could explain this

    Working on testing this now. If this is on the right track, the good news is that it's most likely an incredibly obscure condition for real-world users since it requires so many specific conditions and timing to align (different clients are using the same paths and have the same hostname, running the exact same ops at the same time, etc.).

    • That being said, it probably is time to bite the bullet on generating a "unique machine id" for each dagger client that's stored in the home dir. buildx does something similar. I was hoping we could avoid it since the less state the better, but feels more worth it at this point if this is indeed the cause.
  7. sipsma commented on Jul 12, 2024

    @sipsma
    ContributorAuthor

    Just confirmed that when the error happens, the TestExecWithEntrypoint client that hit the error does indeed end up re-using a local sync cache ref from a client that originally ran in TestLegacyExportAbsolutePath.

    • Needed to add more fields to various logs to track this down, which I'll send a PR for no matter what at the end of this since it was clearly very useful here.

    Like I said in previous comment, this isn't wrong in and of itself, so now need to track down if this is a differ problem or some race condition in the use of cache dir.

    • Differ seems more likely at this point since the cache ref in question is only used twice by these two clients and spaced relatively long apart
  8. sipsma commented on Jul 13, 2024

    @sipsma
    ContributorAuthor

    Somewhat hilariously, this bug appears to also be reliant on the fact that the two test cases have modules with names who are the same length (Bare and Test) 🫠

    This matters because the differ used by buildkit filesync only compares metadata. It appears that the two go.mods in these test cases are ending up with exactly equal file metadata (even though their contents aren't the same). That's why the length of the module name matters; if they weren't both 4 characters the go.mods would have different file sizes and thus be considered different.

    The remaining mystery now is that ModTime is also considered by the differ and based on the logs I added to it, all of the go.mod files have exactly equal mod times down to the nanosecond. There's no way they are actually coinciding down to the nanosecond due to parallelism, so something else is going on still. Continuing to dig.

  9. sipsma commented on Jul 15, 2024

    @sipsma
    ContributorAuthor

    I'm working through adding logging to tons of places to track the mod time of go.mod as it flows back and forth between clients (export after dagger init, import on dagger call, etc.) to see what's happening here.

    I did realize it might be worth double checking my assumption that there's no way the mod time of different files is corresponding down to the nanosecond. That felt like the probabilistic equivalent of hitting the lottery by my intuition, but I'm mildly less sure about that now.

    This program will panic every few executions for me:

    package main
    
    import (
    	"fmt"
    	"time"
    
    	"golang.org/x/sync/errgroup"
    )
    
    func main() {
    	start := make(chan struct{})
    	var eg errgroup.Group
    	const size = 100
    	outs := make([]int64, size)
    	for i := range size {
    		eg.Go(func() error {
    			<-start
    			outs[i] = time.Now().UnixNano()
    			return nil
    		})
    	}
    
    	close(start)
    	if err := eg.Wait(); err != nil {
    		panic(err)
    	}
    
    	set := make(map[int64]struct{})
    	for _, out := range outs {
    		if _, ok := set[out]; ok {
    			panic(fmt.Sprintf("duplicate timestamp: %d", out))
    		}
    		set[out] = struct{}{}
    	}
    }

    That, combined with the fact that it can take a very long time for me to hit the test flake sometimes (until the cache gets "poisoned" in my current hypothesis) makes the possibility of actually just getting different go.mod files with coincidentally equal nanosecond mod times feel somewhat less outrageous (but still crazy).

    Still going to eliminate every other possibility first.

  10. sipsma commented on Jul 16, 2024

    @sipsma
    ContributorAuthor

    Okay, I think I've convinced myself that the mod times of these files are actually coincidentally ending up as the exact same nanosecond values. I wanted to be as sure as possible about this since I don't want to accidentally miss some other bug incorrectly setting the mtime of these files to be the same during filesync, etc.

    There's a few crucial things that increase the likelihood of this happening:

    1. The separate modules are independently running (not being deduped unexpectedly or anything like that), but they do share cache mounts for the go mod pkg cache and go build cache.
      • The go.mod is not stored in these cache mounts, but we run go mod tidy to fix it up, which involves downloading dependencies and updating other shared state. I can see from strace that tidy does all sorts of flocking on top of the cache mounts to synchronize this between concurrent processes.
      • So the end result is that the independent go mod tidy calls actually do end up synchronizing closely with each other and thus get to the point of "update go.mod file" at very close to the same time
    2. Based on some kernel spelunking, the nanosecond precision on mtime is not actually that precise, even when the filesystem supports that granularity
      • IIUC the value is set around here, with the time coming from this current_time function
      • That current_time function is calling ktime_get_coarse_real_ts64, which I found docs for that suggest it's a cached value (so plausible for different calls to get the same value) and possibly from up to 10ms in the past.
      • So different calls spaced up to at least 10ms apart could plausibly end up getting the same nanosecond mtime values

    This is all inherently very heisenberg-y so it's not feasible to get 100% confirmation, but between some wrapping with strace and more dumb annotating various logs with the value of Go's time.Now().UnixNano() that when the bug happens, go mod tidy is being called within 100s of microseconds of each other. Based on what I found above that's close enough to result in the mtimes of the files to get the same nanosecond values.


    So the end result is that:

    1. different go.mods from different test cases end up with precisely equal metadata (same size because module names are the same length, same mtime due to the above, etc.)
    2. different nested clients end up re-using the same mutable filesync cache ref because they all have the same hostname, are operating on the exact same paths in their containers (/work), etc.
    3. the buildkit differ only considers metadata when choosing whether to retransfer a file, and since go.mod matches that metadata, it doesn't get re-transferred and the wrong one is used

    In terms of the fix, I will probably just go with the approach where we generate a stable+unique identifier for each host that's stored in the user's XDG cache dir or something like that. We can then use that to ensure different clients don't accidentally re-use the filesync cache ref in situations like this.

    • To be clear, it's deeply difficult to imagine this actually impacting one of our real-world users. It seems extremely specific to what we are doing in our tests. But this approach would solve the problem for our tests and should alleviate any remote chance of it impacting users.
    • The one possible downside is this impact the performance of our tests since the deduplication being done was plausibly a minor performance benefit (just hit this one problematic corner case). It's also possible this isn't noticeable and even if it is, there might be some clever things we can do to give each of our nested clients a stable host identifier (based on the line number + file name where the call is being made).
  11. modified the milestones: v0.12.1, v0.12.2 on Jul 18, 2024
  12. modified the milestones: v0.12.2, v0.12.3 on Jul 22, 2024
  13. sipsma commented on Jul 23, 2024

    @sipsma
    ContributorAuthor

    fixed by #7943

  14. added
    kind/flake
    on Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/ciarea/enginekind/bugkind/flake

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions