Skip to content

Fix reuse of dtor fiber during shutdown #16026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
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
70 changes: 70 additions & 0 deletions Zend/tests/fibers/destructors_011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--TEST--
Fibers in destructors 011: gc collection after the dtor fiber is dtor
--FILE--
<?php

class SimpleCycle {
public $self;
public function __construct() {
$this->self = $this;
}
public function __destruct() {
printf("%s\n", __METHOD__);
}
}

class CreateGarbageInDtor {
public $self;
public function __construct() {
$this->self = $this;
}
public function __destruct() {
printf("%s\n", __METHOD__);
// Create an object whose dtor will be called after the dtor fiber's
new CollectCyclesInFiberInDtor();
}
}

class CollectCyclesInFiberInDtor {
public $self;
public function __construct() {
$this->self = $this;
}
public function __destruct() {
printf("%s\n", __METHOD__);
new SimpleCycle();
print "Collecting cycles\n";
$f = new Fiber(function () {
gc_collect_cycles();
});
$f->start();
print "Done collecting cycles\n";
}
}

register_shutdown_function(function () {
print "Shutdown\n";
});

// Create a cycle
new SimpleCycle();

// Collect cycles to create the dtor fiber
$f = new Fiber(function () {
gc_collect_cycles();
});
$f->start();

// Create an object whose dtor will be called during shutdown
// (by zend_objects_store_call_destructors)
new CreateGarbageInDtor();

?>
--EXPECT--
SimpleCycle::__destruct
Shutdown
CreateGarbageInDtor::__destruct
CollectCyclesInFiberInDtor::__destruct
Collecting cycles
SimpleCycle::__destruct
Done collecting cycles
3 changes: 3 additions & 0 deletions Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,9 @@ static ZEND_FUNCTION(gc_destructor_fiber)

if (UNEXPECTED(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)) {
/* Fiber is being destroyed by shutdown sequence */
if (GC_G(dtor_fiber) == fiber) {
GC_G(dtor_fiber) = NULL;
}
GC_DELREF(&fiber->std);
gc_check_possible_root((zend_refcounted*)&fiber->std.gc);
return;
Expand Down
Loading