Skip to content

Commit

Permalink
fix: ensure proper closure of client.Client.conn with finalizer
Browse files Browse the repository at this point in the history
Always close the connection, even if we forgot to do this ourselves.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Jan 30, 2025
1 parent 19040ff commit 292ae56
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/machinery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"errors"
"fmt"
"io"
"runtime"
"time"

cosiv1alpha1 "github.com/cosi-project/runtime/api/v1alpha1"
Expand Down Expand Up @@ -173,11 +174,16 @@ func New(_ context.Context, opts ...OptionFunc) (c *Client, err error) {
c.Inspect = &InspectClient{c.InspectClient}
c.COSI = state.WrapCore(client.NewAdapter(cosiv1alpha1.NewStateClient(c.conn)))

// Just like Go team does in their os.File internals, lets ensure that we always close the connection.
runtime.SetFinalizer(c.conn, (*grpcConnectionWrapper).Close)

return c, nil
}

// Close shuts down client protocol.
func (c *Client) Close() error {
runtime.SetFinalizer(c.conn, nil) // remove so it doesn't get closed twice

return c.conn.Close()
}

Expand Down

0 comments on commit 292ae56

Please sign in to comment.