Skip to content

set -T (functrace) DEBUG trap is not inherited into $( ) command substitution — GNU keeps it in the comsub child (trap.c:1469-1471) #825

Closed

Description

@caomengxuan666

Summary

Found by the #713 nested-context audit second pass (probe debug-into-comsub-functrace-on in scripts/audit/nested-context-probes.py, documented in docs/audit/context-inheritance.md §3.1). Under set -T/set -o functrace, GNU bash keeps the DEBUG trap armed inside a $( ) command-substitution child; rubash resets it unconditionally.

GNU anchor: third_party/bash trap.c:1466-1474 (reset_or_restore_signal_handlers):

  if (function_trace_mode == 0)
    {
      sigmodes[DEBUG_TRAP] &= ~SIG_TRAPPED;
      sigmodes[RETURN_TRAP] &= ~SIG_TRAPPED;
    }
  if (error_trace_mode == 0)
    sigmodes[ERROR_TRAP] &= ~SIG_TRAPPED;

DEBUG/RETURN are cleared only when function_trace_mode == 0 — under -T the comsub child keeps the DEBUG trap, and the body's commands fire it.

Repro (WSL GNU bash 5.3.0(1) vs rubash master 2026-10-13)

$ set -T; trap 'echo D:$BASH_COMMAND' DEBUG; x=$(true); echo got=$x
D:x=$(true)
D:echo got=$x
got=D:true

rubash prints got= — no D:true fire for the comsub body's true.

Mechanism: rubash's command-substitution copy path calls the unconditional reset_for_subshell (src/builtins/trap.rs, reset_for_subshell_inner(env_vars, false)); there is no functrace-scoped variant on that path the way reset_for_subshell_child scopes ERR inheritance for ( ) (rubash#702/#719). The functrace-off face already matches GNU (probe debug-into-comsub-functrace-off), and an install inside the substitution already fires (rubash#345) — the gap is exactly the inherited-under--T case.

Suggested direction

Same shape as the #702 fix: a reset_for_comsub_child-style variant (or a flag on reset_for_subshell_inner) that keeps DEBUG/RETURN when functrace is enabled, used by the command-substitution copy path only — the ( ) subshell path (reset_for_subshell_child) and pipeline-element path keep their current contracts after a WSL oracle check.

Relation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions