Skip to content

engine: fix goroutine leaks + deadlock in terminal - #9013

Merged
sipsma merged 2 commits into
dagger:mainfrom
sipsma:fix-terminal-error-leaks
Nov 21, 2024
Merged

sipsma merged 2 commits into
dagger:mainfrom
sipsma:fix-terminal-error-leaks

Conversation

@sipsma

@sipsma sipsma commented Nov 20, 2024

Copy link
Copy Markdown
Contributor

Fixes #8554

While running some tests locally for the (unrelated) filesync PR I accidentally stumbled on a set of tests to run that coincidentally repro'd the flake in #8554 pretty consistently locally:

./hack/with-dev go test -v -run='TestModule/TestDaggerTerminal|TestModule/TestDaggerUp' -count=10 -parallel=8 ./core/integration/

I would hit the flake where the on_failure test hung after 3-4 counts pretty consistently. The set of tests most likely just happens to tweak timing on my machine in just the right way that the race conditions it was caused by would get triggered more often.

There were multiple problems from the test case down through the engine, described in the commit below.

With this fix in place I can't repro the flake locally anymore. Additionally, after running the terminal tests and sending SIGQUIT to the engine I previously saw a ton of leaked goroutines; now after this change they are all gone.

I also included a small one-line performance fix that I noticed while going through the code here.


engine: fix goroutine leaks + deadlock in terminal

Before this, every interactive debug terminal container would leak
multiple goroutines due to various io pipes not getting closed. In one
particular codepath, the read-side of the stdout io.Pipe would be left
open while the other side was in the middle of a write, which caused the
executor code to block forever and never return. This manifested as an
occasional flake in the TestModule/TestDaggerTerminal/on_failure test
case.

Additionally, the part of that test case which meant to assert on
handling of --interactive-command values which didn't exist was not
checking error values. It was hitting an error due to the fact that it
didn't have a TTY setup rather than the error around the command not
existing. That test case is fixed now to setup a TTY and verify the
error message is the expected one.

Signed-off-by: Erik Sipsma [email protected]


engine: slightly optimize terminal reading by reusing buffers

Rather than allocating a new byte slice for every read from
stdout/stderr sent to clients, we can re-use the same byte slice.

This reduces the amount of allocations the engine does. Not a
world-changing optimization but I have seen similar fixes in other
contexts make a noticeable difference in CPU usage since the Go runtime
has a lot less work to do.

Signed-off-by: Erik Sipsma [email protected]

Rather than allocating a new byte slice for every read from
stdout/stderr sent to clients, we can re-use the same byte slice.

This reduces the amount of allocations the engine does. Not a
world-changing optimization but I have seen similar fixes in other
contexts make a noticeable difference in CPU usage since the Go runtime
has a lot less work to do.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma added the needs/changelog Pull requests that require a changelog entry label Nov 20, 2024
@sipsma sipsma added this to the v0.14.1 milestone Nov 20, 2024
Before this, every interactive debug terminal container would leak
multiple goroutines due to various io pipes not getting closed. In one
particular codepath, the read-side of the stdout io.Pipe would be left
open while the other side was in the middle of a write, which caused the
executor code to block forever and never return. This manifested as an
occasional flake in the TestModule/TestDaggerTerminal/on_failure test
case.

Additionally, the part of that test case which meant to assert on
handling of --interactive-command values which didn't exist was not
checking error values. It was hitting an error due to the fact that it
didn't have a TTY setup rather than the error around the command not
existing. That test case is fixed now to setup a TTY and verify the
error message is the expected one.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the fix-terminal-error-leaks branch from 2d49491 to 9d9885a Compare November 21, 2024 00:03

@gerhard gerhard 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.

This must have been a pain to track down & fix. LGTM!

@sipsma
sipsma merged commit e4776b2 into dagger:main Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs/changelog Pull requests that require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TestFlake: TestDaggerTerminal hangs

3 participants