Skip to content

Fix reference ownership when deleting hash values - #56

Open
aspeer wants to merge 1 commit into
6over3:mainfrom
aspeer:codex/fix-hash-delete-refcount
Open

aspeer wants to merge 1 commit into
6over3:mainfrom
aspeer:codex/fix-hash-delete-refcount

Conversation

@aspeer

@aspeer aspeer commented Sep 6, 2026

Copy link
Copy Markdown

I have forked the zeroperl repo to support a WASM build of my WebDyne framework. During that build I saw repeated traps in the Cloudflare WASM provider which were traced to a reference count issue

This PR seeks to fix that issue.

zeroperl_hash_delete decrements the value returned by hv_delete(..., 0),
but Perl has already made that return value mortal. The extra decrement can
release a value still retained by another caller when the temporary scope drains.

Use hv_delete(..., G_DISCARD) so Perl owns deletion cleanup, and check key
existence beforehand to preserve the boolean result, including undef-valued keys.
The exported signature is unchanged.

Adds a native libperl regression using the production function extracted
verbatim. It checks retained values after temporary cleanup, exactly one DESTROY
per object, undef values, missing/repeated deletions and invalid arguments.

Validation on native Perl 5.44.0, macOS arm64:

  • Untouched upstream: the retained-reference assertion fails after FREETMPS.
  • Patched: 100 cycles pass, including retained-value and destructor checks.
  • git diff --check passes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant