When running the following script under bash-4.4.0 to bash-4.4.23 it
seems to fail erroneously on read.

  set -x
  set -o pipefail
  shopt -s lastpipe

  main() {
    # Any input piped into while such that the condition
    # succeeds at least once.
    echo "x" | while read; do
      echo | read # anything piped to read
      sleep 0     # any non-builtin
    done
  }

  main || echo "main failed"

This produces the xtrace:

  + set -o pipefail
  + shopt -s lastpipe
  + main
  + read
  + echo x
  + read
  + echo
  + sleep 0
  + read
  + echo 'main failed'
  main failed

Under bash-4.3 and bash-5.0 the script does not output "main failed",
as expected.

I'm not sure if this is a known bug. One user on IRC reported finding
the same issue on debian stretch, but another could not reproduce the
issue for bash-4.4.12.

I have bisected the history of the devel git branch and found that the commit:
  85ec0778f9d778e1820fb8c0e3e996f2d1103b45 commit bash-20150417 snapshot
introduces the issue.

Applying the attached patch to a bash-4.4.23 source tree produces an
interpreter which does not output "main failed" when running the
aforementioned script.
diff --git a/jobs.c b/jobs.c
index d69b8d8d..ec014d1f 100644
--- a/jobs.c
+++ b/jobs.c
@@ -1043,9 +1043,10 @@ delete_job (job_index, dflags)
   if (temp == 0)
     return;
 
-  if ((dflags & DEL_NOBGPID) == 0 && (temp->flags & (J_ASYNC|J_FOREGROUND)) == J_ASYNC)
+  if ((dflags & DEL_NOBGPID) == 0)
     {
       proc = find_last_proc (job_index, 0);
+      /* Could do this just for J_ASYNC jobs, but we save all. */
       if (proc)
 	bgp_add (proc->pid, process_exit_status (proc->status));
     }

Reply via email to