From b956e8ef35ff30ff771bfe1d38008438ffd027f0 Mon Sep 17 00:00:00 2001 From: Pierre Carles <51314945+pcarles@users.noreply.github.com> Date: Wed, 12 Feb 2020 16:10:52 +0100 Subject: [PATCH] Fix negative parallelism and negative semaphore (#23902) * Throw an error when parallelism <=0 * Panic in case of negative semaphore --- terraform/context.go | 10 ++++++++++ terraform/util.go | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/terraform/context.go b/terraform/context.go index 5637083ce0..2a438787f0 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -138,7 +138,17 @@ func NewContext(opts *ContextOpts) (*Context, tfdiags.Diagnostics) { // Determine parallelism, default to 10. We do this both to limit // CPU pressure but also to have an extra guard against rate throttling // from providers. + // We throw an error in case of negative parallelism par := opts.Parallelism + if par < 0 { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Invalid parallelism value", + fmt.Sprintf("The parallelism must be a positive value. Not %d.", par), + )) + return nil, diags + } + if par == 0 { par = 10 } diff --git a/terraform/util.go b/terraform/util.go index 5428cd5a0a..7966b58dd2 100644 --- a/terraform/util.go +++ b/terraform/util.go @@ -12,8 +12,8 @@ type Semaphore chan struct{} // NewSemaphore creates a semaphore that allows up // to a given limit of simultaneous acquisitions func NewSemaphore(n int) Semaphore { - if n == 0 { - panic("semaphore with limit 0") + if n <= 0 { + panic("semaphore with limit <=0") } ch := make(chan struct{}, n) return Semaphore(ch)