Skip to content

feat(openapi): implement ResetInterface in OpenApiFactory#7922

Open
KevinMartinsDev wants to merge 2 commits intoapi-platform:mainfrom
KevinMartinsDev:feat/reset-interface-openapi-factory
Open

feat(openapi): implement ResetInterface in OpenApiFactory#7922
KevinMartinsDev wants to merge 2 commits intoapi-platform:mainfrom
KevinMartinsDev:feat/reset-interface-openapi-factory

Conversation

@KevinMartinsDev
Copy link
Copy Markdown

@KevinMartinsDev KevinMartinsDev commented Apr 19, 2026

Description

This PR is the fourth part of the worker mode compatibility audit (see #7918). It implements ResetInterface in OpenApiFactory to handle internal state persistence.

The Issue

OpenApiFactory is a singleton service that maintains two types of internal state:

  1. Route Collection Cache: Once the router is accessed, the RouteCollection is stored in a private property.
  2. Local Error Resource Cache: A private array ($localErrorResourceCache) that stores resolved error resources to avoid redundant processing.

In persistent memory environments like FrankenPHP:

  • Stale Data: If routes are modified or dynamically updated, the factory might keep using the old cached RouteCollection until the worker restarts.
  • Memory Growth: The error resource cache grows with every unique error encountered, leading to an unbounded increase in RAM usage over time.

The Solution

By implementing Symfony\Contracts\Service\ResetInterface, we ensure that:

  • $routeCollection is reset to null.
  • $localErrorResourceCache is cleared (reset to []).

This happens automatically after each request in worker mode, ensuring that the next request starts with fresh metadata and a clean memory footprint.

@dunglas
Copy link
Copy Markdown
Member

dunglas commented Apr 20, 2026

Hi, thanks for your work on this topic! This is much appreciated and the linter already proved very useful.

However, I wonder if there is a real problem here. Routes and the OpenAPI spec cannot/shouldn't change between requests and users, so the current implement will be more performant. This matters because it's an endpoint that is called a lot of times in some scenarios (client dynamically calling the OpenAPI route).

@KevinMartinsDev
Copy link
Copy Markdown
Author

Thank you for your feedback! 😃 I completely understand the performance concern regarding the RouteCollection. Maintaining a fast response time for the OpenAPI endpoint is indeed important.

However, I see two main reasons to consider a change here:

Besides the routes, OpenApiFactory also maintains a $localErrorResourceCache. This array grows every time a unique error is encountered. In a long-running worker process, this leads to an unbounded memory leak. Even if we decide to keep the routes, we should at least clear this error cache.

Would you like me to update this PR to follow one of these paths?

Option A: Keep the ResetInterface but only reset the $localErrorResourceCache, leaving the routeCollection as is for performance.

Option B: Transition the internal caching to a proper PSR-16 (Simple Cache) implementation. This would provide the same performance benefits while keeping the service stateless and making the cache shared/clearable via standard tools.

Also, relying on private properties means the cache is isolated per worker. In a multi-worker setup (like FrankenPHP), this leads to redundant memory usage and potential state drift between processes. Using a PSR-16 implementation (e.g., via APCu) would allow workers to share the cache, significantly reducing the total memory footprint and ensuring consistency across the pool.

What do you think? 🤔

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.

2 participants