GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-101766: Fix refleak for _BlockingOnManager resources by corona10 · Pull Request #101942 · python/cpython

Conversation

@corona10

@corona10 corona10 commented Feb 16, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: Refleak in test_importlib on aarch64 RHEL8 #101766

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@corona10
corona10 requested review from gvanrossum and sobolevn February 16, 2023 00:12
@bedevere-bot bedevere-bot added the awaiting core review label Feb 16, 2023
@bedevere-bot bedevere-bot mentioned this pull request Feb 16, 2023
Refleak in test_importlib on aarch64 RHEL8 #101766
Closed
@corona10 corona10 added skip news 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section labels Feb 16, 2023
@bedevere-bot

bedevere-bot commented Feb 16, 2023

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @corona10 for commit 6ff177a 🤖

If you want to schedule another build, you need to add the :hammer: test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 16, 2023
@corona10 corona10 added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 16, 2023
@bedevere-bot

bedevere-bot commented Feb 16, 2023

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @corona10 for commit 6ff177a 🤖

If you want to schedule another build, you need to add the :hammer: test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 16, 2023
gvanrossum
gvanrossum approved these changes Feb 16, 2023
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM, though I never saw this code before.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Feb 16, 2023
@corona10

corona10 commented Feb 16, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

The current failure does not relate to this PR..
Also, it is failed with the main branch too: eb0c485 and also for 89ac665

$> ./python -m test test_imp -R 3:3 -v

======================================================================
FAIL: test_singlephase_variants (test.test_imp.ImportTests.test_singlephase_variants) [_testsinglephase]
Exercise the most meaningful variants described in Python/import.c.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/cpython/Lib/test/test_imp.py", line 366, in test_singlephase_variants
    self.assertEqual(init_count, expected_init_count)
AssertionError: 3 != 1

======================================================================
FAIL: test_singlephase_variants (test.test_imp.ImportTests.test_singlephase_variants) [_testsinglephase_basic_wrapper]
Exercise the most meaningful variants described in Python/import.c.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/cpython/Lib/test/test_imp.py", line 383, in test_singlephase_variants
    self.assertEqual(init_count, expected_init_count)
AssertionError: 3 != 2

----------------------------------------------------------------------
Ran 30 tests in 0.106s
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@corona10

corona10 commented Feb 16, 2023

Copy link
Copy Markdown
Member Author

@ericsnowcurrently Can you please take a look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sobolevn
sobolevn approved these changes Feb 16, 2023
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We can safely remove it, because it is reset here: self.blocked_on = _blocking_on.setdefault(self.thread_id, [])

Thanks for fixing this!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@corona10

corona10 commented Feb 16, 2023

Copy link
Copy Markdown
Member Author

@gvanrossum @sobolevn
I will merge this PR after I check the leak test is passed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@corona10 corona10 self-assigned this Feb 16, 2023
ericsnowcurrently
ericsnowcurrently approved these changes Feb 16, 2023
View reviewed changes

@ericsnowcurrently ericsnowcurrently left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

I left one comment but feel free to ignore it if you don't think it matters.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/importlib/_bootstrap.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@corona10

corona10 commented Feb 16, 2023

Copy link
Copy Markdown
Member Author

@ericsnowcurrently

Hi Eric
096d009 looks causing
$> ./python -m test test_imp -R 3:3 failure.
Is this under investigation?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Feb 16, 2023

Copy link
Copy Markdown
Member

I'll take a look at the refleak.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Feb 16, 2023

Copy link
Copy Markdown
Member

I'm working on it via gh-101969.

corona10 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

corona10 and others added 2 commits February 17, 2023 10:05
@corona10
pythongh-101766: Fix refleak for _BlockingOnManager resources
65c845f
@corona10 @ericsnowcurrently
Update Lib/importlib/_bootstrap.py
79fd697
Co-authored-by: Eric Snow <[email protected]>
@corona10
corona10 force-pushed the gh-101766 branch from cd36caf to 79fd697 Compare February 17, 2023 01:05
@corona10 corona10 added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 17, 2023
@bedevere-bot

bedevere-bot commented Feb 17, 2023

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @corona10 for commit 79fd697 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 17, 2023
@corona10

corona10 commented Feb 17, 2023

Copy link
Copy Markdown
Member Author

Merge this PR since refleaks test are passed, and macOS CI is another issue:
#101981

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@corona10
corona10 merged commit 775f881 into python:main Feb 17, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Feb 17, 2023
@corona10
corona10 deleted the gh-101766 branch February 17, 2023 10:14
exarkun
exarkun reviewed Feb 17, 2023
View reviewed changes
Comment thread Lib/importlib/_bootstrap.py
Comment on lines +88 to +92
if len(self.blocked_on) == 0:
# gh-101766: glboal cache should be cleaned-up
# if there is no more _blocking_on for this thread.
del _blocking_on[self.thread_id]
del self.blocked_on

@exarkun exarkun Feb 17, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@corona10 @gvanrossum @sobolevn

This change re-introduces the re-entrancy bug that _BlockingOnManager was introduced to fix.

Consider: A thread has executed __enter__ and begins executing __exit__. It executes line 87 and the length of self.blocked_on becomes 0. The check on the new line 88 passes and the thread enters the block starting with the new line 89. Call this "point x". Now the garbage collector is triggered and an object with a __del__ is collected. The __del__ runs and imports a module (as is very common now that there are ResourceWarnings). As part of satisfying the import, the thread runs __enter__. It executes line 82 and receives the list that is already in _blocking_on (it uses the same key because it is the same thread - this is a single-threaded case). It executes line 83 to add its lock to the list. The import eventually finishes and it executes __exit__. It removes its lock from the list (line 87) and removes the list from _blocking_on (new line 91). Garbage collection proceeds and we get back to the original import being resolved. Execution returns to "point x". At point x we have already decided there are no locks in self.blocked_on and we're going to clean up the global dictionary. New line 91 runs and finds no item at key self.thread_id. This raises a KeyError which goes unhandled all the way up to some application code that was doing import somemodule.

I recommend reverting this change until a re-entrant safe version of the fix can be determined. The resource leak is not great but if I understand correctly, it is also more or less academic right now (it causes a very small amount of memory to be wasted while the test suite is running). The re-entrancy bug is real and breaks many real applications in extremely confusing ways (consider that the original bug reports were open for years before anyone tracked down the problem, at great expense, and it took more than half a year to get the fix reviewed and merged).

Thanks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@corona10 corona10 Feb 17, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@exarkun cc @gvanrossum @sobolevn
Okay, I understand your intention.
I will try to submit the patch if the resource can be cleaned up at the test suite level including reverting the current change..

And also would you like to submit the test code to prevent regression?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@exarkun exarkun Feb 17, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I will try to submit the patch if the resource can be cleaned up at the test suite level including reverting the current change..

Thanks you very much.

And also would you like to submit the test code to prevent regression?

That would be ideal, certainly. There is a reproducer on #94504 but it is CPU intensive and non-deterministic without additional instrumentation inside _bootstrap.py itself. It's not clear to me exactly how to turn this into a regression test that can be included in the automated suite. I'd be happy to discuss it further though.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
corona10 added a commit to corona10/cpython that referenced this pull request Feb 17, 2023
@corona10
Revert "pythongh-101766: Fix refleak for _BlockingOnManager resources (…
707db41
…pythongh-101942)"

This reverts commit 775f881.
This was referenced Feb 17, 2023
gh-101766: Fix refleak for _BlockingOnManager resources at test-suite level #101987
Closed
gh-101766: Fix refleak for _BlockingOnManager resources from test suite level #101988
Merged
@gvanrossum

gvanrossum commented Feb 17, 2023

Copy link
Copy Markdown
Member

Would it be possible to delete the cached empty list in a __del__ method?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chris-eibl chris-eibl mentioned this pull request Jun 30, 2023
gh-106176: Fix reference leak in importlib/_bootstrap.py #106207
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericsnowcurrently ericsnowcurrently ericsnowcurrently approved these changes
@gvanrossum gvanrossum gvanrossum approved these changes
@sobolevn sobolevn sobolevn approved these changes
+1 more reviewer
@exarkun exarkun exarkun left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@corona10 corona10

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@corona10 @bedevere-bot @ericsnowcurrently @gvanrossum @exarkun @sobolevn
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.