Skip to content

client.Close(): Properly terminate the session #4375

Closed

Description

@aluzzardi

AFAIK client.Close() (all languages) shuts down the connection and that's it.

This means we're not properly shutting down the session and there are some side effects. For instance, there's a few pieces of code here and there doing a time.Sleep to make sure all logs are outputted to the terminal.

Proposal:

  • All SDKs: Properly shutdown the session on Close() (e.g. terminate the process?)
  • Session: Support "clean" shutdown

/cc @sipsma

Activity

  1. sipsma commented on Jan 12, 2023

    @sipsma
    Contributor

    @aluzzardi Just for some more context: we should already be terminating the session child process when Close is called. The child process waits until its stdin pipe gets closed and then closes the localhost listener, which results in engine.Start exiting.

    The stdin pipe technique is nice it handles the case where the parent process dies ungracefully. There is also a listener for sigint and sigterm that does the same thing as closing the stdin pipe though.

    For each SDK:

    1. In Go we close the stdin pipe when the client close is called
    2. In nodejs we use SIGTERM
    3. I'm not totally sure about python since a lot of the code has changed, but I think this gets handled by the exit stack here? cc @helderco to verify

    I'm not sure yet, but one possible problem could be that the Go SDK doesn't actually wait for the process to shutdown fully, it just closes that pipe which then results in the child process to shutdown asynchronously. We could just change it to send SIGTERM and wait on the child proc to shutdown fully (w/ SIGKILL after some timeout). We'd want to leave in the stdin pipe approach to handle the unclean parent exit too, but SIGTERM should be the approach in the default happy path I think.

    It's also possible there's problems elsewhere. E.g. maybe the remote engine hasn't flushed all the logs before the client session exits? Don't remember if buildkit handles that cleanly off the top of my head. But the SIGTERM fix is probably a good place to start w/ Go either way.

  2. helderco commented on Jan 12, 2023

    @helderco
    Contributor

    I'm not totally sure about python since a lot of the code has changed, but I think this gets handled by the exit stack here? cc @helderco to verify

    Correct 👍

    But we're not configuring timeout so it can be forcibly shutdown if it takes longer.

  3. locked and limited conversation to collaborators on Mar 24, 2026
  4. converted this issue into a discussion #12438 on Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions