Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ext/standard/io_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,22 @@ PHPAPI void php_io_poll_stream_notify_close(php_stream *stream)
HashTable *watchers = stream->poll_watchers;
stream->poll_watchers = NULL;

/* Retire all watchers before releasing them so destructors see them inactive. */
ZEND_HASH_FOREACH_VAL(watchers, zval *zv) {
php_io_poll_watcher_object *watcher = Z_PTR_P(zv);
watcher->stream = NULL;
watcher->closed = true;
GC_ADDREF(&watcher->std);
if (watcher->context) {
php_io_poll_context_retire_watcher(watcher->context, watcher);
}
} ZEND_HASH_FOREACH_END();

ZEND_HASH_FOREACH_VAL(watchers, zval *zv) {
php_io_poll_watcher_object *watcher = Z_PTR_P(zv);
OBJ_RELEASE(&watcher->std);
} ZEND_HASH_FOREACH_END();
Comment on lines +394 to 397

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destructor can still see the later watchers of the same stream as active here (closed is set but they are still registered in the backend). It doesn't crash but it would be cleaner to retire all of them first and do the release in a separate loop, so no user code runs before everything is inactive.


zend_hash_destroy(watchers);
pefree(watchers, stream->is_persistent);
}
Expand Down Expand Up @@ -503,9 +510,11 @@ static zend_result php_io_poll_watcher_modify_data(php_io_poll_watcher_object *w
return FAILURE;
}

/* Update user data */
zval_ptr_dtor(&watcher->data);
/* Update user data safely against re-entrant destructor */
zval old_data;
ZVAL_COPY_VALUE(&old_data, &watcher->data);
ZVAL_COPY(&watcher->data, data);
zval_ptr_dtor(&old_data);

return SUCCESS;
}
Expand Down
79 changes: 79 additions & 0 deletions ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--TEST--
Io\Poll: closing a watched stream or modifying watcher data with re-entrant destructor does not crash
--FILE--
<?php
require_once __DIR__ . '/poll.inc';

// Case 1: destructor of watcher #1 removes watcher #2 on the same closing stream
$pair = pt_new_socket_pair();
$ctx1 = pt_new_stream_poll();
$ctx2 = pt_new_stream_poll();
$h1 = new StreamPollHandle($pair[0]);
$h2 = new StreamPollHandle($pair[0]);
$dtor = new class {
public $w2;
public function __destruct() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you echo from the destructors here so the test shows they really run during fclose()? Otherwise it would still pass if they don't.

echo "case1 dtor\n";
if ($this->w2) {
var_dump($this->w2->isActive());
$this->w2->remove();
}
}
};
$ctx1->add($h1, [Io\Poll\Event::Read], $dtor);
$dtor->w2 = $ctx2->add($h2, [Io\Poll\Event::Read]);
unset($dtor);
fclose($pair[0]);
fclose($pair[1]);
echo "case1 ok\n";

// Case 2: destructor of watcher #1 releases context #2 owning watcher #2
$pair = pt_new_socket_pair();
$ctx1 = pt_new_stream_poll();
$ctx2 = pt_new_stream_poll();
$h1 = new StreamPollHandle($pair[0]);
$h2 = new StreamPollHandle($pair[0]);
$dtor = new class($ctx2) {
public function __construct(public &$ctx2) {}
public function __destruct() {
echo "case2 dtor\n";
$this->ctx2 = null;
}
};
$ctx1->add($h1, [Io\Poll\Event::Read], $dtor);
$ctx2->add($h2, [Io\Poll\Event::Read]);
unset($dtor);
fclose($pair[0]);
fclose($pair[1]);
echo "case2 ok\n";

// Case 3: modifyData() with a re-entrant destructor reading and updating watcher data
$pair = pt_new_socket_pair();
$ctx = pt_new_stream_poll();
$h = new StreamPollHandle($pair[0]);
$dtor = new class {
public $watcher;
public function __destruct() {
echo "case3 dtor: " . var_export($this->watcher->getData(), true) . "\n";
$this->watcher->modifyData('from_dtor');
}
};
$watcher = $ctx->add($h, [Io\Poll\Event::Read], $dtor);
$dtor->watcher = $watcher;
unset($dtor);
$watcher->modifyData('new');
var_dump($watcher->getData());
$watcher->remove();
fclose($pair[0]);
fclose($pair[1]);
echo "case3 ok\n";
?>
--EXPECT--
case1 dtor
bool(false)
case1 ok
case2 dtor
case2 ok
case3 dtor: 'new'
string(9) "from_dtor"
case3 ok
Loading