dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.88k stars 235 forks source link

Strange issues with file-descriptors #974

Closed withinboredom closed 2 months ago

withinboredom commented 2 months ago

What happened?

This might be a PHP issue, but I'd expect the following to work:

fopen("php://fd/$fd", "rb"); // PHP Warning:  fopen(php://fd/9): Failed to open stream: operation failed
fopen("/proc/$pid/fd/$fd", "rb"); // PHP Warning:  fopen(/proc/97337/fd/12): Failed to open stream: No such file or directory
var_dump(scandir("/proc/$pid/fd"));

// outputs:

array(12) {
  [0] =>
  string(1) "."
  [1] =>
  string(2) ".."
  [2] =>
  string(1) "0"
  [3] =>
  string(1) "1"
  [4] =>
  string(1) "2"
  [5] =>
  string(1) "3"
  [6] =>
  string(1) "4"
  [7] =>
  string(1) "5"
  [8] =>
  string(1) "6"
  [9] =>
  string(1) "7"
  [10] =>
  string(1) "8"
  [11] =>
  string(1) "9"
}

For some reason, I cannot open file-descriptors from other threads (as you'd expect to be able to do in C/go). Still digging into the underlying cause.

Use-case: experimenting with frankenphp for running "workers" and having a means of communicating between them.

Build Type

Docker (Debian Bookworm)

Worker Mode

No

Operating System

GNU/Linux

CPU Architecture

x86_64

PHP configuration

N/A currently

Relevant log output

No response

withinboredom commented 2 months ago

Ah, only the "CLI" SAPI is allowed to access FD's, and it is hardcoded that way.

if (strcmp(sapi_module.name, "cli")) {
            if (options & REPORT_ERRORS) {
                php_error_docref(NULL, E_WARNING, "Direct access to file descriptors is only available from command-line PHP");
            }
            return NULL;
        }

Strangely, that error message doesn't make it to the user.

Anyway, that is unfortunate. I'll look into adding support to the SAPI handlers for it though. It would be quite handy to share pipes between different PHP threads (sending jobs, running async code, etc).

withinboredom commented 2 months ago

And https://github.com/php/php-src/issues/9551 closing this...