Skip to content

Commit

Permalink
Fix Execution may hang reading trace file (#5683) [ci fast]
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
  • Loading branch information
pditommaso authored Jan 19, 2025
1 parent e14fc99 commit 17d7a08
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ abstract class TaskHandler {
}
}

def file = task.workDir?.resolve(TaskRun.CMD_TRACE)
final file = task.workDir?.resolve(TaskRun.CMD_TRACE)
try {
if(file) record.parseTraceFile(file)
}
Expand Down
82 changes: 41 additions & 41 deletions modules/nextflow/src/main/groovy/nextflow/trace/TraceRecord.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package nextflow.trace

import java.nio.file.Files
import java.nio.file.Path
import java.util.regex.Pattern

Expand Down Expand Up @@ -416,53 +417,52 @@ class TraceRecord implements Serializable {
* </pre>
*
*/

TraceRecord parseTraceFile( Path file ) {

final text = file.text
try(BufferedReader reader = Files.newBufferedReader(file)) {
final lines = reader.readLines()
if( !lines )
return this
if( lines[0] != 'nextflow.trace/v2' )
return parseLegacy(file, lines)

for( int i=0; i<lines.size(); i++ ) {
final pair = lines[i].tokenize('=')
final name = pair[0]
final value = pair[1]
if( value == null )
continue

switch (name) {
case '%cpu':
case '%mem':
// fields '%cpu' and '%mem' are expressed as percent value
this.put(name, parseInt(value, file, name) / 10F)
break

case 'rss':
case 'vmem':
case 'peak_rss':
case 'peak_vmem':
// these fields are provided in KB, so they are normalized to bytes
def val = parseLong(value, file, name) * 1024
this.put(name, val)
break

case 'cpu_model':
this.put(name, value)
break

default:
def val = parseLong(value, file, name)
this.put(name, val)
break
}

final lines = text.readLines()
if( !lines )
return this
if( lines[0] != 'nextflow.trace/v2' )
return parseLegacy(file, lines)

for( int i=0; i<lines.size(); i++ ) {
final pair = lines[i].tokenize('=')
final name = pair[0]
final value = pair[1]
if( value == null )
continue

switch (name) {
case '%cpu':
case '%mem':
// fields '%cpu' and '%mem' are expressed as percent value
this.put(name, parseInt(value, file, name) / 10F)
break

case 'rss':
case 'vmem':
case 'peak_rss':
case 'peak_vmem':
// these fields are provided in KB, so they are normalized to bytes
def val = parseLong(value, file, name) * 1024
this.put(name, val)
break

case 'cpu_model':
this.put(name, value)
break

default:
def val = parseLong(value, file, name)
this.put(name, val)
break
}

return this
}

return this
}

private TraceRecord parseLegacy( Path file, List<String> lines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package nextflow.trace

import java.nio.file.NoSuchFileException
import java.nio.file.Path

import groovy.json.JsonSlurper
import spock.lang.Specification
import spock.lang.Unroll
Expand Down Expand Up @@ -333,4 +336,12 @@ class TraceRecordTest extends Specification {
'COMPLETED' | true
}

def 'should throw file not found exception' () {
given:
def rec = new TraceRecord([:])
when:
rec.parseTraceFile(Path.of('unknown'))
then:
thrown(NoSuchFileException)
}
}

0 comments on commit 17d7a08

Please sign in to comment.