Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-55867

sh step termination is never detected if the wrapper process is killed

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • durable-task-plugin
    • None
    • Jenkins 2.150.2
      Durable Task Plugin 1.28

      When you execute a shell step, Jenkins runs a wrapper shell process that's responsible for saving the exit code of your script. If this process is killed, then Jenkins never discovers that your script has terminated, and the step hangs forever.

      This can be easily demonstrated by killing the wrapper process yourself:

       node() {
            sh 'kill $PPID'
       }
      

      Run this pipeline and the sh step will never terminate.

      I believe this happens due to a bug in the log-touching subshell spawned by the wrapper process. Specifically, the subshell checks whether the wrapper process is still alive with this expression:

       \( -d /proc/$pid -o \! -d /proc/$$ \)
      

      But $$ expands to the PID of the parent shell, not the subshell. This means that $pid and $$ are the same thing, and the whole expression always evaluates to true.

          [JENKINS-55867] sh step termination is never detected if the wrapper process is killed

          Daniel Zabel added a comment -

          example code to visualize the issue:

           

          pid=$$; { echo "$pid - $$"; }

          outputs:

          11953 - 11953
          

          Daniel Zabel added a comment - example code to visualize the issue:   pid=$$; { echo "$pid - $$" ; } outputs: 11953 - 11953

          Daniel Zabel added a comment -

          seems to be the root cause for

          Daniel Zabel added a comment - seems to be the root cause for

          Daniel Zabel added a comment -

          I guess what you want should something lokk like this:

          pid=$$; ( echo "$pid - $PPID"; )
          

          outputs:

          19499 - 11953
          

          where 19499 is the pid of the started sh and 11953 is the pid of the parent process (in this case the wrapper i guess)

          Daniel Zabel added a comment - I guess what you want should something lokk like this: pid=$$; ( echo "$pid - $PPID" ; ) outputs: 19499 - 11953 where 19499 is the pid of the started sh and 11953 is the pid of the parent process (in this case the wrapper i guess)

          Killing the process group (kill with the negative pid) would also be catastrophic wouldn't it?

          Martin d'Anjou added a comment - Killing the process group (kill with the negative pid) would also be catastrophic wouldn't it?

            Unassigned Unassigned
            rdonchen_intel Roman Donchenko
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: