Skip to content

Stdlib: pfsockopen() — persistent TCP socket streams (ext/standard/fsock.c parity) #3384

Description

@PurHur

Category

stdlib | runtime

Status (expanded 2026-06-07 — implementation-ready)

Maintainer probe on master (Docker php-compiler:22.04-dev):

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php -r "var_dump(function_exists(\"pfsockopen\"), function_exists(\"fsockopen\"), function_exists(\"stream_socket_client\"));"'
# bool(false) bool(false) bool(true)  — persistent opener missing; non-persistent fsockopen also absent

Workers should implement pfsockopen() on top of the existing stream_socket_client() / socket stream layer rather than new C in runtime/.

Problem

pfsockopen(string $hostname, int $port, …): resource|false opens a persistent socket stream reused across requests (CGI/FPM) or long-running scripts. Non-persistent fsockopen() is tracked in #3202; pfsockopen() is a separate php-src entry point with persistent handle caching in the stream layer.

function_exists('pfsockopen') is false under bin/vm.php.

php-src reference

Repro (failure today)

<?php
$errno = 0;
$errstr = '';
$fp = @pfsockopen('127.0.0.1', 9, $errno, $errstr, 1);
var_export(is_resource($fp));
echo "\n";
if (is_resource($fp)) {
    fclose($fp);
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php -r "var_dump(function_exists(\"pfsockopen\"));"
php bin/vm.php repro.php
'
Runtime Result
Zend function_exists → true; stream resource or false + populated $errno/$errstr
This compiler VM undefined function fatal

Scope (this repo)

Module Path Notes
Persistent registry lib/VM/StreamRegistry.php or extend stream layer process-lifetime map host:port → handle
Builtin ext/standard/pfsockopen.php delegate connect to shared socket helper
Shared TCP reuse #3202 / stream_socket_client() internals do not fork separate connect code
Registration ext/standard/Module.php
JIT/AOT VM-first v1; document deferral in matrix
Tests test/compliance/cases/stdlib/pfsockopen.phpt discard port 9 or loopback mock
php-in-php keep stream tables in PHP (#5343 stream registry migration) no phpc_stream.c growth

Done when

  • function_exists('pfsockopen') true on VM
  • Repro returns stream resource or false with $errno/$errstr like Zend
  • Second call with same host/port reuses handle until fclose() (Zend subset)
  • ./script/ci-fast.sh --filter pfsockopen green
  • docs/capabilities.md row added

Verification

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

Dependencies

Activity

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