Repository navigation
feat: improve service stop semantics - #6354
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
We don't really have any tests at the moment for services automatically closing.
dagger/core/integration/services_test.go
Line 1172 in 746339f
@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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Signed-off-by: Justin Chadwell <[email protected]>
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]>
29ed52d to
7016e59
Compare
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]>
Signed-off-by: Justin Chadwell <[email protected]>
7016e59 to
4f15991
Compare
|
Ok, to summarize, here's how the rules should work in my head, and where my confusion currently is:
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 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 |
|
@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 re: calling
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 😵💫 |
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Ok, I've done this, this feels like the right call to me. Essentially now:
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 🎉 |
| return core.ServiceID{}, err | ||
| } | ||
| type serviceStopArgs struct { | ||
| Kill dagql.Optional[dagql.Boolean] |
There was a problem hiding this comment.
You can also do *bool here:
| Kill dagql.Optional[dagql.Boolean] | |
| Kill *bool |
Setting a default value should also do what you want:
| Kill dagql.Optional[dagql.Boolean] | |
| Kill bool `default:"false"` |
There was a problem hiding this comment.
Done, thanks!
Spent some time trying to make sure that it was applied as an options struct, instead of a required arg.
| cause := errors.New("service did not terminate") | ||
| ctx2, _ := context.WithTimeoutCause(ctx, timeout, cause) |
Signed-off-by: Justin Chadwell <[email protected]>
0d102d1 to
6ed66c2
Compare
This PR changes how services are stopped:
Service.Stopnow block until the service exitsSIGTERMinstead ofSIGKILLkilloption totrue, which sendsSIGKILLinsteadSIGTERM+SIGKILLafter a default 10 seconds has passed.SIGTERM, and some might not.More information and details in each commit (split into hopefully standalone units to make review a bit easier if that helps)