Skip to content

Commit

Permalink
make ephemeral calls cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Sep 25, 2024
1 parent d18638b commit cd2631d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
7 changes: 5 additions & 2 deletions internal/resources/ephemeral/ephemeral_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ func (r *Resources) Close(ctx context.Context) tfdiags.Diagnostics {
r.mu.Lock()
defer r.mu.Unlock()

// We might be closing due to a context cancellation, but we still need
// to be able to make non-canceled Close requests.
// We might be closing due to a context cancellation, but we still need to
// be able to make non-canceled Close requests.
//
// TODO: if we're going to ignore the cancellation to ensure that Close is
// always called, should we add some sort of timeout?
ctx = context.WithoutCancel(ctx)

var diags tfdiags.Diagnostics
Expand Down
5 changes: 4 additions & 1 deletion internal/terraform/eval_context_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ var _ EvalContext = (*MockEvalContext)(nil)

func (c *MockEvalContext) StopCtx() context.Context {
c.StopCtxCalled = true
return c.StopCtxValue
if c.StopCtxValue != nil {
return c.StopCtxValue
}
return context.TODO()
}

func (c *MockEvalContext) Hook(fn func(Hook) (HookAction, error)) error {
Expand Down
3 changes: 1 addition & 2 deletions internal/terraform/node_module_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package terraform

import (
"context"
"log"

"github.com/hashicorp/terraform/internal/addrs"
Expand Down Expand Up @@ -215,7 +214,7 @@ func (n *nodeCloseModule) Execute(ctx EvalContext, op walkOperation) (diags tfdi
diags = diags.Append(ctx.ClosePlugins())

// We also close up the ephemeral resource manager
diags = diags.Append(ctx.EphemeralResources().Close(context.TODO()))
diags = diags.Append(ctx.EphemeralResources().Close(ctx.StopCtx()))

switch op {
case walkApply, walkDestroy:
Expand Down
10 changes: 3 additions & 7 deletions internal/terraform/node_resource_ephemeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,8 @@ func ephemeralResourceOpen(ctx EvalContext, inp ephemeralResourceInput) tfdiags.
provider: provider,
internal: resp.InternalContext,
}
// TODO: What can we use as a signal to cancel the context we're passing in
// here, so that the object will stop renewing things when we start shutting
// down?
// TODO: The context Stopped channel should probably be updated
// finally to a Context
ephemerals.RegisterInstance(context.TODO(), inp.addr, ephemeral.ResourceInstanceRegistration{

ephemerals.RegisterInstance(ctx.StopCtx(), inp.addr, ephemeral.ResourceInstanceRegistration{
Value: resultVal,
ConfigBody: config.Config,
Impl: impl,
Expand Down Expand Up @@ -172,7 +168,7 @@ func (n *nodeEphemeralResourceClose) ModulePath() addrs.Module {
func (n *nodeEphemeralResourceClose) Execute(ctx EvalContext, op walkOperation) tfdiags.Diagnostics {
log.Printf("[TRACE] nodeEphemeralResourceClose: closing all instances of %s", n.addr)
resources := ctx.EphemeralResources()
return resources.CloseInstances(context.TODO(), n.addr)
return resources.CloseInstances(ctx.StopCtx(), n.addr)
}

// ephemeralResourceInstImpl implements ephemeral.ResourceInstance as an
Expand Down

0 comments on commit cd2631d

Please sign in to comment.