make ephemeral calls cancellable

pull/35778/head
James Bardin 2 years ago
parent d18638bab3
commit cd2631d657

@ -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

@ -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 {

@ -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:

@ -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

Loading…
Cancel
Save