Skip to content

go: wait for CLI session cmd to finish copying on close - #8940

Merged
sipsma merged 1 commit into
dagger:mainfrom
sipsma:fix-test-race
Nov 13, 2024
Merged

sipsma merged 1 commit into
dagger:mainfrom
sipsma:fix-test-race

Conversation

@sipsma

@sipsma sipsma commented Nov 13, 2024

Copy link
Copy Markdown
Contributor

I saw the race detector go off in a test run where it appeared to be saying that the Go SDK CLI session code was still finishing an io.Copy to the LogOutput for the test after the test was done.

That io.Copy was running in an unsynchronized goroutine, so this seems entirely plausible in the case where it is still reading some buffered data from the stderr pipe after the child process exited.

This updates the SDK client's Close method to wait for that io.Copy to finish before returning, which should ensure there's no stray writes after Close ends.

I couldn't repro this test race locally after many attempts, so this is still a bit of an educated guess. Won't know for sure it's gone until we haven't seen it in CI for a bit.


Saw the race detector go off here.

WARNING: DATA RACE
Read at 0x00c0008e7083 by goroutine 7145:
  testing.(*common).logDepth()
      /usr/lib/go/src/testing/testing.go:1024 +0x504
  testing.(*common).log()
      /usr/lib/go/src/testing/testing.go:1011 +0x7d
  testing.(*common).Log()
      /usr/lib/go/src/testing/testing.go:1052 +0x55
  testing.(*T).Log()
      <autogenerated>:1 +0x4f
  github.com/dagger/dagger/internal/testutil.(*tWriter).Write()
      /app/internal/testutil/twriter.go:47 +0x222
  io.(*multiWriter).Write()
      /usr/lib/go/src/io/multi.go:85 +0xb1
  io.copyBuffer()
      /usr/lib/go/src/io/io.go:431 +0x2d0
  io.Copy()
      /usr/lib/go/src/io/io.go:388 +0x66
  os.genericWriteTo()
      /usr/lib/go/src/os/file.go:275 +0x12
  os.(*File).WriteTo()
      /usr/lib/go/src/os/file.go:253 +0xd4
  io.copyBuffer()
      /usr/lib/go/src/io/io.go:411 +0xd3
  io.Copy()
      /usr/lib/go/src/io/io.go:388 +0x65
  dagger.io/dagger/internal/engineconn.startCLISession.gowrap1()
      /app/sdk/go/internal/engineconn/session.go:158 +0x17

Previous write at 0x00c0008e7083 by goroutine 197:
  testing.tRunner.func1()
      /usr/lib/go/src/testing/testing.go:1677 +0x8fa
  runtime.deferreturn()
      /usr/lib/go/src/runtime/panic.go:605 +0x5d
  testing.(*T).Run.gowrap1()
      /usr/lib/go/src/testing/testing.go:1743 +0x44

Goroutine 7145 (running) created at:
  dagger.io/dagger/internal/engineconn.startCLISession()
      /app/sdk/go/internal/engineconn/session.go:158 +0x106c
  dagger.io/dagger/internal/engineconn.FromLocalCLI()
      /app/sdk/go/internal/engineconn/cli.go:52 +0x7e
  dagger.io/dagger/internal/engineconn.Get()
      /app/sdk/go/internal/engineconn/engineconn.go:56 +0x104
  dagger.io/dagger.Connect()
      /app/sdk/go/client.go:92 +0x148
  github.com/dagger/dagger/core/integration.connect()
      /app/core/integration/suite_test.go:71 +0x2c8
  github.com/dagger/dagger/core/integration.ModuleSuite.TestCustomSDK.func2.1()
      /app/core/integration/module_test.go:2379 +0x6d
  github.com/dagger/dagger/core/integration.ModuleSuite.TestCustomSDK.func2.(*T).Run.2()
      /app/testctx/testctx.go:163 +0xfe
  testing.tRunner()
      /usr/lib/go/src/testing/testing.go:1690 +0x226
  testing.(*T).Run.gowrap1()
      /usr/lib/go/src/testing/testing.go:1743 +0x44

Goroutine 197 (finished) created at:
  testing.(*T).Run()
      /usr/lib/go/src/testing/testing.go:1743 +0x825
  github.com/dagger/dagger/testctx.(*T).Run()
      /app/testctx/testctx.go:158 +0x8d
  github.com/dagger/dagger/core/integration.testOnMultipleVCS()
      /app/core/integration/module_config_test.go:906 +0x153
  github.com/dagger/dagger/core/integration.ModuleSuite.TestCustomSDK()
      /app/core/integration/module_test.go:2377 +0x138
  github.com/dagger/dagger/core/integration.(*ModuleSuite).TestCustomSDK()
      <autogenerated>:1 +0x58
  runtime.call32()
      /usr/lib/go/src/runtime/asm_amd64.s:776 +0x42
  reflect.callMethod()
      <autogenerated>:1 +0x56
  reflect.methodValueCall()
      /usr/lib/go/src/reflect/asm_amd64.s:76 +0x6d
  testing.tRunner()
      /usr/lib/go/src/testing/testing.go:1690 +0x226
  testing.(*T).Run.gowrap1()
      /usr/lib/go/src/testing/testing.go:1743 +0x44

I saw the race detector go off in a test run where it appeared to be
saying that the Go SDK CLI session code was still finishing an io.Copy
to the LogOutput for the test *after* the test was done.

That io.Copy was running in an unsynchronized goroutine, so this seems
entirely plausible in the case where it is still reading some buffered
data from the stderr pipe after the child process exited.

This updates the SDK client's Close method to wait for that io.Copy to
finish before returning, which should ensure there's no stray writes
after Close ends.

I couldn't repro this test race locally after many attempts, so this is
still a bit of an educated guess. Won't know for sure it's gone until we
haven't seen it in CI for a bit.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma requested review from jedevc and vito November 13, 2024 00:08
@sipsma
sipsma requested a review from a team as a code owner November 13, 2024 00:08

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha nice I had just started looking for the same thing myself. Thanks! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants