Skip to content

Commit

Permalink
Fix stranded job when we shutdown mid-fork
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed Sep 9, 2021
1 parent 4eb6a88 commit 46dc161
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/Resque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class Resque_Worker
*/
private $child = null;

/**
* @var boolean True if this worker is in the middle of creating a fork of a child
*/
private $isForking = false;
/**
* @var boolean True if this worker received a kill signal mid fork
*/
private $killForkAfterSetup = false;

/**
* Instantiate a new worker, given a list of queues that it should be working
* on. The list of queues should be supplied in the priority that they should
Expand Down Expand Up @@ -200,7 +209,14 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
Resque_Event::trigger('beforeFork', $job);
$this->workingOn($job);

$this->isForking = true;
$this->child = Resque::fork();
$this->isForking = false;
// Received sigint mid-fork
if($this->killForkAfterSetup) {
$this->killChild();
return;
}

// Forked and we're the child. Run the job.
if ($this->child === 0 || $this->child === false) {
Expand Down Expand Up @@ -456,6 +472,13 @@ public function shutdownNow($signal = null)
*/
public function killChild($attempt = 1)
{
if($this->isForking && !$this->child) {
$this->logger->log(Psr\Log\LogLevel::WARNING, 'No child due to being in the middle of forking for {worker}, exiting until fork is done.', [
'worker' => $this
]);
$this->killForkAfterSetup = true;
return;
}
if(!$this->child) {
$this->logger->log(Psr\Log\LogLevel::DEBUG, 'No child to kill for {worker}', Array(
'worker' => $this
Expand Down

0 comments on commit 46dc161

Please sign in to comment.