diff --git a/lib/Threading/Thread.php b/lib/Threading/Thread.php index dee2c01..e7d7c60 100644 --- a/lib/Threading/Thread.php +++ b/lib/Threading/Thread.php @@ -151,9 +151,13 @@ class Thread implements Strand { $this->channel = new ChannelledSocket($channel, $channel); - $this->watcher = Loop::repeat(self::EXIT_CHECK_FREQUENCY, function () { + $this->watcher = Loop::repeat(self::EXIT_CHECK_FREQUENCY, function ($watcher) { if (!$this->thread->isRunning()) { - $this->channel->close(); + // Delay call to close to avoid race condition between thread exiting and data becoming available. + Loop::delay(self::EXIT_CHECK_FREQUENCY, function () { + $this->close(); + }); + Loop::disable($watcher); } });