{"action":"created","issue":{"url":"https://api.github.com/repos/norberteder/trello/issues/5","labels_url":"https://api.github.com/repos/norberteder/trello/issues/5/labels{/name}","comments_url":"https://api.github.com/repos/norberteder/trello/issues/5/comments","events_url":"https://api.github.com/repos/norberteder/trello/issues/5/events","html_url":"https://github.com/norberteder/trello/issues/5","id":41431032,"number":5,"title":"\"possible EventEmitter memory leak detected\" even using promises","user":{"login":"kblok","id":2198466,"avatar_url":"https://avatars.githubusercontent.com/u/2198466?v=3","gravatar_id":"","url":"https://api.github.com/users/kblok","html_url":"https://github.com/kblok","followers_url":"https://api.github.com/users/kblok/followers","following_url":"https://api.github.com/users/kblok/following{/other_user}","gists_url":"https://api.github.com/users/kblok/gists{/gist_id}","starred_url":"https://api.github.com/users/kblok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kblok/subscriptions","organizations_url":"https://api.github.com/users/kblok/orgs","repos_url":"https://api.github.com/users/kblok/repos","events_url":"https://api.github.com/users/kblok/events{/privacy}","received_events_url":"https://api.github.com/users/kblok/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2014-08-28T19:19:09Z","updated_at":"2015-09-09T14:36:19Z","closed_at":"2015-09-09T09:12:02Z","body":"I am getting this \"possible EventEmitter memory leak detected\" when I try to create more than 10 cards at the same time.\r\nThis could be the expected behavior but the thing is that I am using promises so I am chaining the card creation in this way:\r\n\r\n```\r\nvar promise;\r\nvar cardCreationPromise;\r\n_.each(myData, function (ticket) {\r\n                if(!promise) {\r\n                    cardCreationPromise = Q.defer();\r\n                    trello.addCard(ticket.summary, ticket.description, listId, cardCreationPromise.makeNodeResolver());\r\n                    promise = cardCreationPromise.promise;\r\n                } else {\r\n                    promise = promise.then(function(e) {\r\n                        cardCreationPromise = Q.defer();\r\n                        trello.addCard(ticket.summary, ticket.description, listId, cardCreationPromise.makeNodeResolver());\r\n                        return cardCreationPromise.promise;\r\n                    });    \r\n                }\r\n            });\r\n```\r\n\r\nTypical chaining...\r\n\r\nAfter some research I have learned that this \"memory leak issue \"is a common problem in Restler and in  Node JS in general.\r\nYou can do your own research and see if you end up with the same solution as I. The main problem is that when we use the \"on\" event, the listener is still registered so we are \"leaking\" (It is not technically a leak) listeners even if we chain the calls. But if you use \"once\" instead of \"on\" we are calling to the next card creating after the listener is cleared, and we should clear the \"error\" listener too:\r\n\r\n```\r\nfunction makeRequest(fn, uri, options, callback) {\r\n    var request = fn(uri, options);\r\n    request.once('complete', function (result) {\r\n        if (result instanceof Error) {\r\n            callback(result);\r\n        } else {\r\n            callback(null, result);\r\n        }\r\n        request.removeAllListeners('error');\r\n    });\r\n}\r\n```\r\n\r\nThese two changes fix the issue in my scenario.\r\n"},"comment":{"url":"https://api.github.com/repos/norberteder/trello/issues/comments/138929220","html_url":"https://github.com/norberteder/trello/issues/5#issuecomment-138929220","issue_url":"https://api.github.com/repos/norberteder/trello/issues/5","id":138929220,"user":{"login":"sinisterstuf","id":840466,"avatar_url":"https://avatars.githubusercontent.com/u/840466?v=3","gravatar_id":"","url":"https://api.github.com/users/sinisterstuf","html_url":"https://github.com/sinisterstuf","followers_url":"https://api.github.com/users/sinisterstuf/followers","following_url":"https://api.github.com/users/sinisterstuf/following{/other_user}","gists_url":"https://api.github.com/users/sinisterstuf/gists{/gist_id}","starred_url":"https://api.github.com/users/sinisterstuf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sinisterstuf/subscriptions","organizations_url":"https://api.github.com/users/sinisterstuf/orgs","repos_url":"https://api.github.com/users/sinisterstuf/repos","events_url":"https://api.github.com/users/sinisterstuf/events{/privacy}","received_events_url":"https://api.github.com/users/sinisterstuf/received_events","type":"User","site_admin":false},"created_at":"2015-09-09T14:36:19Z","updated_at":"2015-09-09T14:36:19Z","body":"The tests to confirm it are currently marked as pending because they require work to reproduce, but I'm working on generalising them."}}