diff --git a/ext/standard/io_poll.c b/ext/standard/io_poll.c index 583dfdbc0335..feec04e8158c 100644 --- a/ext/standard/io_poll.c +++ b/ext/standard/io_poll.c @@ -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(); + zend_hash_destroy(watchers); pefree(watchers, stream->is_persistent); } @@ -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; } diff --git a/ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phpt b/ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phpt new file mode 100644 index 000000000000..d859679f0969 --- /dev/null +++ b/ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phpt @@ -0,0 +1,79 @@ +--TEST-- +Io\Poll: closing a watched stream or modifying watcher data with re-entrant destructor does not crash +--FILE-- +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