Skip to content

Support signaling core.Start execs. - #2460

Merged
sipsma merged 1 commit into
dagger:mainfrom
sipsma:signal-exec
Jun 2, 2022
Merged

sipsma merged 1 commit into
dagger:mainfrom
sipsma:signal-exec

Conversation

@sipsma

@sipsma sipsma commented May 16, 2022 •

Copy link
Copy Markdown
Contributor

This adds a new core.SendSignal task that enables arbitrary signals to
be sent to an async Start exec. It also adds related support to
core.Stop for specifying a timeout to wait for the exec to exit on its
own before sending SIGKILL.

The main goal here is to support ending Start execs gracefully instead
of being forced to always just SIGKILL them as part of core.Stop. It
should be useful in general for any other use cases that call for
signals to be sent to these execs (i.e. SIGWINCH for future tty
support).

Among other things, this is needed to support running Jaeger via
core.Start (without a graceful stop, its data gets corrupted).

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

@sipsma
sipsma requested review from a team, helderco and shykes as code owners May 16, 2022 20:44
Comment thread pkg/dagger.io/dagger/core/exec.cue Outdated

@jlongtine jlongtine May 17, 2022 •

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.

You could rewrite this as:

_#signals: {
  SIGHUP: 1
  SIGINT: 2
  ...
}

and then use:

#SendSignal: signal: or([for name, signal in _#signals { signal }])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice, that's much cleaner! I also had to include a line that embedded _#signals on the top level of the file so outside callers could still say things like core.#SIGHUP, but that was easy too (thanks cue!)

@sipsma
sipsma force-pushed the signal-exec branch 3 times, most recently from b09e4bb to cf2b2c6 Compare May 17, 2022 20:48
@sipsma sipsma mentioned this pull request May 17, 2022
1 of 9 tasks
@TomChv TomChv added area/core area/engine About dagger core engine labels May 18, 2022

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Left minor comments, that looks really good! I'll give it a try for my final review

Comment thread pkg/dagger.io/dagger/core/exec.cue Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, that's much cleaner! I also had to include a line that embedded _#signals on the top level of the file so outside callers could still say things like core.#SIGHUP, but that was easy too (thanks cue!)

Currently he will do core.SIGHUP, is it still ok for you?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah, I don't think making it a definition does anything since its just a concrete number.

Comment thread plan/task/exec.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is still an asynchronous execution, why did you remove that from log & error??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed it because even though it is asynchronous, that's not the terminology we ended up using, so I thought that using async in the user-facing error message might be slightly confusing.

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.

Is it easy enough for the user to pick that container ID from the error message and find which action started it? Ideally this message would point to the action path of the #Start to avoid doing it manually.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No it's not easy, good point. My next PR that's been pending this one adds logging support, which had to integrate the action path, so I think it will be a good place to address this.

@sipsma
sipsma requested review from TomChv and jlongtine May 24, 2022 03:07

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just tried core.#Start but it didn't run the container asynchronuously.

Maybe I have misunderstood the usage but I expect that the container and the process do not stop until I stop dagger or send a signal SIGINT.

Here's my plan :

package main

import (
	"dagger.io/dagger"
	"dagger.io/dagger/core"
)

dagger.#Plan & {
	actions: {
		image: core.#Pull & {
			source: "postgres:14.3-alpine"
		}

		test: {
			start: core.#Start & {
				input: image.output
				args: ["docker-entrypoint.sh", "postgres"]
				env: {
					POSTGRES_PASSWORD: "password"
					POSTGRES_USER:     "foo"
					POSTGRES_DB:       "test"
					image.config.env
				}
			}
		}
	}
}

Here's a repro case with a docker command

docker run -p 5432:5432 -e POSTGRES_PASSWORD="password" -e POSTGRES_USER="foo" -e POSTGRES_DB="test" postgres:14.3-alpine docker-entrypoint.sh postgres

Do I miss anything? That would be really cool to run a side database, specially for test purpose

@sipsma

sipsma commented May 26, 2022 •

Copy link
Copy Markdown
Contributor Author

@TomChv That's the expected behavior. Being asynchronous means that we don't wait for it to complete, the core.#Start task is considered completed once it has started the process and then the process continues to run in the background until there are no more tasks left to run. In your example, you start the database but then there are no more tasks to run after it, so dagger is done and exits (and kills the container). Your docker run command is different because docker run is by default a synchronous command (like core.#Exec). What you are doing with core.#Start currently is more like if you ran docker run ... & and then exited your shell right away (causing the background process to be killed).

For the use case of running a database to be used by other tasks, what's expected is that you use core.#Start like you do but then actually run tests as separate actions. Dagger will wait for those tests to finish running (while the database runs in the background). Once the tests are done, the background database process will be killed as dagger exits.

There are test cases that verify all this behavior works as expected: https://github.com/dagger/dagger/blob/main/tests/tasks/exec/start_stop_exec.cue

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This improvement is really amazing! I think we should write a piece of documentation about it because it's quite hard to understand how use it.
Feel free to open an issue to keep track of this ;)

PR LGTM on my side, I left questions but it is mergeable IMO

Comment thread plan/task/exec.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This file become bigger and bigger after each improvement. If we continue like this, exec.go will be unmaintainable.

We should maybe work on a refactor of the file tree to make it more sustainable with that kind of improvement.

However, that should be done in a further PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agree, I have more PRs pending this one, can update them to split things up here a bit more

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great!

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.

Also, on the cue side, maybe group the async stuff in a separate async.cue.

Comment thread plan/task/exec.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not just return nil since the value is unused after that? or v?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I tried returning just nil but got a nil pointer exception when the caller of this method tried to unify it with the original value. I guess you could have v unify with itself too but not sure there's much difference between that and returning a new empty value here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Okay, that's strange but it's ok to just return a new value then, thank you!

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.

Not strange, it's what I'd do. You can see it in Nop and ClientFilesystemWrite for example.

You need to return a value and it's wasteful to re-fill the "input" value. Tasks only return new values.

@sipsma

sipsma commented May 27, 2022

Copy link
Copy Markdown
Contributor Author

This improvement is really amazing! I think we should write a piece of documentation about it because it's quite hard to understand how use it. Feel free to open an issue to keep track of this ;)

PR LGTM on my side, I left questions but it is mergeable IMO

Thanks Tom! The remaining issues, including creating all the docs, are tracked here: #2398

Overall, the goal of these tasks in core is to create a set of flexible primitives. Then, we can make higher level interfaces in universe packages that are more opinionated and easy to use.

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

LGTM 🚀

Comment thread pkg/dagger.io/dagger/core/exec.cue Outdated

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.

Typo on commanad.

Comment thread pkg/dagger.io/dagger/core/exec.cue Outdated

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.

nit: add blank line above

Comment thread plan/task/exec.go Outdated

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.

Is it easy enough for the user to pick that container ID from the error message and find which action started it? Ideally this message would point to the action path of the #Start to avoid doing it manually.

Comment thread tests/tasks/exec/start_stop_exec.cue Outdated

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.

I'd just stop.exit & core.SIGKILL.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't define core.SIGKILL (mention why in my other comment), but in retrospect my comment here is confusing because it's not that SIGKILL is literally equal to 137, it's that 137 is the exit code you get when a process is sigkilled (128+9). I'll clarify this 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.

Ah yes, got confused there 🙂

Comment thread pkg/dagger.io/dagger/core/exec.cue Outdated

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.

Maybe filter out SIGKILL if it's not supported?

Suggested change
signal: or([ for name, signal in _signals {signal}])
signal: or([for _, signal in _signals if signal != _signals.SIGKILL { signal }])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I actually just skipped even defining _signals.SIGKILL. The issue with it is that BuildKit sends signals to the process by sending them to runc, which is expected to forward the signal, but SIGKILL can't be caught+forwarded so it just results in runc dying and the underlying process getting orphaned. So it felt safer to just skip defining it at all.

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.

Ah got it. Sorry I didn't notice it wasn't in the list.

This adds a new core.SendSignal task that enables arbitrary signals to
be sent to an async Start exec. It also adds related support to
core.Stop for specifying a timeout to wait for the exec to exit on its
own before sending SIGKILL.

The main goal here is to support ending Start execs gracefully instead
of being forced to always just SIGKILL them as part of core.Stop. It
should be useful in general for any other use cases that call for
signals to be sent to these execs (i.e. SIGWINCH for future tty
support).

Among other things, this is needed to support running Jaeger via
core.Start (without a graceful stop, its data gets corrupted).

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit 7f61640 into dagger:main Jun 2, 2022
@sipsma sipsma mentioned this pull request Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/engine About dagger core engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants