Skip to content

Commit

Permalink
PR: error messages, naming, tests
Browse files Browse the repository at this point in the history
Checking return value of `shutdownInternal` for cleanup in service
tests.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed Feb 3, 2022
1 parent 707bd11 commit 60d133f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmd/containerd-shim-runhcs-v1/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ var serveCommand = cli.Command{
WithTID(idFlag),
WithIsSandbox(ctx.Bool("is-sandbox")))
if err != nil {
return fmt.Errorf("starting service: %w", err)
return fmt.Errorf("failed to create new service: %w", err)
}

s, err := ttrpc.NewServer(ttrpc.WithUnaryServerInterceptor(octtrpc.ServerInterceptor()))
Expand Down Expand Up @@ -240,7 +240,7 @@ var serveCommand = cli.Command{
case err = <-serrs:
// the ttrpc server shutdown without processing a shutdown request
case <-svc.Done():
if !svc.GracefulShutdown {
if !svc.gracefulShutdown {
// Return immediately, but still close ttrpc server, pipes, and spans
// Shouldn't need to os.Exit without clean up (ie, deferred `.Close()`s)
return nil
Expand Down
18 changes: 9 additions & 9 deletions cmd/containerd-shim-runhcs-v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ type service struct {
// concurrently.
cl sync.Mutex

// shut is closed to signal a shut request is received
shut chan struct{}
// shutOnce is responsible for closign `shut` and any other necessary cleanup
shutOnce sync.Once
// GracefulShutdown dictates whether to shutdown gracefully and clean up resources
// shutdown is closed to signal a shutdown request is received
shutdown chan struct{}
// shutdownOnce is responsible for closing `shutdown` and any other necessary cleanup
shutdownOnce sync.Once
// gracefulShutdown dictates whether to shutdown gracefully and clean up resources
// or exit immediately
GracefulShutdown bool
gracefulShutdown bool
}

var _ = (task.TaskService)(&service{})
Expand All @@ -90,7 +90,7 @@ func NewService(o ...ServiceOption) (svc *service, err error) {
events: opts.Events,
tid: opts.TID,
isSandbox: opts.IsSandbox,
shut: make(chan struct{}),
shutdown: make(chan struct{}),
}
return svc, nil
}
Expand Down Expand Up @@ -524,12 +524,12 @@ func (s *service) ComputeProcessorInfo(ctx context.Context, req *extendedtask.Co
}

func (s *service) Done() <-chan struct{} {
return s.shut
return s.shutdown
}

func (s *service) IsShutdown() bool {
select {
case <-s.shut:
case <-s.shutdown:
return true
default:
return false
Expand Down
6 changes: 3 additions & 3 deletions cmd/containerd-shim-runhcs-v1/service_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ func (s *service) shutdownInternal(ctx context.Context, req *task.ShutdownReques
return empty, nil
}

s.shutOnce.Do(func() {
s.shutdownOnce.Do(func() {
// TODO: should taskOrPod be deleted/set to nil?
// TODO: is there any extra leftovers of the shimTask/Pod to clean? ie: verify all handles are closed?
s.GracefulShutdown = !req.Now
close(s.shut)
s.gracefulShutdown = !req.Now
close(s.shutdown)
})

return empty, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func setupPodServiceWithFakes(t *testing.T) (*service, *testShimTask, *testShimT

// clean up the service
t.Cleanup(func() {
s.shutdownInternal(context.Background(), &task.ShutdownRequest{
if _, err := s.shutdownInternal(context.Background(), &task.ShutdownRequest{
ID: s.tid,
Now: true,
})
}); err != nil {
t.Fatalf("could not shutdown service: %v", err)
}
})

pod := &testShimPod{id: tid}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func setupTaskServiceWithFakes(t *testing.T) (*service, *testShimTask, *testShim

// clean up the service
t.Cleanup(func() {
s.shutdownInternal(context.Background(), &task.ShutdownRequest{
if _, err := s.shutdownInternal(context.Background(), &task.ShutdownRequest{
ID: s.tid,
Now: true,
})
}); err != nil {
t.Fatalf("could not shutdown service: %v", err)
}
})

task := &testShimTask{
Expand Down

0 comments on commit 60d133f

Please sign in to comment.