Skip to content

Commit

Permalink
Fix Prevent negative cpus values
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Jul 27, 2024
1 parent b56802a commit af2e4ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import nextflow.Const
import nextflow.ast.NextflowDSLImpl
import nextflow.exception.AbortOperationException
import nextflow.exception.FailedGuardException
import nextflow.exception.ProcessUnrecoverableException
import nextflow.executor.BashWrapperBuilder
import nextflow.executor.res.AcceleratorResource
import nextflow.executor.res.DiskResource
Expand Down Expand Up @@ -256,7 +257,7 @@ class TaskConfig extends LazyMap implements Cloneable {
new MemoryUnit(value.toString().trim())
}
catch( Exception e ) {
throw new AbortOperationException("Not a valid 'memory' value in process definition: $value")
throw new ProcessUnrecoverableException("Not a valid 'memory' value in process definition: $value")
}
}

Expand Down Expand Up @@ -334,7 +335,11 @@ class TaskConfig extends LazyMap implements Cloneable {

int getCpus() {
final val = getCpus0()
if( val<0 )
throw new ProcessUnrecoverableException("Directive 'cpus' cannot be a negative value - offending value: $val")
final lim = getResourceLimit('cpus') as Integer
if( lim!=null && lim<1 )
throw new ProcessUnrecoverableException("Directive 'resourceLimits.cpus' cannot be a negative value - offending value: $lim")
return val && lim && val > lim ? lim : val
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package nextflow.processor
import java.nio.file.Paths

import nextflow.exception.FailedGuardException
import nextflow.exception.ProcessUnrecoverableException
import nextflow.k8s.model.PodOptions
import nextflow.script.BaseScript
import nextflow.script.ProcessConfig
Expand Down Expand Up @@ -650,4 +651,24 @@ class TaskConfigTest extends Specification {
config.getResourceLabels() == [region: 'eu-west-1', organization: 'A', user: 'this', team: 'that']
config.getResourceLabelsAsString() == 'region=eu-west-1,organization=A,user=this,team=that'
}

def 'should report error on negative cpus' () {
when:
def config = new TaskConfig([cpus:-1])
and:
config.getCpus()
then:
def e = thrown(ProcessUnrecoverableException)
e.message == "Directive 'cpus' cannot be a negative value - offending value: -1"
}

def 'should report error on negative resourceLimits cpus' () {
when:
def config = new TaskConfig([cpus:4, resourceLimits:[cpus:-1]])
and:
config.getCpus()
then:
def e = thrown(ProcessUnrecoverableException)
e.message == "Directive 'resourceLimits.cpus' cannot be a negative value - offending value: -1"
}
}

0 comments on commit af2e4ef

Please sign in to comment.