Skip to content

bpo-34042: Fix dict.copy() to maintain correct total refcount #8119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2018

Conversation

1st1
Copy link
Member

@1st1 1st1 commented Jul 5, 2018

@1st1 1st1 requested a review from methane July 5, 2018 20:22

for i in range(10):
dct.copy()
first_ref_count = gettotalrefcount()
Copy link
Member

Choose a reason for hiding this comment

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

Should it be in the loop?

'debug build required')
def test_copy_global_refcount(self):
# See issue #34042 for more details.
first_ref_count = second_ref_count = None
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason of this initialization?


for i in range(10):
dct.copy()
first_ref_count = gettotalrefcount()
Copy link
Member

Choose a reason for hiding this comment

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

Why getting the total for each iteration of the loop?

@1st1
Copy link
Member Author

1st1 commented Jul 5, 2018

@vstinner @serhiy-storchaka The test is structured in a weird way because that's the only way how I can get it stable on my machine. And yet, here's a CI failure:

2018-07-05T20:27:40.8742568Z test test_dict failed -- Traceback (most recent call last):
2018-07-05T20:27:40.8766139Z   File "/opt/vsts/work/1/s/Lib/test/test_dict.py", line 318, in test_copy_global_refcount
2018-07-05T20:27:40.8783603Z     self.assertEqual(first_ref_count, second_ref_count)
2018-07-05T20:27:40.8798364Z AssertionError: 344832 != 344838

Any suggestions on how I can write a reliable test for sys.gettotalrefcount()? Maybe commit this fix without a test?

@methane
Copy link
Member

methane commented Jul 6, 2018

Any suggestions on how I can write a reliable test for sys.gettotalrefcount()? Maybe commit this fix without a test?

Since this is debug feature, I'm OK for this instead of maintain cryptic, fragile test.

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Jul 6, 2018

What about using assertAlmostEqual with a delta much less than the number of loops?

        dct = {'a': 1}
        support.gc_collect()
        first_ref_count = sys.gettotalrefcount()
        for i in range(1000):
            dct.copy()
        support.gc_collect()
        second_ref_count = sys.gettotalrefcount()
        self.assertAlmostEqual(first_ref_count, second_ref_count, delta=10)

@vstinner
Copy link
Member

vstinner commented Jul 6, 2018

Please don't use fragile tests. I already have enough issues with existing fragile tests.

+1 to commit without tests.

@vstinner
Copy link
Member

vstinner commented Jul 6, 2018

What about using assertAlmostEqual with a delta much less than the number of loops?

Honestly, Python internals are too complex to get a reliable total reference count. I'm ok to remove the test.

regrtest usually runs a test 3 times to "warmup caches" before checking for reference leaks, and also use many low-level "cleanup" functions to help to get reliable reference counts.

@1st1
Copy link
Member Author

1st1 commented Jul 6, 2018

Please don't use fragile tests. I already have enough issues with existing fragile tests.

I kind of like Serhiy's idea, but on the other hand Victor is the one who spends so much time herding our buildbots. I'll commit this PR without a test as @vstinner and @methane suggested.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

@1st1 1st1 merged commit 0b75228 into python:master Jul 6, 2018
@miss-islington
Copy link
Contributor

Thanks @1st1 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@1st1 1st1 deleted the fixdict branch July 6, 2018 16:20
@bedevere-bot
Copy link

GH-8143 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 6, 2018
miss-islington added a commit that referenced this pull request Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants