Skip to content

No output staging back to task.workDir → tasks never bind outputs, downstream stays PENDING #14

Closed

Description

@scttfrdmn

Summary

nf-spawn has no copy-back logic — no scp, no S3 download, no .exitcode / .command.out / .command.err handling anywhere in src/ (grep is empty). buildTaskScript() runs the script in a remote working dir on the spawn instance and only writes a SPAWN_COMPLETE marker; nothing returns results to Nextflow.

Nextflow finalizes a task by reading .exitcode plus the declared output files in task.workDir (the local/shared work directory it controls). Those files never materialize there, so the task can't bind its outputs. Downstream consumers never become runnable and sit in PENDING forever — the exact symptom in #12:

No more task to compute -- The following nodes are still active: [operator] map / collectFile / subscribe
pendingCount=2; submittedCount=0

(In that run FETCH_FASTQ (1) was submitted; the two stuck tasks are its downstream consumers, blocked because FETCH_FASTQ produced no retrievable outputs.)

This is the real blocker behind #12

The PENDING hang reported in #12 is caused by this missing staging, not by the --wait-for-ssh blocking described there (that analysis was stale — submit() already uses --wait-for-running=false --wait-for-ssh=false).

Design decision needed

Stage .exitcode and the declared output files back to task.workDir. Options:

  1. Shared filesystem. Mount the Nextflow work dir (S3/EFS) on the instance at the same path so both sides see task.workDir directly. buildTaskScript() writes .exitcode + outputs there. Cleanest if a shared FS is in play.
  2. scp-back before termination. Copy the remote work dir back over SSH. Complication: the handler launches with --on-complete terminate, so the copy must happen before the instance self-terminates — spawn status/scp can't reach a terminated instance afterward. Would need a copy step gated on completion-but-not-yet-terminated, or dropping auto-terminate in favor of an explicit post-stage teardown.

Must also write a real .exitcode reflecting the task's actual exit status (the current spored complete --status success || touch /tmp/SPAWN_COMPLETE discards the real code).

Context

Split out from #12. Blocks any real Nextflow pipeline (e.g. https://github.com/scttfrdmn/aws-microbiome-demo). Depends on the --user-data fix (separate issue) actually running the script first.

Activity

  1. added
    bug
    on Jun 3, 2026
  2. scttfrdmn commented on Jun 3, 2026

    @scttfrdmn
    CollaboratorAuthor

    Staging design: S3 task.workDir is the right answer

    In the microbiome demo setup, workDir is already an S3 path:

    workDir = 's3://bucket/work/job/'

    Nextflow + nf-amazon handles S3 staging automatically for supported executors — it uploads inputs to task.workDir before the task runs, and expects to read .exitcode, .command.out, .command.err, and declared output files from task.workDir after the task completes.

    What nf-spawn's task script should do

    1. Download inputs from task.workDir (S3) to local working dir on the instance
    2. Run the task script (the .command.run that Nextflow provides — this is what buildTaskScript() should emit)
    3. Write .exitcode to local dir
    4. Upload everything (.exitcode, .command.out, .command.err, declared outputs) back to task.workDir (S3) before termination

    The AWS CLI is already present on all instances (AL2023), and the instance role already has S3 read/write on the bucket (we added it for the demo). So staging via aws s3 sync is practical.

    Skeleton of what the staged task script should look like

    #!/bin/bash
    WORK_DIR_S3="@@TASK_WORK_DIR@@"   # e.g. s3://bucket/work/job/ab/1234.../
    LOCAL_DIR=/tmp/nf-work
    
    mkdir -p "${LOCAL_DIR}"
    
    # Stage inputs from S3
    aws s3 sync "${WORK_DIR_S3}" "${LOCAL_DIR}/" \
        --exclude "*" --include ".command.*" --include "*.sra" \
        --region us-east-1 --quiet
    
    cd "${LOCAL_DIR}"
    
    # Run the actual Nextflow task script
    bash .command.run 2>.command.err 1>.command.out
    echo $? > .exitcode
    
    # Stage outputs back to S3
    aws s3 sync "${LOCAL_DIR}/" "${WORK_DIR_S3}" \
        --region us-east-1 --quiet
    
    touch /tmp/SPAWN_COMPLETE

    The key insight: Nextflow already writes .command.run to task.workDir before dispatching the task. nf-spawn just needs to download it, execute it, and sync back the results. The nf-amazon plugin handles the S3 ↔ local translation for inputs/outputs declared in the process definition.

    Why not scp-before-terminate

    The --on-complete terminate approach has a race condition: the instance needs to copy results back before self-terminating, but if the copy fails partway through, results are lost with no recovery. S3 sync is atomic at the object level and idempotent — a partial upload can be retried.

    One subtlety

    task.workDir in Nextflow is a Path object — for S3 work dirs, it's something like s3://bucket/work/job/ab/1234abcd5678.... nf-spawn's SpawnTaskHandler already has access to this via task.workDir.toUriString(). The task script template just needs this injected as a variable.

  3. scttfrdmn commented on Jun 3, 2026

    @scttfrdmn
    CollaboratorAuthor

    Agree the S3 task.workDir approach is right, and rejecting scp-before-terminate for the idempotency reason is the correct call. One implementation prerequisite to flag, because the skeleton assumes something that isn't true of the current handler:

    .command.run doesn't exist yet — nothing builds it. The comment says "Nextflow already writes .command.run to task.workDir before dispatching the task." That only holds when the executor's handler builds the bash wrapper itself, the way grid executors do. nf-spawn currently does not: there's no BashWrapperBuilder/TaskBean usage anywhere (grep for command.run/wrapper is empty), and buildTaskScript() hand-rolls a script from task.script. So the staged script's bash .command.run would find no such file.

    So #14 actually has two parts:

    1. Generate the wrapper in submit(). Adopt nextflow.executor.BashWrapperBuilder (build a TaskBean from the TaskRun and call .build()), which writes .command.run + .command.sh into task.workDir. This is what makes .command.out/.command.err/.exitcode semantics line up with what Nextflow reads back — and it replaces the current hand-rolled buildTaskScript() entirely. Worth confirming the exact 26.04.x BashWrapperBuilder/TaskBean API while we're touching SpawnExecutor.createTaskMonitor uses removed TaskPollingMonitor.create signature (Nextflow 26.x) #9-era signatures.
    2. Inject S3 staging around it as in the skeleton, using task.workDir.toUriString().

    Two follow-on details for the skeleton:

    • The input aws s3 sync ... --include "*.sra" is demo-specific. With the BashWrapperBuilder approach the inputs are already declared in .command.run's stage-in section (nf-amazon resolves them), so the explicit --include allowlist shouldn't be needed — sync the whole work dir prefix down, or rely on the wrapper's own staging.
    • .exitcode must capture the real task exit code (the skeleton's echo $? > .exitcode does this correctly) — note this supersedes the current spored complete --status success || touch which discards it. And the upload of .exitcode must be last / after the outputs so Nextflow doesn't observe a completed exitcode before the outputs land.

    Net: #13 (@-prefix) unblocks the script running at all; #14 is really "replace buildTaskScript with BashWrapperBuilder + S3 sync wrapper." Sequence is #13 → #14.

  4. scttfrdmn commented on Jun 3, 2026

    @scttfrdmn
    CollaboratorAuthor

    Implemented in #16 — but I dropped the BashWrapperBuilder part of my earlier comment after verifying it against the 26.04.3 jar. Recording why:

    BashWrapperBuilder(task).build() exists and would write .command.run into task.workDir, but it bakes the work-dir path straight into the wrapper — the exit-file printf -- $exit_status {{exit_file}}, the {{stdout_file}}/{{stderr_file}} redirects, and the nxf_stage/nxf_unstage blocks (confirmed from nextflow/executor/command-run.txt in the jar). Those paths come from the copy strategy. For an S3 work dir the correct strategy lives in nf-amazon, which is not on a generic executor plugin's classpath — so the builder falls back to SimpleFileCopyStrategy (POSIX) and emits things like printf 0 s3://bucket/.../.exitcode, which the instance can't write. So .command.run isn't usable as-is for an S3 work dir here.

    So #16 uses the self-contained aws s3 sync wrapper from your skeleton instead: sync the work dir down → run .command.sh locally (capturing .command.out/.command.err + real .exitcode) → sync back, .exitcode last. It doesn't depend on a copy strategy and is directly unit-testable.

    Two refinements vs the skeleton:

    • No --include "*.sra" allowlist — sync the whole work-dir prefix down so it's pipeline-agnostic.
    • .exitcode uploaded after the output sync (separate aws s3 cp), so its appearance always trails the outputs and Nextflow can't finalize early.

    Revisiting BashWrapperBuilder would be worth it only if nf-spawn later declares an nf-amazon dependency / S3 copy strategy; noting that as possible future work rather than blocking this.

  5. scttfrdmn commented on Jun 3, 2026

    @scttfrdmn
    CollaboratorAuthor

    Update: still blocked even with 0.2.1 S3 staging fix

    After upgrading to 0.2.1, Nextflow's TaskPollingMonitor is still timing out (5 min default) before the instance even boots and receives .command.run from S3.

    The S3 work dir exists but is empty — Nextflow created it, but .command.run was never written there because Nextflow abandoned the session before completing input staging.

    Root cause update: The monitor timeout fires ~5 min after submit() is called. EC2 instance boot + spored setup takes ~2-3 min, then the task needs to download .command.run, execute, and upload outputs. This means a t4g.medium takes at least 5-8 min per task, longer than the 5-min monitor timeout.

    What needs to happen in the 0.2.1+ task lifecycle:

    1. submit() calls spawn launch → returns quickly (non-blocking ✅ already in 0.2.1)
    2. Nextflow stages .command.run to S3 task.workDir ← this needs to happen before the task can start
    3. The spawned instance boots, runs .command.run, uploads results
    4. nf-spawn polls spawn status --check-complete and marks task done

    The problem: Step 2 won't happen if Nextflow has already given up (step 4 never fires "running"). The task needs to transition to RUNNING state for Nextflow to stage inputs.

    Suggested fix: In checkIfRunning(), return true (RUNNING) immediately after spawn launch succeeds (instance is booting), rather than waiting for SPAWN_COMPLETE. Then poll for COMPLETE separately in checkIfCompleted(). This keeps Nextflow's session alive and allows it to stage inputs.

  6. scttfrdmn commented on Jun 3, 2026

    @scttfrdmn
    CollaboratorAuthor

    I dug into this update against the 0.2.1 code + the Nextflow 26.04.3 jar before acting on it, because parts of the diagnosis don't match what the code actually does. Want to correct the record so we fix the right thing:

    1. The suggested fix is already implemented. "In checkIfRunning(), return RUNNING immediately after spawn launch succeeds." submit() already sets status = TaskStatus.RUNNING (SpawnTaskHandler.groovy:74) the moment launch returns 0, and checkIfRunning() returns true while polling (:83-84). The task transitions to RUNNING right after the non-blocking launch — there's nothing to change there.

    2. There is no 5-minute monitor abandon-timeout. I checked TaskPollingMonitor in the jar: it has no per-task timeout that abandons RUNNING/SUBMITTED tasks. The "No more task to compute -- nodes still active" line is the periodic dumpInterval status dump (default 5 min) — a heartbeat log, not a give-up. A workflow can sit there for hours; the 5-min cadence is just when it re-prints. So "the monitor timed out at 5 min" isn't the mechanism.

    3. "Nextflow stages .command.run to S3" — it does NOT, for this executor. This is the actual crux, and it's the same point from my earlier comment: input/.command.run staging to an S3 work dir is done by nf-amazon's copy strategy, which isn't on a generic executor plugin's classpath. So nothing writes .command.run to the S3 work dir — which is exactly why 0.2.1's staging script (bash .command.sh built from task.script) doesn't depend on it. The "S3 work dir exists but is empty" observation is expected and not a regression; .command.run was never going to appear there.

    So what is failing? If a real instance launched and the workflow still produced nothing, the likely culprits are downstream of launch, not the monitor:

    • spored/SSH completion detection. checkIfRunning() shells spawn status <name> --check-complete, which SSHes to the instance to run spored (spawn cmd/status.go:77-95) and needs a resolvable public IP + reachable spored. If spored never installed, or the instance has no public IP, or it already self-terminated (--on-complete terminate → resolveInstance can't find it), --check-complete returns 3 and we poll forever — RUNNING but never COMPLETED. That matches "never marked running or complete" far better than a monitor timeout.
    • Did the staging script actually run? With 0.2.1 the script is appended after spored's bootstrap in user-data (launch.go:2417-2418, under set +e). To confirm it executed at all we need the instance's cloud-init log / /tmp/SPAWN_COMPLETE / whether .exitcode ever landed in S3.

    To pin this down, could you grab from one stuck run:

    1. spawn list + spawn status <nf-name> while it's "stuck" — is the instance running, and does it have a public IP?
    2. On the instance (or its console log): did cloud-init run the appended staging script? Is spored installed (/usr/local/bin/spored)?
    3. aws s3 ls <workDir> — did .command.out/.exitcode ever appear?
    4. The actual spawn status <name> --check-complete; echo $? exit code.

    My strong suspicion: this is a completion-detection / instance-reachability problem (spored-over-SSH, or the instance self-terminating before status can read it), not a Nextflow monitor timeout and not the already-present RUNNING transition. Let's confirm with the above before changing the lifecycle — I don't want to "fix" the state machine when the real gap is spored/SSH or self-terminate timing.

  7. scttfrdmn commented on Jun 3, 2026

    @scttfrdmn
    CollaboratorAuthor

    Closing the loop for the record: the "still blocked after 0.2.1" symptom reported here was not a separate completion-detection bug — it was the same root cause as #18. The head node was running spawn 0.36.6 (pre-#34), where the pipe-mode JSON-parse bug silently swallowed the launch, so no instance ever ran the staging script. Once -y landed (#18 / v0.2.2) and the head node moved off 0.36.6, the staging implemented here (#14) worked end-to-end and the pipeline progressed.

    So the resolved chain is: #13 (script runs) → #14 (results stage back to S3) → #18 (launch actually fires on the head node's spawn version). No further nf-spawn change needed; my earlier request for instance/cloud-init diagnostics is moot. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions