{"action":"created","comment":{"url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/comments/51698253","id":51698253,"diff_hunk":"@@ -37,16 +43,110 @@ public function __construct(ManagerRegistry $registry)\n     public function detach($object)\n     {\n         $objectManager = $this->getObjectManager($object);\n+        $visited = [];\n \n         if ($objectManager instanceof DocumentManager) {\n-            $visited = [];\n             $this->doDetach($object, $visited);\n         } else {\n             $objectManager->detach($object);\n+            $this->doDetachScheduled($object, $visited);\n+        }\n+    }\n+\n+    /**\n+     * Detach entity living in the scheduledForDirtyCheck's\n+     * unit of work property.\n+     *\n+     * @param object $entity the entity to be detached\n+     * @param array  $visited array of detached entity\n+     *\n+     * @return void\n+     */\n+    public function doDetachScheduled($entity, array &$visited)\n+    {\n+        $objectManager = $this->getObjectManager($entity);\n+        $uow = $objectManager->getUnitOfWork();\n+        $class = $objectManager->getClassMetadata(ClassUtils::getClass($entity));\n+        $rootClassName = $class->rootEntityName;\n+        $oid = spl_object_hash($entity);\n+\n+        if (isset($visited[$oid])) {\n+            return;\n+        }\n+\n+        $visited[$oid] = $entity;\n+\n+        if (null === $this->scheduledForDirtyCheck) {\n+            $this->scheduledForDirtyCheck = &$this->getScheduledForDirtyCheck($uow);\n+        }\n+        unset($this->scheduledForDirtyCheck[$rootClassName][$oid]);\n+\n+        $this->cascadeDetachScheduled($entity, $visited);\n+    }\n+\n+    /**\n+     * Cascades a detach entities associated to entities living in the\n+     * scheduledForDirtyCheck unit of work property.\n+     *\n+     * @param object $entity the entity to be detached\n+     * @param array  $visited array of already detached entity\n+     *\n+     * @return void","path":"src/Akeneo/Bundle/StorageUtilsBundle/Doctrine/Common/Detacher/ObjectDetacher.php","position":79,"original_position":79,"commit_id":"87aa9bd15ceb32fdcecffaad89b35b26aa5b40d8","original_commit_id":"87aa9bd15ceb32fdcecffaad89b35b26aa5b40d8","user":{"login":"aRn0D","id":3585922,"avatar_url":"https://avatars.githubusercontent.com/u/3585922?v=3","gravatar_id":"","url":"https://api.github.com/users/aRn0D","html_url":"https://github.com/aRn0D","followers_url":"https://api.github.com/users/aRn0D/followers","following_url":"https://api.github.com/users/aRn0D/following{/other_user}","gists_url":"https://api.github.com/users/aRn0D/gists{/gist_id}","starred_url":"https://api.github.com/users/aRn0D/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aRn0D/subscriptions","organizations_url":"https://api.github.com/users/aRn0D/orgs","repos_url":"https://api.github.com/users/aRn0D/repos","events_url":"https://api.github.com/users/aRn0D/events{/privacy}","received_events_url":"https://api.github.com/users/aRn0D/received_events","type":"User","site_admin":false},"body":"idem","created_at":"2016-02-03T09:44:26Z","updated_at":"2016-02-03T09:44:26Z","html_url":"https://github.com/akeneo/pim-community-dev/pull/3928#discussion_r51698253","pull_request_url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928","_links":{"self":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/comments/51698253"},"html":{"href":"https://github.com/akeneo/pim-community-dev/pull/3928#discussion_r51698253"},"pull_request":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928"}}},"pull_request":{"url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928","id":57216543,"html_url":"https://github.com/akeneo/pim-community-dev/pull/3928","diff_url":"https://github.com/akeneo/pim-community-dev/pull/3928.diff","patch_url":"https://github.com/akeneo/pim-community-dev/pull/3928.patch","issue_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues/3928","number":3928,"state":"open","locked":false,"title":"[SLA] PIM-5470: Fix memory leak on rule engine","user":{"login":"SamirBoulil","id":1826473,"avatar_url":"https://avatars.githubusercontent.com/u/1826473?v=3","gravatar_id":"","url":"https://api.github.com/users/SamirBoulil","html_url":"https://github.com/SamirBoulil","followers_url":"https://api.github.com/users/SamirBoulil/followers","following_url":"https://api.github.com/users/SamirBoulil/following{/other_user}","gists_url":"https://api.github.com/users/SamirBoulil/gists{/gist_id}","starred_url":"https://api.github.com/users/SamirBoulil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SamirBoulil/subscriptions","organizations_url":"https://api.github.com/users/SamirBoulil/orgs","repos_url":"https://api.github.com/users/SamirBoulil/repos","events_url":"https://api.github.com/users/SamirBoulil/events{/privacy}","received_events_url":"https://api.github.com/users/SamirBoulil/received_events","type":"User","site_admin":false},"body":"| Q                 | A\r\n| ----------------- | ---\r\n| Specs             | Yes\r\n| Behats            | n/a\r\n| Blue CI           | running\r\n| Changelog updated | Yes\r\n| Review and 2 GTM  | \r\n\r\nThis PR solves an issue we encounter with the rule engine and especially with Doctrine's `UnitOfWork.php` file.\r\n\r\nWhen we iterate over subsets of products selected by a rule, the products which the `computeChangeSet()` function returns False (that the rule does not apply on) are referenced into a doctrine's internal attribute called `$scheduledForDirtyCheck`.\r\n\r\nWhen commiting those unchanged products, Doctrine's `commit()` function makes an early return and  do not clear them from the array.\r\nAs we iterate and load more and more products the array keeps growing until a fatal error arises because no memory space is available for the process to continue.\r\n\r\nThe first solution we found to solve this issue was by listening and waiting for the doctrine's `post_flush` event to clear the `$scheduledForDirtyCheck` Doctrine's internal array.\r\nThe way it was implemented was by using listeners as well as reflection. As the `$scheduledForDirtyCheck` array is private, we use reflection to set it accessible to clear the array.\r\n\r\nThe problem with this implementation was that it was defined in it's own bundle with a new service and  we were resetting the `$scheduledForDirtyCheck` array to `[]` which can have bad consequences in the long term.\r\n\r\nWe chose to implement this fix in the `ObjectDetacher` and to make it smarter by detaching entities from the `$scheduledForDirtyCheck` array at the same time we call the usual doctrine `detach()`. \r\n\r\nSo on Flush, the entity is detached from the `$identityMap` as well as from the `$scheduledForDirtyCheck`.\r\n\r\nI opened an issue on Doctrine's repo (https://github.com/doctrine/doctrine2/issues/5625).\r\n\r\n![Alt Text](http://i.giphy.com/4PvpVZBoNpuDe.gif)\r\n","created_at":"2016-01-26T14:24:24Z","updated_at":"2016-02-03T09:44:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0bb1706df1b4f2d0ff6cd1884b4e099fcf0c013a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928/commits","review_comments_url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928/comments","review_comment_url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/comments{/number}","comments_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues/3928/comments","statuses_url":"https://api.github.com/repos/akeneo/pim-community-dev/statuses/87aa9bd15ceb32fdcecffaad89b35b26aa5b40d8","head":{"label":"akeneo:PIM-5470","ref":"PIM-5470","sha":"87aa9bd15ceb32fdcecffaad89b35b26aa5b40d8","user":{"login":"akeneo","id":2510186,"avatar_url":"https://avatars.githubusercontent.com/u/2510186?v=3","gravatar_id":"","url":"https://api.github.com/users/akeneo","html_url":"https://github.com/akeneo","followers_url":"https://api.github.com/users/akeneo/followers","following_url":"https://api.github.com/users/akeneo/following{/other_user}","gists_url":"https://api.github.com/users/akeneo/gists{/gist_id}","starred_url":"https://api.github.com/users/akeneo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akeneo/subscriptions","organizations_url":"https://api.github.com/users/akeneo/orgs","repos_url":"https://api.github.com/users/akeneo/repos","events_url":"https://api.github.com/users/akeneo/events{/privacy}","received_events_url":"https://api.github.com/users/akeneo/received_events","type":"Organization","site_admin":false},"repo":{"id":5367323,"name":"pim-community-dev","full_name":"akeneo/pim-community-dev","owner":{"login":"akeneo","id":2510186,"avatar_url":"https://avatars.githubusercontent.com/u/2510186?v=3","gravatar_id":"","url":"https://api.github.com/users/akeneo","html_url":"https://github.com/akeneo","followers_url":"https://api.github.com/users/akeneo/followers","following_url":"https://api.github.com/users/akeneo/following{/other_user}","gists_url":"https://api.github.com/users/akeneo/gists{/gist_id}","starred_url":"https://api.github.com/users/akeneo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akeneo/subscriptions","organizations_url":"https://api.github.com/users/akeneo/orgs","repos_url":"https://api.github.com/users/akeneo/repos","events_url":"https://api.github.com/users/akeneo/events{/privacy}","received_events_url":"https://api.github.com/users/akeneo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akeneo/pim-community-dev","description":"[Community Development Repository] The open source Product Information Management (PIM)","fork":false,"url":"https://api.github.com/repos/akeneo/pim-community-dev","forks_url":"https://api.github.com/repos/akeneo/pim-community-dev/forks","keys_url":"https://api.github.com/repos/akeneo/pim-community-dev/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akeneo/pim-community-dev/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akeneo/pim-community-dev/teams","hooks_url":"https://api.github.com/repos/akeneo/pim-community-dev/hooks","issue_events_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues/events{/number}","events_url":"https://api.github.com/repos/akeneo/pim-community-dev/events","assignees_url":"https://api.github.com/repos/akeneo/pim-community-dev/assignees{/user}","branches_url":"https://api.github.com/repos/akeneo/pim-community-dev/branches{/branch}","tags_url":"https://api.github.com/repos/akeneo/pim-community-dev/tags","blobs_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/refs{/sha}","trees_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akeneo/pim-community-dev/statuses/{sha}","languages_url":"https://api.github.com/repos/akeneo/pim-community-dev/languages","stargazers_url":"https://api.github.com/repos/akeneo/pim-community-dev/stargazers","contributors_url":"https://api.github.com/repos/akeneo/pim-community-dev/contributors","subscribers_url":"https://api.github.com/repos/akeneo/pim-community-dev/subscribers","subscription_url":"https://api.github.com/repos/akeneo/pim-community-dev/subscription","commits_url":"https://api.github.com/repos/akeneo/pim-community-dev/commits{/sha}","git_commits_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/commits{/sha}","comments_url":"https://api.github.com/repos/akeneo/pim-community-dev/comments{/number}","issue_comment_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues/comments{/number}","contents_url":"https://api.github.com/repos/akeneo/pim-community-dev/contents/{+path}","compare_url":"https://api.github.com/repos/akeneo/pim-community-dev/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akeneo/pim-community-dev/merges","archive_url":"https://api.github.com/repos/akeneo/pim-community-dev/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akeneo/pim-community-dev/downloads","issues_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues{/number}","pulls_url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls{/number}","milestones_url":"https://api.github.com/repos/akeneo/pim-community-dev/milestones{/number}","notifications_url":"https://api.github.com/repos/akeneo/pim-community-dev/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akeneo/pim-community-dev/labels{/name}","releases_url":"https://api.github.com/repos/akeneo/pim-community-dev/releases{/id}","deployments_url":"https://api.github.com/repos/akeneo/pim-community-dev/deployments","created_at":"2012-08-10T09:27:26Z","updated_at":"2016-02-02T23:09:35Z","pushed_at":"2016-02-03T09:39:29Z","git_url":"git://github.com/akeneo/pim-community-dev.git","ssh_url":"git@github.com:akeneo/pim-community-dev.git","clone_url":"https://github.com/akeneo/pim-community-dev.git","svn_url":"https://github.com/akeneo/pim-community-dev","homepage":"http://www.akeneo.com","size":135881,"stargazers_count":285,"watchers_count":285,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":147,"mirror_url":null,"open_issues_count":98,"forks":147,"open_issues":98,"watchers":285,"default_branch":"master"}},"base":{"label":"akeneo:1.4","ref":"1.4","sha":"d9a45155728fbfe994e53ac98a683153243cda02","user":{"login":"akeneo","id":2510186,"avatar_url":"https://avatars.githubusercontent.com/u/2510186?v=3","gravatar_id":"","url":"https://api.github.com/users/akeneo","html_url":"https://github.com/akeneo","followers_url":"https://api.github.com/users/akeneo/followers","following_url":"https://api.github.com/users/akeneo/following{/other_user}","gists_url":"https://api.github.com/users/akeneo/gists{/gist_id}","starred_url":"https://api.github.com/users/akeneo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akeneo/subscriptions","organizations_url":"https://api.github.com/users/akeneo/orgs","repos_url":"https://api.github.com/users/akeneo/repos","events_url":"https://api.github.com/users/akeneo/events{/privacy}","received_events_url":"https://api.github.com/users/akeneo/received_events","type":"Organization","site_admin":false},"repo":{"id":5367323,"name":"pim-community-dev","full_name":"akeneo/pim-community-dev","owner":{"login":"akeneo","id":2510186,"avatar_url":"https://avatars.githubusercontent.com/u/2510186?v=3","gravatar_id":"","url":"https://api.github.com/users/akeneo","html_url":"https://github.com/akeneo","followers_url":"https://api.github.com/users/akeneo/followers","following_url":"https://api.github.com/users/akeneo/following{/other_user}","gists_url":"https://api.github.com/users/akeneo/gists{/gist_id}","starred_url":"https://api.github.com/users/akeneo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akeneo/subscriptions","organizations_url":"https://api.github.com/users/akeneo/orgs","repos_url":"https://api.github.com/users/akeneo/repos","events_url":"https://api.github.com/users/akeneo/events{/privacy}","received_events_url":"https://api.github.com/users/akeneo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akeneo/pim-community-dev","description":"[Community Development Repository] The open source Product Information Management (PIM)","fork":false,"url":"https://api.github.com/repos/akeneo/pim-community-dev","forks_url":"https://api.github.com/repos/akeneo/pim-community-dev/forks","keys_url":"https://api.github.com/repos/akeneo/pim-community-dev/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akeneo/pim-community-dev/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akeneo/pim-community-dev/teams","hooks_url":"https://api.github.com/repos/akeneo/pim-community-dev/hooks","issue_events_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues/events{/number}","events_url":"https://api.github.com/repos/akeneo/pim-community-dev/events","assignees_url":"https://api.github.com/repos/akeneo/pim-community-dev/assignees{/user}","branches_url":"https://api.github.com/repos/akeneo/pim-community-dev/branches{/branch}","tags_url":"https://api.github.com/repos/akeneo/pim-community-dev/tags","blobs_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/refs{/sha}","trees_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akeneo/pim-community-dev/statuses/{sha}","languages_url":"https://api.github.com/repos/akeneo/pim-community-dev/languages","stargazers_url":"https://api.github.com/repos/akeneo/pim-community-dev/stargazers","contributors_url":"https://api.github.com/repos/akeneo/pim-community-dev/contributors","subscribers_url":"https://api.github.com/repos/akeneo/pim-community-dev/subscribers","subscription_url":"https://api.github.com/repos/akeneo/pim-community-dev/subscription","commits_url":"https://api.github.com/repos/akeneo/pim-community-dev/commits{/sha}","git_commits_url":"https://api.github.com/repos/akeneo/pim-community-dev/git/commits{/sha}","comments_url":"https://api.github.com/repos/akeneo/pim-community-dev/comments{/number}","issue_comment_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues/comments{/number}","contents_url":"https://api.github.com/repos/akeneo/pim-community-dev/contents/{+path}","compare_url":"https://api.github.com/repos/akeneo/pim-community-dev/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akeneo/pim-community-dev/merges","archive_url":"https://api.github.com/repos/akeneo/pim-community-dev/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akeneo/pim-community-dev/downloads","issues_url":"https://api.github.com/repos/akeneo/pim-community-dev/issues{/number}","pulls_url":"https://api.github.com/repos/akeneo/pim-community-dev/pulls{/number}","milestones_url":"https://api.github.com/repos/akeneo/pim-community-dev/milestones{/number}","notifications_url":"https://api.github.com/repos/akeneo/pim-community-dev/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akeneo/pim-community-dev/labels{/name}","releases_url":"https://api.github.com/repos/akeneo/pim-community-dev/releases{/id}","deployments_url":"https://api.github.com/repos/akeneo/pim-community-dev/deployments","created_at":"2012-08-10T09:27:26Z","updated_at":"2016-02-02T23:09:35Z","pushed_at":"2016-02-03T09:39:29Z","git_url":"git://github.com/akeneo/pim-community-dev.git","ssh_url":"git@github.com:akeneo/pim-community-dev.git","clone_url":"https://github.com/akeneo/pim-community-dev.git","svn_url":"https://github.com/akeneo/pim-community-dev","homepage":"http://www.akeneo.com","size":135881,"stargazers_count":285,"watchers_count":285,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":147,"mirror_url":null,"open_issues_count":98,"forks":147,"open_issues":98,"watchers":285,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928"},"html":{"href":"https://github.com/akeneo/pim-community-dev/pull/3928"},"issue":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/issues/3928"},"comments":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/issues/3928/comments"},"review_comments":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928/comments"},"review_comment":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/pulls/3928/commits"},"statuses":{"href":"https://api.github.com/repos/akeneo/pim-community-dev/statuses/87aa9bd15ceb32fdcecffaad89b35b26aa5b40d8"}}}}