Skip to content

Query Policy Evaluation Timeouts - #38938

Open
sebasslash wants to merge 1 commit into
mainfrom
tfsearch/per-call-and-overall-policy-eval-timeouts
Open

Query Policy Evaluation Timeouts#38938
sebasslash wants to merge 1 commit into
mainfrom
tfsearch/per-call-and-overall-policy-eval-timeouts

Conversation

@sebasslash

@sebasslash sebasslash commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

This PR introduces policy evaluation timeouts for Terraform's query policy evaluation.

Policy evaluations could previously block indefinitely if the policy server was slow or unresponsive. This PR adds two configurable timeout mechanisms:

  • Per-call timeout (DefaultPerCallTimeout = 30s): Each EvaluateResource RPC is wrapped with a context.WithTimeout. If the deadline fires while the parent context is still live, the resource is recorded as PolicyErrorResult with a human-readable diagnostic. Crucially, this does not cancel the parent context, so subsequent resources continue to be evaluated.
  • Overall pass deadline (DefaultOverallDeadline = 10m): An overall deadline is installed on the policySubgraph via setDeadline() in nodePolicyEval.DynamicExpand. Each nodeQueryResourcePolicy.Execute checks overallDeadlineExceeded() before acquiring the semaphore. Resources that arrive after the deadline is detected are short-circuited with a PolicyErrorResult and a deadline diagnostic instead of being silently dropped. The deadline context is cancelled in nodePolicyEvalFinish.Execute to release resources.
  • policy.EvalTimeouts struct and policy.DefaultEvalTimeouts() factory are introduced in a new internal/policy/timeout.go file.
  • evaluatePolicies now returns evaluatePoliciesResult (wrapping EvaluationResponse + PerCallTimedOut flag) so callers can rewrite diagnostics with precise timeout messages.
  • policy.MockClient is refactored to release the mutex before calling delegate functions, preventing deadlocks in tests that call back into the mock from within the function.

Fixes #

Target Release

1.17.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

No changes to access controls, encryption, or logging mechanisms. The timeout additions improve resilience against unresponsive policy servers; they do not alter what is evaluated or who can trigger evaluation.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing, as the functionality is hidden behind an experimental flag.

@sebasslash
sebasslash requested a review from a team as a code owner July 28, 2026 20:23
@sebasslash
sebasslash force-pushed the tfsearch/per-call-and-overall-policy-eval-timeouts branch from fc0ce0b to 99a4dce Compare July 28, 2026 20:23
@sebasslash
sebasslash requested a review from dsa0x July 28, 2026 20:24
@sebasslash sebasslash added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Jul 28, 2026
@sebasslash
sebasslash force-pushed the tfsearch/per-call-and-overall-policy-eval-timeouts branch from 99a4dce to 7c652cd Compare July 28, 2026 20:39
@dsa0x
dsa0x changed the base branch from main to tfsearch/policy-query-summary-records August 5, 2026 08:23
@sebasslash
sebasslash force-pushed the tfsearch/per-call-and-overall-policy-eval-timeouts branch from 7c652cd to 15a5c03 Compare August 10, 2026 16:44
@sebasslash
sebasslash force-pushed the tfsearch/policy-query-summary-records branch from 618cf2c to e955885 Compare August 10, 2026 16:44
@sebasslash
sebasslash requested a review from a team as a code owner August 10, 2026 16:44
@sebasslash
sebasslash force-pushed the tfsearch/per-call-and-overall-policy-eval-timeouts branch 2 times, most recently from f3d6084 to 292fd88 Compare August 10, 2026 21:30
@sebasslash
sebasslash force-pushed the tfsearch/policy-query-summary-records branch from e955885 to 8559a7c Compare August 11, 2026 14:04
Base automatically changed from tfsearch/policy-query-summary-records to main August 11, 2026 15:06
…ce RPCs

- Wrap each EvaluateResource RPC in a 30 s per-call context timeout; a
  timed-out call records the resource as error with a diagnostic naming
  the address and duration, then the pass continues.
- Add a 10 min overall deadline for the entire policy pass; resources
  that have not been evaluated when the deadline fires are short-circuited
  with an error diagnostic so downstream aggregators still receive a row
  for every resource.
- deadlineCtx stored as atomic.Pointer to make concurrent reads safe by
  construction; MockClient releases its mutex before invoking callback
  functions to prevent deadlocks under concurrent callers.
- 14 unit tests covering per-call timeout, overall deadline, partial
  results, diagnostic content, parent cancellation, mid-RPC deadline
  race, and -race detector validation.
@sebasslash
sebasslash force-pushed the tfsearch/per-call-and-overall-policy-eval-timeouts branch from 292fd88 to 6903f34 Compare August 12, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog-needed Add this to your PR if the change does not require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant