Skip to content

session-start-hook.sh cannot be profiled with bash -x: bootstrap-dirs.sh:364 redirects fd 2 mid-run and the trace stops silently #690

Closed

Description

@fdaviddpt

What happens

scripts/bootstrap-dirs.sh:364 runs:

exec 2>> "$REMEMBER_DIR/logs/hook-errors.log"

From that line on, everything written to fd 2 goes to the hook's own error log -- including an xtrace stream, if the profiler put one there. bash -x scripts/session-start-hook.sh therefore produces a trace that covers only the part of the run before bootstrap-dirs.sh is sourced, and nothing anywhere says the rest was lost.

Measured while building the #660 diagnostic, on macOS, one run:

wall 0.71s | traced span 0.07s | 238 traced lines

90% of the run was missing. With BASH_XTRACEFD pointed at a dedicated fd, the same run gives wall 0.71s | traced span 0.66s | 1372 traced lines.

Why it matters

A truncated trace does not look truncated. It looks like a complete profile of a fast hook, and every number derived from it -- fork counts, per-step attributions, "the largest gap is X" -- describes a fraction of the run while reading as though it describes all of it. This is the repo's own cardinal failure shape (a thing that ran, produced a clean result, and silently covered a fraction of what it claimed) sitting inside a diagnostic aid.

It is also not hypothetical: #660 has been reasoned about for days using PS4='+$EPOCHREALTIME ' bash -x measurements from a reporter, and unless their trace was on a non-default fd it stopped at this same line.

Not a proposal to remove the redirect

The redirect exists for a reason -- a hook's stderr must not leak into the host's transcript (#643 and the stderr-leak lane). The ask is that it stop being invisible:

  • leave the redirect alone when BASH_XTRACEFD is already set to something other than 2, or when an explicit opt-out (REMEMBER_TRACE=1) is present; and/or
  • document it at the redirect and in docs/diagnostics.md, so the next person to profile this hook is told where their trace went rather than discovering it by arithmetic.

A positive control belongs in whichever shape lands: a traced run whose span is within some margin of its wall clock, so a future redirect that swallows the trace again fails a test rather than a person.

[AI-generated]

Activity

  1. added
    cohort-12
    priority:medium
    lane-capture
    on Sep 15, 2026
  2. fdaviddpt commented on Sep 16, 2026

    @fdaviddpt
    ContributorAuthor

    Two platform floors the fix in PR #708 does not yet clear. Recording them here rather than
    only on the pull request, so they survive if that branch stalls. Both are read out of run
    #35091652526 (16 legs, 8 failed: every macOS leg and every Windows leg; all Linux legs green).

    macOS, bash 3.2 -- a real defect in the fix, not a test artifact.
    tests/test_trace_not_swallowed_690.py:164 fails with assert 'echo MARKER-690-ON-FD-2' in '':
    the trace file is empty. GitHub's macOS runners resolve bash to stock /bin/bash 3.2, the same
    floor tests/test_session_start_fork_tax_665.py already names in its $BASHPID skip reasons.
    BASH_XTRACEFD was added in bash 4.1, so on 3.2 export BASH_XTRACEFD=9 is ignored and xtrace
    stays on fd 2 -- while the new guard at scripts/bootstrap-dirs.sh:384 reads
    [ "${BASH_XTRACEFD:-2}" = "2" ], sees 9, concludes the trace is safely elsewhere, and
    redirects fd 2 into hook-errors.log anyway. That is the #690 symptom itself, reproduced by the
    fix, on the platform the original measurement was taken on.

    The guard needs a version test alongside the fd test: below bash 4.1 (BASH_VERSINFO[0] < 4, or
    4 with BASH_VERSINFO[1] < 1), BASH_XTRACEFD is not honoured, so xtrace is on fd 2 regardless
    of what the variable says and the redirect must stand down.

    Windows, Git Bash -- not yet diagnosed.
    tests/test_trace_not_swallowed_690.py:242 fails with assert 'NOW-BODY-690' in '': running
    bash -x scripts/session-start-hook.sh exits 0 and produces zero bytes on stdout. The preceding
    six tests in the same file pass on Windows, so bash resolution and path handling are not the
    cause. The hook's stdout goes through the _REMEMBER_CTX_FILE buffer flushed by the cat calls
    at scripts/session-start-hook.sh:2003 and :2006; under trace on Git Bash that flush yields
    nothing. Whether the buffer redirect never engages, or engages and the flush branch is not
    reached, has not been established -- it needs a Windows runner to answer, and no claim is made
    here beyond what the log shows.

    [AI-generated]

  3. added 3 commits that reference this issue on Sep 16, 2026
  4. fdaviddpt commented on Sep 16, 2026

    @fdaviddpt
    ContributorAuthor

    Reopening. PR #708 merged and fixed the macOS bash-3.2 regression, but the issue's own
    end-to-end promise (the real hook's bash -x trace surviving past the bootstrap) is still
    unverified on Windows -- test_the_real_hook_traces_past_the_bootstrap is xfail'd there,
    not passing, tracked separately as #712.

    I edited PR #708's body before merging to change "Closes #690" to "Part of #690" for exactly
    this reason, but GitHub's merge auto-close used the issue link established at PR creation time
    rather than the edited body text, and closed this anyway. Reopening to correct that.

    This will close for real once #712 is fixed and the xfail marker on
    test_the_real_hook_traces_past_the_bootstrap is removed.

    [AI-generated]

  5. added 2 commits that reference this issue on Sep 19, 2026
  6. removed their assignment
    on Sep 19, 2026
  7. added a commit that references this issue on Sep 23, 2026
    a0dd1d5
  8. added a commit that references this issue on Oct 5, 2026
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

    cohort-12lane-capturepriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions