Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-44353: Fix memory leak introduced by #27262#27305

Merged
ambv merged 3 commits into
python:mainpython/cpython:mainfrom
uriyyo:fix-issue-44353-memory-leakuriyyo/cpython:fix-issue-44353-memory-leakCopy head branch name to clipboard
Jul 23, 2021
Merged

bpo-44353: Fix memory leak introduced by #27262#27305
ambv merged 3 commits into
python:mainpython/cpython:mainfrom
uriyyo:fix-issue-44353-memory-leakuriyyo/cpython:fix-issue-44353-memory-leakCopy head branch name to clipboard

Conversation

@uriyyo

@uriyyo uriyyo commented Jul 23, 2021

Copy link
Copy Markdown
Member

@uriyyo

uriyyo commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

@pablogsal This should fix memory leak introduced by #27262

And could you please add skip-news label?

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not correct, you are returning a new object without incrementing the refcount, this will crash surely

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ambv

ambv commented Jul 23, 2021

Copy link
Copy Markdown
Contributor

@uriyyo, look at #27262 (review)

@uriyyo

uriyyo commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

Hmm, I will try to investigate where memory leak is.

@uriyyo

uriyyo commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

@ambv Thanks for comment. Did forget about it.

@uriyyo uriyyo changed the title bpo-44353: Fix memory leak at _typing__idfunc bpo-44353: Fix memory leak introduced by #27262 Jul 23, 2021
@uriyyo

uriyyo commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

@pablogsal @ambv I have found were the memory leak is, that's not because of C extension, sorry about confusing with me first comment.

Memory leak was because of inner typing module cache when typing.Union was created.

This lines lead to memory leak:

self.assertEqual(UserId | cls, self.module.Union[UserId, cls])
self.assertEqual(cls | UserId, self.module.Union[cls, UserId])
self.assertEqual(self.module.get_args(UserId | cls), (UserId, cls))
self.assertEqual(self.module.get_args(cls | UserId), (cls, UserId))

I fixed this issue by clearing cache after test execution.

@uriyyo

uriyyo commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@pablogsal: please review the changes made to this pull request.

@ambv ambv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confirmed locally this fixes the refleaks.

Without the change:

❯ ./python.exe -m test test_typing -R :
+ cd cpython
+ shift
+ PYTHONWARNINGS=
+ PYTHONSTARTUP=
+ IN_REGRTEST=1
+ ./python.exe -E -Wd -m test test_typing -R :
0:00:00 load avg: 1.99 Run tests sequentially
0:00:00 load avg: 1.99 [1/1] test_typing
beginning 9 repetitions
123456789
.........
test_typing leaked [220, 220, 220, 220] references, sum=880
test_typing leaked [68, 68, 68, 68] memory blocks, sum=272
test_typing failed (reference leak)

== Tests result: FAILURE ==

1 test failed:
    test_typing

1 re-run test:
    test_typing

Total duration: 5.6 sec
Tests result: FAILURE

After the change:

❯ ./python.exe -m test test_typing -R :
+ cd cpython
+ shift
+ PYTHONWARNINGS=
+ PYTHONSTARTUP=
+ IN_REGRTEST=1
+ ./python.exe -E -Wd -m test test_typing -R :
0:00:00 load avg: 2.03 Run tests sequentially
0:00:00 load avg: 2.03 [1/1] test_typing
beginning 9 repetitions
123456789
.........

== Tests result: SUCCESS ==

1 test OK.

Total duration: 5.5 sec
Tests result: SUCCESS

Comment thread Lib/test/test_typing.py


class NewTypeTests:
def cleanup(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note to self: this is different from BaseTestCase.clear_caches because here we use self.module instead of hard-coded typing.

@Fidget-Spinner Fidget-Spinner Jul 23, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wow, great investigating @uriyyo! I forgot that the global typing and the py_typing used for NewType tests are different module objects. I was quite puzzled for a while about why the BaseTestCase clearing didn't work :).

@uriyyo uriyyo Jul 23, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@Fidget-Spinner I spent a lot of time investigating C code because though that problem was at it, but actual problem was at test code)

It was tricky but interesting to investigate this memory leak)

Comment thread Lib/test/test_typing.py
class NewTypeTests:
def cleanup(self):
for f in self.module._cleanups:
f()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Those seem to be cleaning LRU caches for various type __getitem__.

@uriyyo uriyyo Jul 23, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup, and it clears Union.__getitem__ cache which lead to memory leak.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.