php / php/php-src

Non-suspended generators in suspended Fiber do not participate in GC

Open
#10,134 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Engine Category: Fibers Category: GC Category: Generators Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

Found this while checking if https://github.com/php/php-src/issues/9916 also happened during GC.

The following code:

<?php

class Canary {
    public function __construct(
        public readonly string $x,
    ) {
    }

    public function __destruct() {
        var_dump($this->x);
    }
}

$gen = (function() {
    $canary = new Canary('Generator dtor');
    $fiber = yield;
    Fiber::suspend();
})();

$fiber = new Fiber(function() use ($gen, &$fiber) {
    $canary = new Canary('Fiber dtor');
    $gen->send($fiber);
});
$fiber->start();

$gen = null;
$fiber = null;
gc_collect_cycles();

var_dump('Shutdown');

Resulted in this output:

string(8) "Shutdown"
string(14) "Generator dtor"
string(10) "Fiber dtor"

But I expected this output instead:

string(14) "Generator dtor"
string(10) "Fiber dtor"
string(8) "Shutdown"
PHP Version

PHP 8.1

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided PHP 8.1 reproducer and trace the Fiber and Generator lifecycle during gc_collect_cycles(). Investigate how non-suspended generators held by a suspended Fiber participate in garbage collection; done means both destructors run before the "Shutdown" output.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.