Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk/db: do not hold the lock on Close #29097

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions sdk/database/dbplugin/v5/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,37 @@ func (g *gRPCServer) Type(ctx context.Context, _ *proto.Empty) (*proto.TypeRespo

func (g *gRPCServer) Close(ctx context.Context, _ *proto.Empty) (*proto.Empty, error) {
g.Lock()
defer g.Unlock()

impl, err := g.getDatabaseInternal(ctx)
if err != nil {
return nil, err
fairclothjm marked this conversation as resolved.
Show resolved Hide resolved
}

err = impl.Close()
if err != nil {
return &proto.Empty{}, status.Errorf(codes.Internal, "unable to close database plugin: %s", err)
}

var id string
if g.singleImpl == nil {
// only cleanup instances map when multiplexing is supported
id, err := pluginutil.GetMultiplexIDFromContext(ctx)
id, err = pluginutil.GetMultiplexIDFromContext(ctx)
if err != nil {
return nil, err
fairclothjm marked this conversation as resolved.
Show resolved Hide resolved
}
delete(g.instances, id)
}

// unlock here so that the subsequent call to Close() does not hold the
// lock in case the DB is slow to respond
g.Unlock()

err = impl.Close()
if err != nil {
// call to Close failed, so we will put the DB instance back in the map
g.Lock()
defer g.Unlock()
if g.singleImpl == nil {
g.instances[id] = impl
fairclothjm marked this conversation as resolved.
Show resolved Hide resolved
}
return &proto.Empty{}, status.Errorf(codes.Internal, "unable to close database plugin: %s", err)
}

return &proto.Empty{}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions sdk/database/dbplugin/v5/grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,19 @@ func TestGRPCServer_Close(t *testing.T) {
}
},
},
"error path for multiplexed plugin": {
db: fakeDatabase{
closeErr: errors.New("close error"),
},
expectErr: true,
expectCode: codes.Internal,
grpcSetupFunc: testGrpcServer,
assertFunc: func(t *testing.T, g gRPCServer) {
if len(g.instances) != 1 {
t.Fatalf("err expected instances map to contain exactly 1 element")
}
},
},
"happy path for non-multiplexed plugin": {
db: fakeDatabase{},
expectErr: false,
Expand Down
Loading