Skip to content

Assertion failure in Zend/zend_weakrefs.c:646 #16371

Closed

Description

@YuanchengJiang

Description

The following code:

<?php
$map = new WeakMap();
$fusion = $map;
$obj = new stdClass;
$ao = new ArrayObject($obj);
$i = $fusion->getIterator();
$ao->offsetUnset($i->key());

Resulted in this output:

/php-src/Zend/zend_weakrefs.c:646: void zend_weakmap_iterator_get_current_key(zend_object_iterator *, zval *): Assertion `0 && "Must have integer key"' failed.
Aborted (core dumped)

PHP Version

nightly

Operating System

ubuntu 22.04

Activity

  1. ndossche commented on Oct 11, 2024

    @ndossche
    Member

    See also #15775 (comment)
    IMO We should just yeet this class ASAP

  2. cmb69 commented on Oct 11, 2024

    @cmb69
    Member

    See php/doc-en#3858 (a humble first step).

  3. arnaud-lb commented on Oct 14, 2024

    @arnaud-lb
    Member

    Is the class also problematic when backed by arrays? (new ArrayObject([]))

    I've seen it used as pass-by-ref arrays. Until now I was convinced this was the primary use-case, and that objects support had been added because "why not". Looking at the history, it was added in 173cb1436fb5 ("Add class spl_array which is an array wrapper"). It was originally named spl_array, but later renamed ArrayClass, and ultimately ArrayObject.

    If that part is relatively sane, can we just discourage the use of objects as backing store?

  4. cmb69 commented on Oct 14, 2024

    @cmb69
    Member

    Good point @arnaud-lb! I guess you're right, and there are no general issues when wrapping arrays (and also about the history of the class, and especially the "why not" comment). Thus, I've just submitted php/doc-en#3867.

  5. added a commit that references this issue on Oct 15, 2024
    1d94fb8
  6. HeenaBansal20 commented on Jul 29, 2025

    @HeenaBansal20

    @arnaud-lb , I am facing the similar kind of assertion failure here
    php: /usr/src/php/Zend/zend_weakrefs.c:547: zend_weakmap_get_object_key_entry_gc: Assertion `zv' failed in php 8.5 alpha2.

    DO we need to fix the issue here as well and validate zv before zend_assert ?

  7. arnaud-lb commented on Jul 30, 2025

    @arnaud-lb
    Member

    @HeenaBansal20 are you able to provide a small reproducer code snippet?

  8. HeenaBansal20 commented on Jul 30, 2025

    @HeenaBansal20

    @arnaud-lb ,I am finding a hard time to find a reproducer for this case. However this is only getting generated for my extension in PHP 8.5 alpha2.

    Basically , The assertion ZEND_ASSERT(zv) is failing, which means zv is NULL. This happens when the function zend_hash_index_find() doesn't find the key obj_key in the hash table wm->ht.

    and I am assuming this is likely happening because during garbage collection, PHP is trying to clean up weak references, but it's encountering an object that has been removed from the weak reference map

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions