Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions host/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ func TestOutOfProcessDependency(t *testing.T) {
host.WithDependencyProviders(greeterpb.ServerPoint),
)
assert.NilError(t, err)
defer func() { assert.NilError(t, h.Shutdown(context.WithoutCancel(ctx))) }()

assert.Equal(t, calls.Load(), int32(1),
"in-process greeter provider was not called by the out-of-process extension")
"in-process greeter provider was not called by the out-of-process extension's Init")

// The extension only logs Shutdown failures, so check that its call arrived.
assert.NilError(t, h.Shutdown(context.WithoutCancel(ctx)))
// Windows can't deliver os.Interrupt, so the launcher kills the
// extension before its Shutdown hook runs.
if runtime.GOOS != "windows" {
assert.Equal(t, calls.Load(), int32(2),
"in-process greeter provider was not called by the out-of-process extension's Shutdown")
}
}
8 changes: 5 additions & 3 deletions sdk/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ func (s *Server) ListenWithIO(ctx context.Context, stdin io.Reader, stdout io.Wr
s.initCtx = ctx

defer func() {
if s.callbackConn != nil {
_ = s.callbackConn.Close()
}
// Shutdown only what initialized, using a context detached from cancellation
// because Serve usually returns after the daemon signals the process.
if s.initialized && s.shutdown != nil {
if err := s.shutdown(context.WithoutCancel(ctx)); err != nil {
log.G(ctx).WithError(err).Warn("extension shutdown failed")
}
}
// Shutdown may still call dependencies, and the host keeps serving them
// until the process exits.
if s.callbackConn != nil {
_ = s.callbackConn.Close()
}
}()

sdkapipb.RegisterServer(s.grpc, runtimeServer{s: s})
Expand Down
3 changes: 2 additions & 1 deletion testdata/greeterdep/cmd/greeterdep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

// Command greeterdep serves the greeterdep fixture as an out-of-process
// extension that depends on the greeter point and calls it at init.
// extension that depends on the greeter point and calls it at init and
// shutdown.
package main

import (
Expand Down
19 changes: 15 additions & 4 deletions testdata/greeterdep/greeterdep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import (
// ID is the extension id and binary name.
const ID = "org.mobyproject.example.greeterdep.v1"

// initialize calls the greeter dependency and verifies the reply.
func initialize(ctx context.Context, _ extensions.Config, r extensions.Resolver) error {
// resolver keeps the Init resolver for Shutdown; the fixture process serves
// only this extension.
var resolver extensions.Resolver

// greet calls the greeter dependency and verifies the reply.
func greet(ctx context.Context, r extensions.Resolver) error {
reply, err := greeterv0.Greet(ctx, r, &greeterv0.HelloRequest{Name: "dep"})
if err != nil {
return fmt.Errorf("greeterdep: call greeter dependency: %w", err)
Expand All @@ -27,9 +31,16 @@ func initialize(ctx context.Context, _ extensions.Config, r extensions.Resolver)
return nil
}

// Extension declares and uses a dependency on the greeter point.
// Extension declares a dependency on the greeter point and calls it from both
// Init and Shutdown.
var Extension = extensions.New(extensions.Declaration{
ID: ID,
Dependencies: []extensions.Dependency{greeterv0.Point.Dependency()},
Init: initialize,
Init: func(ctx context.Context, _ extensions.Config, r extensions.Resolver) error {
resolver = r
return greet(ctx, r)
},
Shutdown: func(ctx context.Context) error {
return greet(ctx, resolver)
},
})