Skip to content

Regression: WeakReference/WeakMap — referent not cleared on unset (Zend/zend_weakrefs.c) #14103

Closed

Description

@PurHur

Repro

After dropping the last strong reference to a weak referent ($o = null), Zend clears weak refs immediately without gc_collect_cycles().

<?php

declare(strict_types=1);

$o = new stdClass();
$wr = WeakReference::create($o);
$o = null;
var_export($wr->get()); // Zend: NULL

$k = new stdClass();
$wm = new WeakMap();
$wm[$k] = 42;
$k = null;
var_export(count($wm)); // Zend: 0

Maintainer repros:

  • test/repro/maintainer_gap_weakreference_gc.php
  • test/repro/maintainer_gap_weakmap_gc.php
./script/docker-exec.sh -- bash -lc 'php test/repro/maintainer_gap_weakreference_gc.php'
./script/docker-exec.sh -- bash -lc 'php bin/vm.php test/repro/maintainer_gap_weakreference_gc.php'
./script/docker-exec.sh -- bash -lc 'php test/repro/maintainer_gap_weakmap_gc.php'
./script/docker-exec.sh -- bash -lc 'php bin/vm.php test/repro/maintainer_gap_weakmap_gc.php'

Expected (Zend/php-src)

  • WeakReference::get() returns null once the referent is released.
  • WeakMap drops entries when the key object is released; count() is 0.

Actual (php-compiler VM)

  • WeakReference::get() still returns the collected object.
  • WeakMap::count() stays 1 after key unset.

php-src reference

  • Zend/zend_weakrefs.c — weak ref invalidation on object destruction / refcount drop
  • ext/standard/weakrefs.stub.php

PHP implementation target

  • lib/VM/WeakRefRegistry.php — ensure clearForObject() runs when a variable holding the referent is unset (not only on full GC sweep).
  • lib/VM/WeakRefSupport.php, lib/VM/Builtin/WeakReferenceGet.php, lib/VM/Builtin/WeakMapCount.php
  • Wire unset path in lib/VM.php / lib/VM/Context.php (already calls WeakRefRegistry::clearForObject in some paths — verify refcount-drop coverage).

Done when (php-src-strict)

  • Both maintainer repros green on VM.
  • Compliance guard for unset-clears-weakref + weakmap entry removal.

Activity

  1. added
    enhancementNew feature or request
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 30, 2026
  2. PurHur commented on Jun 30, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Runtime → PHP — batch (issues #14103, #13810)

  3. PurHur commented on Jun 30, 2026

    @PurHur
    OwnerAuthor

    Merged via #14115.

    Fix: WeakMap __weak_map_keys no longer holds strong refs; hasStrongScopeBinding ignores compiler temps; JUMPIF/!== release cond temps.

    Verification: WeakReferenceWeakMapTest 19/19; maintainer_gap_weakmap_gc + probe_weakreference green; bootstrap-selfhost-vm-driver-execute-probe OK.

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:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions