Open bbc2 opened 4 years ago
Hi! Thanks a lot for the bug report; this is indeed a nasty bug.
The memory leak should indeed be present with Django as well, but won't have as big an impact there. I'll look into it.
I tested this again but with more recent versions of SQLAlchemy and found the following:
SELECT
disappears with SQLAlchemy >= 1.3.21 and any version of factory_boy.
As a result, the impact of this issue is much lower for us, since we only use factory_boy in tests, but I suppose the extra memory reference could still be an issue for other users.
Description
Factory_boy keeps SQLAlchemy instances passed as argument to factories in memory, causing a memory leak and changing what requests SQLAlchemy performs.
The changed behavior for SQLAlchemy, in our case, is additional
SELECT
statements. That's an issue for us because we count SQL queries in tests to ensure the absence of typical SQL bugs such as the n+1-query problem.That being said, I would expect the memory leak to potentially cause other issues.
To Reproduce
Run the following with factory_boy 3.0.0 or 3.0.1:
When running this, you'll see an unexpected
SELECT
statement performed on theorgs
table:The expected output is the following:
To see the memory leak, replace
main
by:You'll see the following output:
That shows that
Org
is still in memory although it should have been collected.Expected output:
Notes
I bisected the problem to PR #658. More precisely, it seems to be the assignment
cls._original_params = params
in the_generate
method that causes the memory leak. Therefore, the problem was introduced in version 3.0.0.I suspect the problem is also present in the Django model factory.