@@ -15,21 +15,26 @@ import (
1515 "github.com/hashicorp/terraform/version"
1616)
1717
18- func (c * Meta ) PolicyClient (ctx context.Context , policyPaths []string ) (policy.Client , policy.Diagnostics ) {
18+ func (c * Meta ) PolicyClient (ctx context.Context , policyPaths []string ) (policy.Client , policy.Diagnostics , func () ) {
1919 var client policy.Client
20+ closer := func () {
21+ if client != nil {
22+ client .Stop ()
23+ }
24+ }
2025 if ! c .AllowExperimentalFeatures {
2126 log .Printf ("[DEBUG] Policies are not supported without experiments enabled, skipping policy client setup" )
22- return client , nil
27+ return client , nil , closer
2328 }
2429 if len (policyPaths ) == 0 {
2530 log .Printf ("[DEBUG] No policy paths configured, skipping policy client setup" )
26- return client , nil
31+ return client , nil , closer
2732 }
2833
2934 // Use a pre-initialized client for tests if one is available
3035 if c .testingOverrides != nil {
3136 if client := c .testingOverrides .PolicyClient ; client != nil {
32- return client , nil
37+ return client , nil , closer
3338 }
3439 }
3540
@@ -41,7 +46,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
4146 fmt .Sprintf ("Failed to connect to policy engine: %s." , err ),
4247 policy .SetupErrorResult ,
4348 ))
44- return nil , diags
49+ return nil , diags , closer
4550 }
4651
4752 var callbackServiceID uint32
@@ -50,8 +55,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
5055 if srv , ok := client .(policy.CallbackService ); ok {
5156 callbackServer , cbDiags := srv .RegisterCallbackService (ctx )
5257 if cbDiags != nil {
53- client .Stop ()
54- return nil , cbDiags
58+ return nil , cbDiags , closer
5559 }
5660 callbackServiceID = callbackServer .ID
5761 }
@@ -66,7 +70,6 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
6670 for _ , config := range resp .ServerConfigurations () {
6771 version , err := constraints .ParseRubyStyleMulti (config .RequiredVersion )
6872 if err != nil {
69- client .Stop ()
7073 diags = append (diags , policy .NewErrorDiagnostic (
7174 "Failed to validate required Terraform version" ,
7275 fmt .Sprintf ("The policy file %s had a Terraform version constraint that could not be parsed: %s." , config .File , err ),
@@ -80,7 +83,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
8083
8184 if len (diags ) > 0 {
8285 client .Stop ()
83- return nil , diags
86+ return nil , diags , closer
8487 }
8588
8689 terraformVersion , err := versions .ParseVersion (version .Version )
@@ -97,10 +100,9 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
97100 fmt .Sprintf ("The current version of Terraform is %s, and it is not compatible with the versions of Terraform required by the selected policies." , version .String ()),
98101 policy .SetupErrorResult ,
99102 ))
100- client .Stop ()
101- return nil , diags
103+ return nil , diags , closer
102104 }
103105
104106 log .Printf ("[INFO] backend/operation/policy: Policy engine initialized" )
105- return client , diags
107+ return client , diags , closer
106108}
0 commit comments