Skip to content

fix: report missing closure blocks instead of panicking - #19159

Merged
fdncred merged 1 commit into
nushell:mainfrom
Monster-GM:fix/job-closure-missing-block
Oct 16, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
Monster-GM:fix/job-closure-missing-block

Conversation

@Monster-GM

@Monster-GM Monster-GM commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

When a job is spawned before a later input is parsed, the job retains an older EngineState. The later closure's BlockId is therefore unavailable in that job, which previously panicked in EngineState::get_block.

  • remove the infallible ClosureEval / ClosureEvalOnce constructors and make every try_new* constructor take a span pointing at the failing call or closure value, so a stale BlockId can no longer panic through any entry point (do, each, zip, where, …)
  • add EngineState::get_closure_block as the single fallible lookup with a span and actionable help (shared MISSING_CLOSURE_BLOCK_HELP const); direct get_block callers on runtime closure values (tee, with-env, export-env, explain, view source, record where, job spawn, debug profile, the hook runner and the tui closure_arity) now report the same error instead of panicking
  • debug profile checks the block before installing the profiler, and job spawn checks before registering the job, so the error reaches the caller's try/catch instead of the background thread
  • best-effort consumers degrade instead of panicking: describe --detailed omits the signature key, to text / to toml --serialize fall back to the existing placeholder string, to yaml --serialize returns the closure_block_not_found error, and Closure::coerce_into_string (to msgpack, to kdl) returns CantConvert
  • par-each builds the evaluator once in run (skipping empty input like each) and clones it into the workers, returning a single error instead of one error value per item
  • default propagates the construction error instead of storing it as the default value
  • generate reads the signature from the evaluator's block() instead of looking the block up twice
  • ClosureEval / ClosureEvalOnce expose block(), removing the remaining second lookups in record where, the tui call_closure and generate
  • convert the record commands from record family of commands #19190 to the fallible constructors
  • add regression tests that reproduce the interactive, multi-input job spawn / job send flow from Panics executing a closure received from the main process in a job #19147 through do, each, zip, tee, par-each, record each, view source, describe --detailed, to text --serialize, debug profile, job spawn, and a combined test for with-env, explain, generate, record where, record walk and sort-by

Removing the infallible constructors is a breaking change to nu-engine's public API without a deprecation step: a deprecated shim cannot avoid the panic, which is what this PR removes, so the break is intentional.

User-facing changes (Release notes)

Report missing closure blocks instead of panicking

Fixed a bug where using a closure created after a job was spawned could crash the job. Nushell now reports Closure cannot be evaluated with a source span and helpful hint instead. Errors from job spawn and debug profile reach the caller's try/catch. For unavailable closures, par-each returns a single error rather than an error per item, and default propagates the error instead of storing it as the default value.

Inspecting or serializing these closures no longer crashes: describe --detailed omits the signature key, to text and to toml --serialize use the existing placeholder string, to yaml --serialize reports closure_block_not_found, and to msgpack and to kdl report conversion errors.

Additional notes

Validation

  • cargo test -p nu-command --test tests job (44 passed; do / each / zip / tee / par-each / record each / record where / record walk / view source / describe / to text / debug profile / job spawn / with-env / explain / generate / sort-by scenarios)
  • cargo test -p nu-command --test tests (3438 passed; the 21 failures are pre-existing Windows symlink-privilege environment failures, confirmed identical on upstream main)
  • cargo test for nu-engine, nu-cmd-lang, nu-protocol, nu-tui, nu-cmd-base, nu-color-config, nu-heavy-utils, nu-cli, nu-cmd-extra, nu-lsp, nu-dap, nu-plugin-engine (only the same symlink-privilege failures, confirmed on upstream main)
  • cargo test -p nu-engine --doc, cargo test -p nu-protocol --doc
  • cargo fmt --all -- --check
  • cargo clippy -p nu-engine -p nu-cmd-lang -p nu-command -p nu-cli -p nu-cmd-extra -p nu-color-config -p nu-plugin-engine -p nu-tui -p nu-protocol -p nu-cmd-base -p nu-heavy-utils --tests --no-deps -- -D warnings

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Other closure-consuming commands retain infallible evaluator construction and can still panic on the same stale closure.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Prevents do from panicking when evaluating closures whose blocks are absent from a job’s cloned engine state.

Changes:

  • Adds fallible ClosureEvalOnce constructors.
  • Updates do to propagate missing-block errors.
  • Adds a multi-input job regression test.
File Description
crates/​nu-engine/​src/​closure_eval.rs Adds fallible closure evaluator construction.
crates/​nu-cmd-lang/​src/​core_commands/​do_.rs Uses fallible constructors in do.
crates/​nu-command/​tests/​commands/​job.rs Tests the reported job/closure scenario.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/nu-engine/src/closure_eval.rs Resolved
@fdncred

fdncred commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

are you able to address the review?

Monster-GM added a commit to Monster-GM/nushell that referenced this pull request Oct 9, 2026
Address the review on nushell#19159: the stale-closure panic was still
reachable through the infallible ClosureEval/ClosureEvalOnce
constructors, e.g. a job receiving a closure parsed after it was
spawned and passing it to zip, or each/where via ClosureEval::new.

- remove the infallible constructors so missing-block lookup can only
  happen through the fallible try_* variants
- add ClosureEval::try_new / try_new_preserve_out_dest
- migrate all closure-consuming commands to propagate the error
- generate now reports the same error instead of panicking on
  EngineState::get_block for a stale closure
- add job regression tests for zip and each (nushell#19147 flow)
@Monster-GM

Copy link
Copy Markdown
Contributor Author

Addressed the review — thanks for the nudge! The infallible ClosureEval/ClosureEvalOnce constructors are gone: construction is now fallible across all public entry points (try_*), and every closure-consuming command propagates the missing-block error instead of panicking, including the zip and each/where paths flagged in the review. generate also had a direct EngineState::get_block on the closure that would have panicked the same way, so it now reports the error too. Added job regression tests covering the zip and each scenarios. PTAL!

@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch from fcbd49f to 9688cb1 Compare October 9, 2026 17:45
@fdncred

fdncred commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Let's do one more clanker review to make sure everything checks out.

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

The fallible constructors fix do, each and zip, but the branch no longer compiles against main after #19190, and tee, describe --detailed, the to serializers and the tui hooks still panic on the same closure.

Findings: 3 High severity · 2 Medium severity · 3 Low severity

Open (8)

Reviewed by reading the code at 9688cb1 and comparing it with current main (67edfb6). I didn't build or run the tests.

Comment thread crates/nu-engine/src/closure_eval.rs Resolved
Comment thread crates/nu-tui/src/hooks.rs Resolved
Comment thread crates/nu-command/tests/commands/job.rs Resolved
Comment thread crates/nu-engine/src/closure_eval.rs Resolved
Comment thread crates/nu-engine/src/closure_eval.rs Resolved
@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch 2 times, most recently from 6c03fdd to 99ffc63 Compare October 10, 2026 09:00
@fdncred

fdncred commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

when you're done, please respond to each review comment so we can be sure all have been addressed. thanks!

@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch 2 times, most recently from 17137ef to 25c14a6 Compare October 10, 2026 13:51

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 25c14a6

I checked all nine earlier threads against this commit. Seven are resolved. The par-each and tui preview threads have follow-ups posted in their threads.

New findings, inline below:

  • debug profile leaves the profiler active when the closure is stale.
  • job spawn reports a stale-closure error from the background thread instead of returning it.
  • The YAML help string contains a run of spaces.
  • Three call sites look up the block twice.
  • Three changed paths have no regression test.

The release notes say to yaml --serialize and to msgpack --serialize degrade to a placeholder. They return an error: closure_block_not_found for YAML and CantConvert for msgpack. Only to text, to toml and describe --detailed produce a placeholder.

I reviewed by reading the code at 25c14a6. CI is green on that commit. I didn't build it or run the tests.

Comment thread crates/nu-command/src/debug/profile.rs Resolved
Comment thread crates/nu-heavy-utils/src/yaml/error.rs Resolved
Comment thread crates/nu-command/src/record/where_.rs Resolved
Comment thread crates/nu-command/tests/commands/job.rs Resolved
@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch 2 times, most recently from a740756 to a04bea0 Compare October 10, 2026 15:40

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes from my clanker

Review of a04bea0

I built this commit and ran cargo test -p nu-command --test tests -- commands::job (45 pass). I also drove the REPL through a pty and fed a job a closure parsed after it was spawned, then ran it through about 50 consumers (items, reduce, insert, tee, par-each, the to serializers, view ir, describe -d, table and so on). None of them panic. Root CI was still pending when I checked.

Six of the seven open threads are fixed, and I resolved them. I left a note on the par-each thread about leftover clones. The tests thread stays open: default is reachable from a script, see the reply there.

New findings, inline below:

  • Errors from sites that use the closure value's span render OutOfBounds instead of a label in the job.
  • job spawn keeps a second copy of head.
  • closure_arity takes a span that its only caller sets to Span::unknown().
  • The combined consumer test reports only false when it fails.

Comment thread crates/nu-command/src/filters/insert.rs Resolved
id
};

let call_head = call.head;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call_head is a copy of head from line 56. Span is Copy, so the move closure can capture head directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped; the thread now captures head directly.

Comment thread crates/nu-tui/src/hooks.rs Resolved
Comment thread crates/nu-command/tests/commands/job.rs Resolved
@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch from a04bea0 to 784e472 Compare October 11, 2026 01:00
@fdncred

fdncred commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

ok, please fix the conflicts so we can try to move on with this. thanks!

@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch from 784e472 to 3b2d8f9 Compare October 16, 2026 05:36
@Monster-GM

Copy link
Copy Markdown
Contributor Author

Conflicts fixed and rebased onto current main (single commit as before).

The par-each conflict with #19229 needed a closer look: the evaluator built once in run carries the recursion_count + 1 from captures_to_stack, and each worker clones it, so closure calls in workers count toward the limit exactly once — same as the --keep-order path. While checking that, I found par-each workers still aborted on infinite closure recursion on current main: Rayon's default worker stack can't fit 50 nested closure calls, so the limit check never got a chance to fire. The pool now builds with an 8MB stack, and there's a regression test next to your infinite_closure_recursion_does_not_panic one.

Everything green on my side: the recursion tests, the job/par-each/group-by suites, fmt and clippy. Only the usual Windows symlink-privilege failures in the full run.

@fdncred

fdncred commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

ugh, more conflicts. sorry.

When a job is spawned before a later input is parsed, the job retains an
older EngineState. The later closure's BlockId is therefore unavailable
in that job, which previously panicked in EngineState::get_block.
Closure evaluator construction is now fallible across all public entry
points and every closure-consuming command propagates the error, so this
condition returns 'Closure cannot be evaluated' with a span and
actionable help instead of panicking.

- remove the infallible ClosureEval / ClosureEvalOnce constructors and
  make every try_new* constructor take a span pointing at the failing
  call or closure value
- add EngineState::get_closure_block as the single fallible lookup with
  a span and help text; direct get_block callers on runtime closure
  values (tee, with-env, explain, view source, record where, the hook
  runner and the tui closure_arity) now report the error too
- degrade gracefully where a best-effort string is enough: describe
  --detailed, the to text / to toml / to yaml / to msgpack serializers
  and Closure::coerce_into_string
- par-each checks the block once before consuming input (skipping empty
  input like each) instead of yielding one error value per item
- default propagates the construction error instead of storing it as
  the default value
- generate looks the block up once through try_new
- convert the record commands from nushell#19190 to the fallible constructors
- add regression tests that reproduce the interactive, multi-input
  job spawn / job send flow from nushell#19147 through do, each, zip, tee,
  par-each, record each, view source, describe --detailed, to text
  --serialize, and a combined test for with-env, explain, generate,
  record where, record walk and sort-by

Fixes nushell#19147
@Monster-GM
Monster-GM force-pushed the fix/job-closure-missing-block branch from 3b2d8f9 to 039a899 Compare October 16, 2026 14:08
@Monster-GM

Copy link
Copy Markdown
Contributor Author

No worries 🙂 Rebased again — still a single commit.

Only real overlap was par-each with #19252: took your wrap_error_with_input for the worker error wrapping and kept the shared-evaluator structure on top of it. Also adapted the par-each recursion test to the new EvalBlockWithInputError shape. Nice change, by the way — the job tests now read a lot cleaner with the closure's own message in .msg.

@fdncred
fdncred merged commit cfeea82 into nushell:main Oct 16, 2026
11 checks passed
@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 16, 2026
@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 16, 2026
kronberger-droid added a commit to kronberger-droid/nushell that referenced this pull request Nov 13, 2026
…nfig

The prompt resolver takes over the `ClosureEvalOnce::try_new` call that
nushell#19159 gave `get_prompt_string`, so a prompt closure whose block is gone
is still reported instead of panicking. `commandline set-prompt` builds
its push as a `PromptContents` the way nushell#19236 does, with `vi_visual`
added.

The deprecation note in `doc_config.nu` names 0.118.0, the release this
now lands in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panics executing a closure received from the main process in a job

3 participants