8000 Make sure there are two copies of the callable around because of http… · reactphp/event-loop@3f5457f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f5457f

Browse files
committed
Make sure there are two copies of the callable around because of https://bugs.php.net/bug.php?id=62452
1 parent ab6c584 commit 3f5457f

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/ExtEventLoop.php

Lines changed: 10 additions & 7 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,21 @@ public function __construct(EventBaseConfig $config = null)
3636

3737
$this->signals = new SignalsHandler(
3838
function ($signal) {
39-
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, function () use ($signal) {
39+
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, $f = function () use ($signal, &$f) {
4040
$this->signals->call($signal);
41+
// Ensure there are two copies of the callable around until it has been executed.
42+
// For more information see: https://bugs.php.net/bug.php?id=62452
43+
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
44+
$g = $f;
45+
$f = $g;
4146
});
4247
$this->signalEvents[$signal]->add();
4348
},
4449
function ($signal) {
45-
$this->futureTick(function () use ($signal) {
46-
if ($this->signals->count($signal) === 0) {
47-
$this->signalEvents[$signal]->del();
48-
unset($this->signalEvents[$signal]);
49-
}
50-
});
50+
if ($this->signals->count($signal) === 0) {
51+
$this->signalEvents[$signal]->del();
52+
unset($this->signalEvents[$signal]);
53+
}
5154
}
5255
);
5356

src/LibEvLoop.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ public function __construct()
3434

3535
$this->signals = new SignalsHandler(
3636
function ($signal) {
37-
$this->signalEvents[$signal] = new SignalEvent(function () use ($signal) {
37+
$this->signalEvents[$signal] = new SignalEvent($f = function () use ($signal, &$f) {
3838
$this->signals->call($signal);
39+
// Ensure there are two copies of the callable around until it has been executed.
40+
// For more information see: https://bugs.php.net/bug.php?id=62452
41+
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
42+
$g = $f;
43+
$f = $g;
3944
}, $signal);
4045
$this->loop->add($this->signalEvents[$signal]);
4146
},
4247
function ($signal) {
43-
$this->futureTick(function () use ($signal) {
44-
if ($this->signals->count($signal) === 0) {
45-
$this->loop->remove($this->signalEvents[$signal]);
46-
unset($this->signalEvents[$signal]);
47-
}
48-
});
48+
if ($this->signals->count($signal) === 0) {
49+
$this->loop->remove($this->signalEvents[$signal]);
50+
unset($this->signalEvents[$signal]);
51+
}
4952
}
5053
);
5154
}

src/LibEventLoop.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,23 @@ public function __construct()
3939
$this->signals = new SignalsHandler(
4040
function ($signal) {
4141
$this->signalEvents[$signal] = event_new();
42-
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, function () use ($signal) {
42+
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, $f = function () use ($signal, &$f) {
4343
$this->signals->call($signal);
44+
// Ensure there are two copies of the callable around until it has been executed.
45+
// For more information see: https://bugs.php.net/bug.php?id=62452
46+
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
47+
$g = $f;
48+
$f = $g;
4449
});
4550
event_base_set($this->signalEvents[$signal], $this->eventBase);
4651
event_add($this->signalEvents[$signal]);
4752
},
4853
function ($signal) {
49-
$this->futureTick(function () use ($signal) {
50-
if ($this->signals->count($signal) === 0) {
51-
event_del($this->signalEvents[$signal]);
52-
event_free($this->signalEvents[$signal]);
53-
unset($this->signalEvents[$signal]);
54-
}
55-
});
54+
if ($this->signals->count($signal) === 0) {
55+
event_del($this->signalEvents[$signal]);
56+
event_free($this->signalEvents[$signal]);
57+
unset($this->signalEvents[$signal]);
58+
}
5659
}
5760
);
5861

src/StreamSelectLoop.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ public function __construct()
3232
$this->pcntl = extension_loaded('pcntl');
3333
$this->signals = new SignalsHandler(
3434
function ($signal) {
35-
\pcntl_signal($signal, function ($signal) {
35+
\pcntl_signal($signal, $f = function ($signal) use (&$f) {
3636
$this->signals->call($signal);
37+
// Ensure there are two copies of the callable around until it has been executed.
38+
// For more information see: https://bugs.php.net/bug.php?id=62452
39+
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
40+
$g = $f;
41+
$f = $g;
3742
});
3843
},
3944
function ($signal) {
40-
$this->futureTick(function () use ($signal) {
41-
if ($this->signals->count($signal) === 0) {
42-
\pcntl_signal($signal, SIG_DFL);
43-
}
44-
});
45+
if ($this->signals->count($signal) === 0) {
46+
\pcntl_signal($signal, SIG_DFL);
47+
}
4548
}
4649
);
4750
}

0 commit comments

Comments
 (0)
0