|
| 1 | +--TEST-- |
| 2 | +Fibers in destructors 011: gc collection after the dtor fiber is dtor |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class SimpleCycle { |
| 7 | + public $self; |
| 8 | + public function __construct() { |
| 9 | + $this->self = $this; |
| 10 | + } |
| 11 | + public function __destruct() { |
| 12 | + printf("%s\n", __METHOD__); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class CreateGarbageInDtor { |
| 17 | + public $self; |
| 18 | + public function __construct() { |
| 19 | + $this->self = $this; |
| 20 | + } |
| 21 | + public function __destruct() { |
| 22 | + printf("%s\n", __METHOD__); |
| 23 | + // Create an object whose dtor will be called after the dtor fiber's |
| 24 | + new CollectCyclesInFiberInDtor(); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class CollectCyclesInFiberInDtor { |
| 29 | + public $self; |
| 30 | + public function __construct() { |
| 31 | + $this->self = $this; |
| 32 | + } |
| 33 | + public function __destruct() { |
| 34 | + printf("%s\n", __METHOD__); |
| 35 | + new SimpleCycle(); |
| 36 | + print "Collecting cycles\n"; |
| 37 | + $f = new Fiber(function () { |
| 38 | + gc_collect_cycles(); |
| 39 | + }); |
| 40 | + $f->start(); |
| 41 | + print "Done collecting cycles\n"; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +register_shutdown_function(function () { |
| 46 | + print "Shutdown\n"; |
| 47 | +}); |
| 48 | + |
| 49 | +// Create a cycle |
| 50 | +new SimpleCycle(); |
| 51 | + |
| 52 | +// Collect cycles to create the dtor fiber |
| 53 | +$f = new Fiber(function () { |
| 54 | + gc_collect_cycles(); |
| 55 | +}); |
| 56 | +$f->start(); |
| 57 | + |
| 58 | +// Create an object whose dtor will be called during shutdown |
| 59 | +// (by zend_objects_store_call_destructors) |
| 60 | +new CreateGarbageInDtor(); |
| 61 | + |
| 62 | +?> |
| 63 | +--EXPECT-- |
| 64 | +SimpleCycle::__destruct |
| 65 | +Shutdown |
| 66 | +CreateGarbageInDtor::__destruct |
| 67 | +CollectCyclesInFiberInDtor::__destruct |
| 68 | +Collecting cycles |
| 69 | +SimpleCycle::__destruct |
| 70 | +Done collecting cycles |
0 commit comments