Skip to content

Commit 17d7a08

Browse files
authored
Fix Execution may hang reading trace file (#5683) [ci fast]
Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
1 parent e14fc99 commit 17d7a08

File tree

3 files changed

+53
-42
lines changed

3 files changed

+53
-42
lines changed

modules/nextflow/src/main/groovy/nextflow/processor/TaskHandler.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ abstract class TaskHandler {
227227
}
228228
}
229229

230-
def file = task.workDir?.resolve(TaskRun.CMD_TRACE)
230+
final file = task.workDir?.resolve(TaskRun.CMD_TRACE)
231231
try {
232232
if(file) record.parseTraceFile(file)
233233
}

modules/nextflow/src/main/groovy/nextflow/trace/TraceRecord.groovy

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package nextflow.trace
1818

19+
import java.nio.file.Files
1920
import java.nio.file.Path
2021
import java.util.regex.Pattern
2122

@@ -416,53 +417,52 @@ class TraceRecord implements Serializable {
416417
* </pre>
417418
*
418419
*/
419-
420420
TraceRecord parseTraceFile( Path file ) {
421421

422-
final text = file.text
422+
try(BufferedReader reader = Files.newBufferedReader(file)) {
423+
final lines = reader.readLines()
424+
if( !lines )
425+
return this
426+
if( lines[0] != 'nextflow.trace/v2' )
427+
return parseLegacy(file, lines)
428+
429+
for( int i=0; i<lines.size(); i++ ) {
430+
final pair = lines[i].tokenize('=')
431+
final name = pair[0]
432+
final value = pair[1]
433+
if( value == null )
434+
continue
435+
436+
switch (name) {
437+
case '%cpu':
438+
case '%mem':
439+
// fields '%cpu' and '%mem' are expressed as percent value
440+
this.put(name, parseInt(value, file, name) / 10F)
441+
break
442+
443+
case 'rss':
444+
case 'vmem':
445+
case 'peak_rss':
446+
case 'peak_vmem':
447+
// these fields are provided in KB, so they are normalized to bytes
448+
def val = parseLong(value, file, name) * 1024
449+
this.put(name, val)
450+
break
451+
452+
case 'cpu_model':
453+
this.put(name, value)
454+
break
455+
456+
default:
457+
def val = parseLong(value, file, name)
458+
this.put(name, val)
459+
break
460+
}
423461

424-
final lines = text.readLines()
425-
if( !lines )
426-
return this
427-
if( lines[0] != 'nextflow.trace/v2' )
428-
return parseLegacy(file, lines)
429-
430-
for( int i=0; i<lines.size(); i++ ) {
431-
final pair = lines[i].tokenize('=')
432-
final name = pair[0]
433-
final value = pair[1]
434-
if( value == null )
435-
continue
436-
437-
switch (name) {
438-
case '%cpu':
439-
case '%mem':
440-
// fields '%cpu' and '%mem' are expressed as percent value
441-
this.put(name, parseInt(value, file, name) / 10F)
442-
break
443-
444-
case 'rss':
445-
case 'vmem':
446-
case 'peak_rss':
447-
case 'peak_vmem':
448-
// these fields are provided in KB, so they are normalized to bytes
449-
def val = parseLong(value, file, name) * 1024
450-
this.put(name, val)
451-
break
452-
453-
case 'cpu_model':
454-
this.put(name, value)
455-
break
456-
457-
default:
458-
def val = parseLong(value, file, name)
459-
this.put(name, val)
460-
break
461462
}
462463

464+
return this
463465
}
464-
465-
return this
466466
}
467467

468468
private TraceRecord parseLegacy( Path file, List<String> lines) {

modules/nextflow/src/test/groovy/nextflow/trace/TraceRecordTest.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package nextflow.trace
1818

19+
import java.nio.file.NoSuchFileException
20+
import java.nio.file.Path
21+
1922
import groovy.json.JsonSlurper
2023
import spock.lang.Specification
2124
import spock.lang.Unroll
@@ -333,4 +336,12 @@ class TraceRecordTest extends Specification {
333336
'COMPLETED' | true
334337
}
335338

339+
def 'should throw file not found exception' () {
340+
given:
341+
def rec = new TraceRecord([:])
342+
when:
343+
rec.parseTraceFile(Path.of('unknown'))
344+
then:
345+
thrown(NoSuchFileException)
346+
}
336347
}

0 commit comments

Comments
 (0)