Skip to content

feat: improve service stop semantics - #6354

Merged
jedevc merged 13 commits into
dagger:mainfrom
jedevc:service-sigterm
Jan 30, 2024
Merged

jedevc merged 13 commits into
dagger:mainfrom
jedevc:service-sigterm

Conversation

@jedevc

@jedevc jedevc commented Jan 2, 2024 •

Copy link
Copy Markdown
Contributor

This PR changes how services are stopped:

  1. Calls to Service.Stop now block until the service exits
  2. Services are now stopped using SIGTERM instead of SIGKILL
  • This can be changed by setting the new kill option to true, which sends SIGKILL instead
  1. Services that are automatically cleaned up (e.g. client disconnection, all bindings are detached) are now gracefully stopped using SIGTERM+SIGKILL after a default 10 seconds has passed.
  • This isn't really ideal really. Some programs may exit quickly after SIGTERM, and some might not.
  • Maybe we could provide a service option that allows setting the default termination time, though I think this should be a follow-up.

More information and details in each commit (split into hopefully standalone units to make review a bit easier if that helps)

@jedevc jedevc added the area/engine About dagger core engine label Jan 2, 2024
Comment thread core/schema/service.graphqls Outdated
Comment on lines 47 to 50

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.

Bikeshedding welcome 🎉 This is the "easiest" possible interface - users can implement their own graceful stop on top of this if they like.

However, it could also be nice to have a default graceful stop similarly to this PR implements internally (but doesn't expose). One problem with this though is it means we'd really want to pass time.Durations through the API (I think this is the first case?) - ideally each SDK would choose how to work with datetime, instead of passing around magic strings/integers. This feels like a big can of worms that I don't really want to open right now.

Comment thread core/integration/services_test.go Outdated

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.

We don't really have any tests at the moment for services automatically closing.

// TODO(vito): test that the service stops when the client closes... somehow

@vito I had an idea where we could potentially spin up a dagger instance and watch the logs to see if the service exits? Not 100% sure how to approach this, but I think we should probably try and do it now, so as to not accidentally break that behavior in this patch.

@jedevc
jedevc requested review from sipsma and vito January 2, 2024 17:23
Comment thread core/service.go Resolved
Comment thread core/services.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.

Debatable, but I probably would have gone with force killing the services in this codepath (which should only matter when the client has disconnected but left some services running). If the client didn't care enough to actually stop their services then they probably just want to shutdown asap rather than wait for anything.

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.

Ahhh this is definitely debatable, but I think it makes sense to avoid blocking close as much as possible.

We could gracefully shutdown in the background... but now any stopping messages/logs get lost, without the client being able to see them, so I don't love that.

I think SIGKILL is the right call here - users should stop their own services if they really care about this case.


So in that case, we'd only apply the graceful stop when detaching - when all the service bindings are let go. But - does that even make sense? A common case will be that the client immediately disconnects after a run of a container that had service bindings, so we'd end up skipping that graceful exit, and apply the force-kill here.

Maybe the only way to get SIGTERM is to manually call Stop - if users don't do this, we always SIGKILL. stopGraceful isn't then necessary, which is somewhat nice.

Comment thread core/services.go Resolved
jedevc added 5 commits January 3, 2024 16:02
Previously, this was signal passing was only applied to the high-level
runc wrapper invoked directly by buildkit, but not to the shim inside
the runc container.

This prevented SIGTERM and similar signals from being received by the
process.

Signed-off-by: Justin Chadwell <[email protected]>
This refactor changes the Stop signature on services to allow gentle
termination and additionally ensures that for container services that
this maps onto distinguishing between SIGKILL/SIGTERM.

Additionally, Stop is now a blocking call.

Signed-off-by: Justin Chadwell <[email protected]>
This plugs through the work in the previous commits so that clients can
use the kill boolean option to send SIGKILL instead of SIGTERM.

Signed-off-by: Justin Chadwell <[email protected]>
Because Service.Stop has become a blocking function, we need to refactor
to ensure that we don't unneccessarily hold the lock.

This was preventing us from calling .Stop multiple times - e.g. if we
were to call with force=false, and then again with force=true, the
force=true would not be able to acquire the lock, and so the process
might not terminate.

Signed-off-by: Justin Chadwell <[email protected]>
jedevc added 4 commits January 3, 2024 16:38
With the ability to distinguish SIGTERM/SIGKILL, we can try and attempt
to more gracefully clean up services. By default, we will try and
SIGTERM a service, and then after 10 seconds, we'll come in and
SIGKILL it if its still running.

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
@jedevc

jedevc commented Jan 3, 2024 •

Copy link
Copy Markdown
Contributor Author

Ok, to summarize, here's how the rules should work in my head, and where my confusion currently is:

  • If a service is manually started, then manually stopped, it uses SIGTERM unless SIGKILL is specified
  • If a service is manually started, and is still running when the client exits, it is SIGKILLed
  • If a service is automatically started (because it was bound to) then it will be automatically SIGKILLed 10s after all callers have detached from it
    • What happens in this scenario though, if the user manually calls Stop on the underlying service? We could potentially override the SIGKILL behavior (but this could introduce potential raciness into pipelines, e.g. automatic SIGKILL triggers before Stop gets called). Or maybe we should require that every Stop call must correspond to a Start call - a service can only be manually stopped if it was manually started - I kinda like this idea.
    • Potentially we could even enforce this at the type level by having a RunningService, and putting Stop on that.
  • If a service is automatically started (because it was started for a host tunnel), I have no idea what should happen.
    • The last example in feat: improve service stop semantics #6354 (comment) is weird.
    • It feels strange that killing the tunnel wouldn't affect the underlying service if it was automatically started - especially since they're the same type, users could overwrite their service type with the tunnel type, and lose the original service object (I've done this in playing around). We'd have a dangling service that can't be stopped.
    • But if killing the tunnel does affect the underlying service, then there's an open question of what should happen if the underlying service got released from it's other bindings before/after it was closed.
      • If I call close on the tunnel before the other bindings got released do we block until they were released? If so, then it's weird that calling Stop doesn't actually trigger a signal and has to wait.
      • If it's called immediately, it potentially invalidates the other bindings, which is bad (why is this tunnel more important than the other tunnels/bindings?)
      • If it just returns immediately and would never call stop (I think this is what happens in this PR currently if there were multiple things attached) then Stop is inconsistent which is weird.
      • There's more options that are all equally weird from what I understand.

Maybe it's fine to say that tunnels are special cases - closing them "frees" an automatically started service just as we'd do so for service bindings, but doesn't actually stop it immediately. (There's a weird edge case to be aware of - if you stop a tunnel, then wait around for a while, and then go to stop the underlying service, it feels very strange if the service already got cleaned up after 10s automatically by the tunnel closing. This is resolved if we required that every stop corresponded to a start - in that case, for the stop of the underlying service to be valid, we'd have to have started it manually, in which case the automatic cleanup can't be triggered).


Aside - but when tunnels are special cases, it feels weird that we'd have a kill option on them that doesn't really apply. Out of curiosity... why are tunnels the same type as services? If we imagine that host tunnels had a different type (services from the host exposed to dagger could still have the same type, just tunnels from dagger to the client should have a different type).

For one, it doesn't make a huge amount of sense to be able to do this:

tunnel, err := c.Host().Tunnel(srv).Start(ctx)
tunnel2, err := c.Host().Tunnel(tunnel).Start(ctx)

You can do this, but it does feel sort of nonsensical - just like being able to take the tunnel and bind it in a ServiceBinding.

@jedevc jedevc mentioned this pull request Jan 16, 2024
@marcosnils

Copy link
Copy Markdown
Contributor

@jedevc relevant to this context: #6430

@vito

vito commented Jan 22, 2024 •

Copy link
Copy Markdown
Contributor

@jedevc Finally got around to this, sorry again for the wait.

It makes sense to me that tunnels relate to their tunneled service in the same way that service bindings work. It seems like a clean mental model: it binds to the service and serves its own proxy. So if you stop the tunnel, that shouldn't immediately stop the tunneled service; the tunneled service should stop after the same grace period as if a client went away.

I have some hesitation with defaulting to SIGKILL for auto-started services, but I'm warming to it. In cases where you have to care about it you can just manually Start it, which might be better than allowing it to potentially thrash anyway (imagining slow CI leading to 10s gaps here and there for a common dependency like dockerd). On the other hand, it seems like slightly less mental overhead if we just have the grace period auto-stop match the behavior of Stop (SIGTERM -> SIGKILL), so it's literally an auto-Stop.

re: calling Stop without a corresponding Start, I can imagine a contrived scenario where you've constructed a Service and passed it to a bunch of things that conditionally bind to it or conditionally run, so you're not sure if you need to Start it, but you still want to be able to Stop it if it started. This is really a fringe case though, especially considering client.Close() will automatically stop services for you, so this would only help if you wanted to clean up the service for some reason but keep using the client for other things.

Aside - but when tunnels are special cases, it feels weird that we'd have a kill option on them that doesn't really apply. Out of curiosity... why are tunnels the same type as services? If we imagine that host tunnels had a different type (services from the host exposed to dagger could still have the same type, just tunnels from dagger to the client should have a different type).

We can certainly reconsider this, but at the time there was enough overlap that it didn't seem worth introducing a whole new type, and I was able to stick the landing on some mental gymnastics to justify it: #5557 (comment)


Lemme know if I missed any lingering questions 😵‍💫

@jedevc
jedevc marked this pull request as ready for review January 29, 2024 14:05
@jedevc
jedevc requested review from a team as code owners January 29, 2024 14:05
@jedevc

jedevc commented Jan 29, 2024 •

Copy link
Copy Markdown
Contributor Author

On the other hand, it seems like slightly less mental overhead if we just have the grace period auto-stop match the behavior of Stop (SIGTERM -> SIGKILL), so it's literally an auto-Stop.

Ok, I've done this, this feels like the right call to me.

Essentially now:

  • Services started manually will now run until the are explicitly stopped - if the client closes and the services are still running, they are killed.
  • Services started automatically (either via tunnels or container dependencies) are automatically stopped using SIGTERM -> SIGKILL behavior. But can also be stopped manually.
    • Importantly, tunnels do not forward the force option.

IMO, there's still some follow-ups I'd like to do at some point, but I really want this feature, so I'd rather get it into a state where it's usable 🎉

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

Went over the subtle changes to locking/etc., makes sense to me consdering Stop is a blocking call now 👍

Only noticed an optional tiny DagQL cleanup, so approving

Comment thread core/schema/service.go Outdated
return core.ServiceID{}, err
}
type serviceStopArgs struct {
Kill dagql.Optional[dagql.Boolean]

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 can also do *bool here:

Suggested change
Kill dagql.Optional[dagql.Boolean]
Kill *bool

Setting a default value should also do what you want:

Suggested change
Kill dagql.Optional[dagql.Boolean]
Kill bool `default:"false"`

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.

Done, thanks!

Spent some time trying to make sure that it was applied as an options struct, instead of a required arg.

Comment thread core/services.go
Comment on lines +383 to +384
cause := errors.New("service did not terminate")
ctx2, _ := context.WithTimeoutCause(ctx, timeout, cause)

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.

:neat:

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