Skip to content

Stdlib: stream_context_create() — VM fatal "Refcount is > 1" instead of stream-context resource (ext/standard/streams.c) #4738

Closed

Description

@PurHur

Category

stdlib

Problem

Calling stream_context_create(['http' => ['timeout' => 5]]) under the VM aborts with Refcount is > 1, but was asserted to be 1 instead of returning a stream-context resource like Zend. This blocks HTTP/stream wrappers with custom context (bootstrap probes, file_get_contents with context).

Distinct from #4627 (TypeError for non-array options) — operand type is valid but runtime crashes.

php-src reference

  • php/php-src ext/standard/streams.c — php_stream_context_create(), php_stream_context_alloc()
  • Returns php_stream_context wrapped as stream-context resource (zend_list entry)

Repro (today)

Save as repro_stream_context_create.php:

<?php
$ctx = stream_context_create(['http' => ['timeout' => 5]]);
var_export(is_resource($ctx));
echo "\n";
var_export(get_resource_type($ctx));
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro_stream_context_create.php
php bin/vm.php repro_stream_context_create.php
'
Runtime Result
Zend true, 'stream-context'
VM (2026-06-03) Fatal: Refcount is > 1, but was asserted to be 1

Scope (this repo)

Area Files Notes
VM builtin ext/standard/stream_context_create.php Today returns array via VmStreamContext::create() — align with Zend resource model
Stream layer ext/standard/VmStreamContext.php, VmFs.php Fix refcount when nesting options arrays (likely copy-by-ref from VmHttpBuildQuery::export)
Registration ext/standard/Module.php
Tests test/compliance/cases/stdlib/stream_context_create.phpt (extend existing JIT cases)

Investigation hint: trace VmStreamContext::create($options, $params) — ensure nested wrapper arrays are copied, not aliased refcount>1 Variable graphs passed into resource boxing.

Done when

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter stream_context_create'

Links

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 3, 2026
  2. PurHur commented on Jun 15, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Runtime → PHP — starting this run (#4738: stream_context_create nested options array + resource return).

  3. PurHur commented on Jun 15, 2026

    @PurHur
    OwnerAuthor

    Merged via #8646.

    Fix: lib/Compiler.php matchInlineCallArgProducer() — map nested Expr_Array producer chains to the correct inline literal call operand (php-cfg emits one producer per nesting level; disconnected temps broke stream_context_create(['http'=>['timeout'=>5]])).

    Verification:

    ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && cd /compiler
    php bin/vm.php test/repro-maintainer/repro_stream_context_inline.php
    vendor/bin/phpunit test/compliance/VMTest.php --filter stream_context_create_inline_nested
    vendor/bin/phpunit test/compliance/VMTest.php --filter stream_context  # 5/7 pass (was 1/7)
    '

    Follow-up (separate): stream_context_set_options/set_params PHPTs still red when callee is nested inside var_export(...) (inner FUNCCALL return slot); assign-form $ok = stream_context_set_options(...) works on branch.

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 apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions