Skip to content

php-in-php: phpc_gc.c shutdown destructors — GC cycle collection in lib/VM/Gc.php (Zend/zend_gc.c parity) #6519

Closed

Description

@PurHur

Category

php-in-php | runtime

Problem

Cycle collection and shutdown __destruct ordering still live partly in lib/AOT/runtime/phpc_gc.c (~400+ lines). Zend implements this in Zend/zend_gc.c; this repo already has lib/VM/Gc.php and gc_collect_cycles() in ext/standard/, but AOT/JIT paths call C helpers for:

Self-host (#1492) requires compiled lib/ to run GC semantics without growing runtime/*.c.

php-src reference

Repro (behavior gap today)

<?php
class Node {
    public ?Node $next = null;
    public function __destruct() { echo "dtor\n"; }
}
$a = new Node();
$b = new Node();
$a->next = $b;
$b->next = $a;
unset($a, $b);
gc_collect_cycles();
// Zend: prints dtor (possibly twice depending on cycle handling)
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro_gc_cycle_destruct.php
php bin/vm.php repro_gc_cycle_destruct.php
# Compare output; AOT-linked binary must match VM (no missing dtor)
'

Also audit symbols: nm lib/AOT/runtime/*.o | grep phpc_gc — any call from compiled PHP builtins should route through VM PHP helpers after migration.

Scope (PHP-in-PHP first)

Path Work
lib/VM/Gc.php Port cycle mark/sweep + destructor queue from C
ext/standard/gc_collect_cycles.php Single entry; delete duplicate C paths
lib/AOT/runtime/phpc_gc.c Delete or shrink to thin ABI trampoline
JIT/AOT Lower gc_collect_cycles() to PHP helper or shared runtime symbol
Tests Extend gc_collect_cycles.phpt; add cyclic destructor PHPT

Done when

  • Repro destructor output matches Zend on VM and AOT-linked driver
  • Measurable lines removed from lib/AOT/runtime/phpc_gc.c (target: delete file or <50 line ABI)
  • ./script/ci-fast.sh --filter gc_collect_cycles green
  • php script/bootstrap-inventory.php --check still green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter gc_collect_cycles'
make bootstrap-selfhost-link

Related

Activity

  1. added
    enhancementNew feature or request
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 6, 2026
  2. PurHur commented on Jun 8, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Runtime → PHP — starting this run (#6519: invoke __destruct during gc_collect_cycles for cyclic graphs in CycleCollector, add compliance PHPT).

  3. PurHur commented on Jun 8, 2026

    @PurHur
    OwnerAuthor

    Merged via #7673.

    PHP path: CycleCollector::collect() now calls ObjectLifetime::invokeDestructorBeforeGcRelease() before ObjectRegistry::release() — matches Zend cyclic-graph destructor ordering (zend_gc.c).

    C shrink: phpc_gc.c was already deleted (#5315); no new C added.

    Verification (green):
    ```bash
    ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter "GcCollectCyclesDestructTest|gc_collect_cycles_destruct"'

    OK (2 tests, 2 assertions)

    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITarea:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-0:FoundationPhase 0 – foundation & DevExphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions