4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\ExecutionLoop;
18 * A forking version of the Psy Shell execution loop.
20 * This version is preferred, as it won't die prematurely if user input includes
21 * a fatal error, such as redeclaring a class or function.
23 class ForkingLoop extends Loop
28 * Run the execution loop.
30 * Forks into a master and a loop process. The loop process will handle the
31 * evaluation of all instructions, then return its state via a socket upon
36 public function run(Shell $shell)
38 list($up, $down) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
41 throw new \RuntimeException('Unable to create socket pair.');
46 throw new \RuntimeException('Unable to start execution loop.');
48 // This is the main thread. We'll just wait for a while.
50 // We won't be needing this one.
53 // Wait for a return value from the loop process.
57 if (stream_select($read, $write, $except, null) === false) {
58 throw new \RuntimeException('Error waiting for execution loop.');
61 $content = stream_get_contents($down);
65 $shell->setScopeVariables(@unserialize($content));
71 // This is the child process. It's going to do all the work.
72 if (function_exists('setproctitle')) {
73 setproctitle('psysh (loop)');
76 // We won't be needing this one.
79 // Let's do some processing.
82 // Send the scope variables back up to the main thread
83 fwrite($up, $this->serializeReturn($shell->getScopeVariables(false)));
86 posix_kill(posix_getpid(), SIGKILL);
90 * Create a savegame at the start of each loop iteration.
92 public function beforeLoop()
94 $this->createSavegame();
98 * Clean up old savegames at the end of each loop iteration.
100 public function afterLoop()
102 // if there's an old savegame hanging around, let's kill it.
103 if (isset($this->savegame)) {
104 posix_kill($this->savegame, SIGKILL);
105 pcntl_signal_dispatch();
110 * Create a savegame fork.
112 * The savegame contains the current execution state, and can be resumed in
113 * the event that the worker dies unexpectedly (for example, by encountering
114 * a PHP fatal error).
116 private function createSavegame()
118 // the current process will become the savegame
119 $this->savegame = posix_getpid();
123 throw new \RuntimeException('Unable to create savegame fork.');
124 } elseif ($pid > 0) {
125 // we're the savegame now... let's wait and see what happens
126 pcntl_waitpid($pid, $status);
128 // worker exited cleanly, let's bail
129 if (!pcntl_wexitstatus($status)) {
130 posix_kill(posix_getpid(), SIGKILL);
133 // worker didn't exit cleanly, we'll need to have another go
134 $this->createSavegame();
139 * Serialize all serializable return values.
141 * A naïve serialization will run into issues if there is a Closure or
142 * SimpleXMLElement (among other things) in scope when exiting the execution
143 * loop. We'll just ignore these unserializable classes, and serialize what
146 * @param array $return
150 private function serializeReturn(array $return)
152 $serializable = array();
154 foreach ($return as $key => $value) {
155 // No need to return magic variables
156 if (Context::isSpecialVariableName($key)) {
160 // Resources and Closures don't error, but they don't serialize well either.
161 if (is_resource($value) || $value instanceof \Closure) {
167 $serializable[$key] = $value;
168 } catch (\Exception $e) {
169 // we'll just ignore this one...
170 } catch (\Throwable $e) {
171 // and this one too...
175 return @serialize($serializable);