puppetlabs/marionette-collective — pull request #83 by databus23 (2013-05-26 21:06:36+00:00)
Title: run() call in an agent can return incorrect Process::Status

A child process can only be reaped once. Calling waitpid(cid) in a separate thread creates a race condition between the "main" systememu thread and the "guardian" thread.
In case the guardian thread reaps the child process, the $? variable is not properly updated in the "main" thread and the wrong exist status (of the previous reaped child process) is returned by systemu.

By calling waitpid non-blocking in a loop we make sure the child process is always reaped by the main thread unless it was killed before the child finished.