Skip to content

[Runtime] Automatically use FrankenPHP runner when its worker mode is detected #60503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented May 21, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Currently, to use FrankenPHP worker mode, you have to install the runtime/frankenphp-symfony package and set the APP_RUNTIME env var to the correct FQCN.

@dunglas and I would like to move the runtime and the runner classes to the Symfony core now that The PHP Foundation officially supports FrakenPHP.

This PR adds FrankenPHP worker mode detection and registers the correct runner to use it. This specific runner is only used when using the worker mode. When using FrankenPHP in classic mode, HttpKernelRunner is still used.

The DX would be greatly improved, given that it works out of the box with this PR.

Classes and tests are coming from this repo with a few tweaks to stick to Symfony CS.

@carsonbot carsonbot added this to the 7.3 milestone May 21, 2025
@alexandre-daubois alexandre-daubois added the DX DX = Developer eXperience (anything that improves the experience of using Symfony) label May 21, 2025
@alexandre-daubois alexandre-daubois changed the title [Runtime] Automatically enable FrankenPHP runtime when its worker mode is detected [Runtime] Automatically use FrankenPHP runner when its worker mode is detected May 21, 2025
Comment on lines 42 to 44
if (\extension_loaded('xdebug') && \function_exists('xdebug_connect_to_client')) {
xdebug_connect_to_client();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, this feature is currently broken in Xdebug. We're investigating what's going on. It doesn't hurt to keep this code anyway.

@@ -143,12 +147,23 @@ public function __construct(array $options = [])

$options['error_handler'] ??= SymfonyErrorHandler::class;

$workerLoopMax = $options['worker_loop_max'] ?? $_SERVER['FRANKENPHP_LOOP_MAX'] ?? $_ENV['FRANKENPHP_LOOP_MAX'] ?? null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the env var has the same name as the option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept as is to keep the backward compatibility with the current runtime package, but maybe it's the occasion to have a better name indeed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd say we can keep FRANKENPHP_LOOP_MAP to help transitioning.
We also have the $_SERVER['APP_RUNTIME_OPTIONS'] vars which allows configuring any options.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that require a modification in FrankenPHP itself?

Copy link
Member Author

@alexandre-daubois alexandre-daubois May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it requires an update, we could make FrankenPHP compatible with both FRANKENPHP_LOOP_MAX and (something like) WORKER_LOOP_MAX, then deprecated the first one day when we're ready

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to modify FrankenPHP, this option is entirely handled by the runtime.

@@ -143,12 +147,23 @@ public function __construct(array $options = [])

$options['error_handler'] ??= SymfonyErrorHandler::class;

$workerLoopMax = $options['worker_loop_max'] ?? $_SERVER['FRANKENPHP_LOOP_MAX'] ?? $_ENV['FRANKENPHP_LOOP_MAX'] ?? null;
if (null !== $workerLoopMax && (!\is_numeric($workerLoopMax) || $workerLoopMax <= 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter_var?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this (for example filter_var($options['worker_loop_max'] ?? $_SERVER['FRANKENPHP_LOOP_MAX'] ?? $_ENV['FRANKENPHP_LOOP_MAX'] ?? null, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);), we cannot detect if the option is a string like "foo", and it'll fallback on the default value. It works, but there will be no explicit error message to warn the user. Maybe I'm missing an argument to provide to filter_var?

parent::__construct($options);
}

public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof HttpKernelInterface) {
if (filter_var($_SERVER['FRANKENPHP_WORKER'] ?? $_ENV['FRANKENPHP_WORKER'] ?? null, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false) {
Copy link
Member

@dunglas dunglas May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to check for $_ENV or use filter_var(). IIRC $_SERVER is always populated by FrankenPHP in worker mode, and the other SAPIs don't have any reason to use the FRANKENPHP_ prefix.

Copy link
Member

@nicolas-grekas nicolas-grekas May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is not an env var, but something the sapi exposes? then indeed let's revert to the previous code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

parent::__construct($options);
}

public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof HttpKernelInterface) {
if (filter_var($_SERVER['FRANKENPHP_WORKER'] ?? $_ENV['FRANKENPHP_WORKER'] ?? null, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false) {
Copy link
Member

@nicolas-grekas nicolas-grekas May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is not an env var, but something the sapi exposes? then indeed let's revert to the previous code

@@ -143,12 +147,23 @@ public function __construct(array $options = [])

$options['error_handler'] ??= SymfonyErrorHandler::class;

$workerLoopMax = $options['worker_loop_max'] ?? $_SERVER['FRANKENPHP_LOOP_MAX'] ?? $_ENV['FRANKENPHP_LOOP_MAX'] ?? null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd say we can keep FRANKENPHP_LOOP_MAP to help transitioning.
We also have the $_SERVER['APP_RUNTIME_OPTIONS'] vars which allows configuring any options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature Runtime Status: Needs Work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants