diff --git a/internal/resources/ephemeral/ephemeral_resources.go b/internal/resources/ephemeral/ephemeral_resources.go index 3474bc000f..773a3d59b3 100644 --- a/internal/resources/ephemeral/ephemeral_resources.go +++ b/internal/resources/ephemeral/ephemeral_resources.go @@ -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 diff --git a/internal/terraform/eval_context_mock.go b/internal/terraform/eval_context_mock.go index 236d7260cc..47daaceadb 100644 --- a/internal/terraform/eval_context_mock.go +++ b/internal/terraform/eval_context_mock.go @@ -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 { diff --git a/internal/terraform/node_module_expand.go b/internal/terraform/node_module_expand.go index 5a86c7f4f9..e18a135fbc 100644 --- a/internal/terraform/node_module_expand.go +++ b/internal/terraform/node_module_expand.go @@ -4,7 +4,6 @@ package terraform import ( - "context" "log" "github.com/hashicorp/terraform/internal/addrs" @@ -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: diff --git a/internal/terraform/node_resource_ephemeral.go b/internal/terraform/node_resource_ephemeral.go index 01798506aa..ef9a9029bd 100644 --- a/internal/terraform/node_resource_ephemeral.go +++ b/internal/terraform/node_resource_ephemeral.go @@ -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, @@ -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