Skip to content
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

Don't hold a reference to the last job in the thread cache #1639

Merged
merged 3 commits into from
Jun 23, 2020

Conversation

smurfix
Copy link
Contributor

@smurfix smurfix commented Jun 22, 2020

Fixes #1638

@codecov
Copy link

codecov bot commented Jun 22, 2020

Codecov Report

Merging #1639 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1639   +/-   ##
=======================================
  Coverage   99.69%   99.69%           
=======================================
  Files         110      110           
  Lines       13894    13911   +17     
  Branches     1070     1070           
=======================================
+ Hits        13852    13869   +17     
  Misses         27       27           
  Partials       15       15           
Impacted Files Coverage Δ
trio/_core/_thread_cache.py 100.00% <100.00%> (ø)
trio/_core/tests/test_thread_cache.py 100.00% <100.00%> (ø)

@belm0
Copy link
Member

belm0 commented Jun 22, 2020

hard to add test coverage for it?

@smurfix
Copy link
Contributor Author

smurfix commented Jun 22, 2020

Yes. I adapted the test_thread_cache_basics test:

def test_thread_cache_deref():
    res = [False]

    def test_main():
        class deltype(type):
            def __del__(self):
                res[0] = True

        Test = deltype("Test", (object,), {})
        def fn():
            return 42

        q = Queue()
        def deliver(outcome):
            q.put(outcome)

        start_thread_soon(fn, deliver)

        outcome = q.get()
        assert outcome.unwrap() == 42
        assert not res[0]
    test_main()
    gc_collect_harder()
    assert res[0]

which unfortunately fails to trigger the problem in the unpatched case.

@njsmith
Copy link
Member

njsmith commented Jun 23, 2020

CI failure is the mysterious #1604, nothing related to this.

Nice catch, and we definitely want this. Can you add a test and newsfragment to the PR, though?

return 42

def __del__(self):
res[0] = True
Copy link
Member

Choose a reason for hiding this comment

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

so this is the instance's destructor, where my original report was on the class destructor

but I see that the bug indeed affects both of these. (I suppose that not destroying the instance is enough to keep a reference to the class?)

does this variant of the test fail before the patch?

Copy link
Member

Choose a reason for hiding this comment

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

does this variant of the test fail before the patch?

I see it does 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't easily reproduce the problem using a class destructor, so I opted for a minimal test that shows the reference to be gone / some destructor to be called.

@smurfix smurfix merged commit 3b18740 into python-trio:master Jun 23, 2020
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.

to_thread.run_sync() foils meta-class destructors
3 participants