Skip to content

Conversation

@santysisi
Copy link
Contributor

@santysisi santysisi commented Jan 31, 2026

Q A
Bug fix? yes
New feature? yes
Docs? no
Issues Fix #1531
License MIT

This PR introduces the ResetInterface and reset tags to the tracers and in memory stores. These changes help optimize memory usage by preventing unnecessary memory increases with each request. Additionally, they ensure that erroneous information is no longer displayed in the profiler, improving accuracy and performance monitoring.

@carsonbot carsonbot added AI Bundle Issues & PRs about the AI integration bundle Bug Something isn't working Feature New feature Status: Needs Review labels Jan 31, 2026
@santysisi
Copy link
Contributor Author

santysisi commented Feb 1, 2026

Status: needs work

I do not finish the manual tests yet

@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from 266afd6 to ade5b26 Compare February 1, 2026 04:52
@santysisi santysisi requested a review from Nyholm as a code owner February 1, 2026 04:52
@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from ade5b26 to aa97ee1 Compare February 1, 2026 04:54
@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from aa97ee1 to a3d001b Compare February 1, 2026 05:01
@santysisi santysisi changed the title [AI Bundle] clean tracers memory after each request [AI Bundle][MCP Bundle][Chat][Store] add ResetInterface to prevent memory leaks Feb 1, 2026
@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from a3d001b to 6c7526e Compare February 1, 2026 05:21
@carsonbot carsonbot changed the title [AI Bundle][MCP Bundle][Chat][Store] add ResetInterface to prevent memory leaks [AI Bundle] add ResetInterface to prevent memory leaks Feb 1, 2026
@santysisi
Copy link
Contributor Author

santysisi commented Feb 1, 2026

Thanks for the approval! ❤️
But please don't merge this yet.
I think there's an issue with both stores. they're not cleaning the memory properly. I think is because are proxy objects

I'll continue investigating.

Edit:

I found and pushed a solution, but I'm not sure if it's correct

diff --git a/src/ai-bundle/src/Profiler/TraceableChat.php b/src/ai-bundle/src/Profiler/TraceableChat.php
index 6755669d..df838b31 100644
--- a/src/ai-bundle/src/Profiler/TraceableChat.php
+++ b/src/ai-bundle/src/Profiler/TraceableChat.php
@@ -70,6 +70,9 @@ final class TraceableChat implements ChatInterface, ResetInterface
 
     public function reset(): void
     {
+        if ($this->chat instanceof ResetInterface) {
+            $this->chat->reset();
+        }
         $this->calls = [];
     }
 }
diff --git a/src/ai-bundle/src/Profiler/TraceableMessageStore.php b/src/ai-bundle/src/Profiler/TraceableMessageStore.php
index d8eb416e..077f7ef6 100644
--- a/src/ai-bundle/src/Profiler/TraceableMessageStore.php
+++ b/src/ai-bundle/src/Profiler/TraceableMessageStore.php
@@ -73,6 +73,9 @@ final class TraceableMessageStore implements ManagedStoreInterface, MessageStore
 
     public function reset(): void
     {
+        if ($this->messageStore instanceof ResetInterface) {
+            $this->messageStore->reset();
+        }
         $this->calls = [];
     }
 }

@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from 6c7526e to aa6ac29 Compare February 1, 2026 16:46
@santysisi
Copy link
Contributor Author

santysisi commented Feb 1, 2026

I think another alternative could be to delete the proxies and set lazy to false in these two services:
Symfony\AI\Store\InMemory\Store and Symfony\AI\Chat\InMemory\Store.

WDYT?

Edit:
I think both options are okay, the current one and this other one. I don’t have any other solutions in mind

@OskarStark
Copy link
Contributor

Friendly Ping @Guikingone

@Guikingone
Copy link
Contributor

Hi @santysisi 👋🏻

Good for me to remove lazy on both services 👍🏻

@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch 2 times, most recently from 25a63c9 to 627557c Compare February 4, 2026 14:15
@OskarStark OskarStark requested a review from Copilot February 4, 2026 22:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses memory leak issues in FrankenPHP worker mode and Messenger consumption by implementing ResetInterface on traceable services and in-memory stores. The changes ensure that profiling data and in-memory storage are properly cleared between requests.

Changes:

  • Added ResetInterface support to in-memory stores (Chat and Store components) to clear stored data between requests
  • Added ResetInterface support to all traceable profiler decorators (Platform, Chat, MessageStore, Toolbox, and Registry) to clear profiling data
  • Removed lazy loading from in-memory stores since ResetInterface requires working with actual instances rather than proxies
  • Added symfony/service-contracts dependency to affected packages for ResetInterface

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/store/src/InMemory/Store.php Implements ResetInterface with reset() delegating to drop()
src/store/tests/InMemory/StoreTest.php Adds test verifying reset clears documents
src/store/composer.json Adds symfony/service-contracts dependency
src/store/CHANGELOG.md Documents ResetInterface addition
src/chat/src/InMemory/Store.php Implements ResetInterface with reset() clearing all messages
src/chat/tests/InMemory/StoreTest.php Adds test verifying reset clears messages
src/chat/composer.json Adds symfony/service-contracts dependency
src/chat/CHANGELOG.md Documents ResetInterface addition
src/mcp-bundle/src/Profiler/TraceableRegistry.php Implements ResetInterface delegating to clear()
src/mcp-bundle/tests/Profiler/TraceableRegistryTest.php Adds test verifying reset calls clear()
src/mcp-bundle/src/McpBundle.php Adds kernel.reset tag to traceable registry service
src/mcp-bundle/composer.json Adds symfony/service-contracts dependency
src/mcp-bundle/CHANGELOG.md Documents ResetInterface addition
src/ai-bundle/src/Profiler/TraceableToolbox.php Implements ResetInterface to clear calls array
src/ai-bundle/src/Profiler/TraceablePlatform.php Implements ResetInterface to clear calls and resultCache
src/ai-bundle/src/Profiler/TraceableMessageStore.php Implements ResetInterface with delegation to wrapped store
src/ai-bundle/src/Profiler/TraceableChat.php Implements ResetInterface with delegation to wrapped chat
src/ai-bundle/tests/Profiler/TraceableToolboxTest.php Adds test verifying reset clears calls
src/ai-bundle/tests/Profiler/TraceablePlatformTest.php Adds test verifying reset clears calls and resultCache
src/ai-bundle/tests/Profiler/TraceableMessageStoreTest.php Adds test verifying reset clears calls
src/ai-bundle/tests/Profiler/TraceableChatTest.php Adds test verifying reset clears calls
src/ai-bundle/src/AiBundle.php Adds kernel.reset tags to all traceable services and in-memory stores; removes lazy loading and proxy tags from in-memory stores
src/ai-bundle/tests/DependencyInjection/AiBundleTest.php Updates tests to expect non-lazy in-memory stores with kernel.reset tag instead of proxy tags
src/ai-bundle/composer.json Adds symfony/service-contracts dependency
src/ai-bundle/CHANGELOG.md Documents ResetInterface addition to traceable services

@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from 627557c to 0adcdfb Compare February 4, 2026 22:31
@santysisi santysisi force-pushed the feat/clean-memory-after-each-request-tracers branch from 0adcdfb to 88ecda9 Compare February 4, 2026 22:35
@OskarStark
Copy link
Contributor

rebase unlocked

@OskarStark
Copy link
Contributor

after a rebase, we can merge this PR

@santysisi
Copy link
Contributor Author

Hi, I don't have my computer right now 😔
I am on vacation
I will have my computer again after vacation ( 10 days aprox ), after that I will rebase and resolve conflicts 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Bundle Issues & PRs about the AI integration bundle Bug Something isn't working Feature New feature Status: Reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory Not Cleared and Tracers Reporting Cumulative Data in Symfony AI with FrankenPHP Worker Mode

4 participants