@@ -15,21 +15,24 @@ 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+ client .Stop ()
22+ }
2023 if ! c .AllowExperimentalFeatures {
2124 log .Printf ("[DEBUG] Policies are not supported without experiments enabled, skipping policy client setup" )
22- return client , nil
25+ return client , nil , closer
2326 }
2427 if len (policyPaths ) == 0 {
2528 log .Printf ("[DEBUG] No policy paths configured, skipping policy client setup" )
26- return client , nil
29+ return client , nil , closer
2730 }
2831
2932 // Use a pre-initialized client for tests if one is available
3033 if c .testingOverrides != nil {
3134 if client := c .testingOverrides .PolicyClient ; client != nil {
32- return client , nil
35+ return client , nil , closer
3336 }
3437 }
3538
@@ -41,7 +44,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
4144 fmt .Sprintf ("Failed to connect to policy engine: %s." , err ),
4245 policy .SetupErrorResult ,
4346 ))
44- return nil , diags
47+ return nil , diags , closer
4548 }
4649
4750 var callbackServiceID uint32
@@ -50,8 +53,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
5053 if srv , ok := client .(policy.CallbackService ); ok {
5154 callbackServer , cbDiags := srv .RegisterCallbackService (ctx )
5255 if cbDiags != nil {
53- client .Stop ()
54- return nil , cbDiags
56+ return nil , cbDiags , closer
5557 }
5658 callbackServiceID = callbackServer .ID
5759 }
@@ -66,7 +68,6 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
6668 for _ , config := range resp .ServerConfigurations () {
6769 version , err := constraints .ParseRubyStyleMulti (config .RequiredVersion )
6870 if err != nil {
69- client .Stop ()
7071 diags = append (diags , policy .NewErrorDiagnostic (
7172 "Failed to validate required Terraform version" ,
7273 fmt .Sprintf ("The policy file %s had a Terraform version constraint that could not be parsed: %s." , config .File , err ),
@@ -80,7 +81,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
8081
8182 if len (diags ) > 0 {
8283 client .Stop ()
83- return nil , diags
84+ return nil , diags , closer
8485 }
8586
8687 terraformVersion , err := versions .ParseVersion (version .Version )
@@ -97,10 +98,9 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
9798 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 ()),
9899 policy .SetupErrorResult ,
99100 ))
100- client .Stop ()
101- return nil , diags
101+ return nil , diags , closer
102102 }
103103
104104 log .Printf ("[INFO] backend/operation/policy: Policy engine initialized" )
105- return client , diags
105+ return client , diags , closer
106106}
0 commit comments