Skip to content

Commit

Permalink
fix: kill language plugins after cli and tests (#3313)
Browse files Browse the repository at this point in the history
Fixes #3287
Handles `ftl build`, `ftl deploy`, `ftl new` and `just test-backend`
  • Loading branch information
matt2e authored Nov 4, 2024
1 parent cbd0f05 commit c8404ab
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions frontend/cli/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (b *buildCmd) Run(ctx context.Context, client ftlv1connect.ControllerServic
return err
}

// Cancel build engine context to ensure all language plugins are killed.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
engine, err := buildengine.New(ctx, client, projConfig, b.Dirs, bindAllocator, buildengine.BuildEnv(b.BuildEnv), buildengine.Parallelism(b.Parallelism))
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions frontend/cli/cmd_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (d *deployCmd) Run(ctx context.Context, projConfig projectconfig.Config) er
return err
}

// Cancel build engine context to ensure all language plugins are killed.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
engine, err := buildengine.New(ctx, client, projConfig, d.Build.Dirs, bindAllocator, buildengine.BuildEnv(d.Build.BuildEnv), buildengine.Parallelism(d.Build.Parallelism))
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions frontend/cli/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (i newCmd) Run(ctx context.Context, ktctx *kong.Context, config projectconf
return err
}
}
_ = plugin.Kill() //nolint:errcheck
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions frontend/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func main() {
// Dynamically update the kong app with language specific flags for the "ftl new" command.
languagePlugin, err := prepareNewCmd(log.ContextWithNewDefaultLogger(ctx), app, os.Args[1:])
app.FatalIfErrorf(err)
if plugin, ok := languagePlugin.Get(); ok {
// Kill the plugin when the app exits due to an error, or after showing help.
addToExit(app, func(code int) {
_ = plugin.Kill() //nolint:errcheck
})
}

kctx, err := app.Parse(os.Args[1:])
app.FatalIfErrorf(err)
Expand Down Expand Up @@ -184,6 +190,18 @@ func createKongApplication(cli any, csm *currentStatusManager) *kong.Kong {
return app
}

func addToExit(k *kong.Kong, cleanup func(code int)) {
originalExit := k.Exit
k.Exit = func(code int) {
cleanup(code)
if originalExit == nil {
// Should not happen, but no harm being cautious
os.Exit(code)
}
originalExit(code)
}
}

func makeBindContext(logger *log.Logger, cancel context.CancelFunc) terminal.KongContextBinder {
var bindContext terminal.KongContextBinder
bindContext = func(ctx context.Context, kctx *kong.Context) context.Context {
Expand Down
3 changes: 2 additions & 1 deletion internal/buildengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

func TestGraph(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
ctx, cancel := context.WithCancel(log.ContextWithNewDefaultLogger(context.Background()))
t.Cleanup(cancel)

bindURL, err := url.Parse("http://127.0.0.1:8893")
assert.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions jvm-runtime/plugin/common/java_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestJavaConfigDefaults(t *testing.T) {
assert.NoError(t, err)
plugin, err := languageplugin.New(ctx, allocator, "java", "test")
assert.NoError(t, err)
t.Cleanup(func() {
_ = plugin.Kill() //nolint:errcheck
})

defaults, err := plugin.ModuleConfigDefaults(ctx, dir)
assert.NoError(t, err)
Expand Down

0 comments on commit c8404ab

Please sign in to comment.