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

gh-116915: Make _thread._ThreadHandle support GC #116934

Merged
merged 1 commit into from Mar 18, 2024

Conversation

mpage
Copy link
Contributor

@mpage mpage commented Mar 17, 2024

Even though it has no internal references to Python objects it still has a reference to its type by virtue of being a heap type. We need to provide a traverse function that visits the type, but we do not need to provide a clear function.

Even though it has no internal references to Python objects it still
has a reference to its type by virtue of being a heap type. We need
to provide a traverse function that visits the type, but we do not
need to provide a clear function.
@Fidget-Spinner
Copy link
Member

Fidget-Spinner commented Mar 17, 2024

Would a fix that only decrefs the type in dealloc be sufficient, without supporting GC? That could save some GC cycles.

https://docs.python.org/3/c-api/typeobj.html#c.Py_TPFLAGS_HEAPTYPE

@mpage
Copy link
Contributor Author

mpage commented Mar 17, 2024

Would a fix that only decrefs the type in dealloc be sufficient, without supporting GC?

Unfortunately, no. That decref was already present.

@mpage
Copy link
Contributor Author

mpage commented Mar 17, 2024

The CIFuzz (memory) failure is unrelated to this PR (gh-116886)

@mpage mpage marked this pull request as ready for review March 17, 2024 19:59
@mpage mpage requested review from pitrou and colesbury March 17, 2024 19:59
@colesbury colesbury added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Mar 17, 2024
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 2ef088d 🤖

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Mar 17, 2024
@Fidget-Spinner
Copy link
Member

Would a fix that only decrefs the type in dealloc be sufficient, without supporting GC?

Unfortunately, no. That decref was already present.

Huh that's really strange. Usually cycles between types and the object itself are special. Will think about this.

@Fidget-Spinner
Copy link
Member

Ah right I forgot that heap types need to support GC as well. This can be seen in previous work. In this case, this LGTM.

E.g.
https://github.com/python/cpython/pull/23136/files

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

+1. Let's wait for the refleaks builders to be become green again and then merge this.

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

It seems there is indeed a reference cycle between heap types and the module object itself

res->ht_module = Py_XNewRef(module);
.

@colesbury
Copy link
Contributor

Windows 11:

https://buildbot.python.org/all/#/builders/1214/builds/304/steps/4/logs/stdio

======================================================================
FAIL: test_finalize_running_thread (test.test_threading.ThreadTests.test_finalize_running_thread)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "b:\uildarea\pull_request.ware-win11.refleak\build\Lib\test\test_threading.py", line 459, in test_finalize_running_thread
    self.assertEqual(rc, 42)
    ~~~~~~~~~~~~~~~~^^^^^^^^
AssertionError: 3221225477 != 42
----------------------------------------------------------------------
Ran 207 tests in 34.247s

3221225477 is 0xC0000005 or STATUS_ACCESS_VIOLATION

def test_finalize_running_thread(self):
# Issue 1402: the PyGILState_Ensure / _Release functions may be called
# very late on python exit: on deallocation of a running thread for
# example.
if support.check_sanitizer(thread=True):
# the thread running `time.sleep(100)` below will still be alive
# at process exit
self.skipTest("TSAN would report thread leak")
import_module("ctypes")
rc, out, err = assert_python_failure("-c", """if 1:
import ctypes, sys, time, _thread
# This lock is used as a simple event variable.
ready = _thread.allocate_lock()
ready.acquire()
# Module globals are cleared before __del__ is run
# So we save the functions in class dict
class C:
ensure = ctypes.pythonapi.PyGILState_Ensure
release = ctypes.pythonapi.PyGILState_Release
def __del__(self):
state = self.ensure()
self.release(state)
def waitingThread():
x = C()
ready.release()
time.sleep(100)
_thread.start_new_thread(waitingThread, ())
ready.acquire() # Be sure the other thread is waiting.
sys.exit(42)
""")
self.assertEqual(rc, 42)

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.

@vstinner
Copy link
Member

@colesbury: I cannot reproduce the crash on Windows 11.

vstinner@WIN C:\victor\python\main>python -m test test_threading -m test_finalize_running_thread -R 3:200
Running Debug|x64 interpreter...
Using random seed: 3966998252
0:00:00 Run 1 test sequentially
0:00:00 [1/1] test_threading
beginning 203 repetitions. Showing number of leaks (. for 0 or less, X for 10 or more)
123:45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123
XX1 ........................................................................................................................................................................................................
test_threading passed in 1 min 17 sec

== Tests result: SUCCESS ==

1 test OK.

Total duration: 1 min 17 sec
Total tests: run=1 (filtered)
Total test files: run=1/1 (filtered)
Result: SUCCESS

I stressed the test:

  • Windows: 1 terminal: python -m test.bisect_cmd -o bisect test_threading -R 3:3 -v
  • Windows: 2 terminals: python -m test test_threading -m test_finalize_running_thread -R 3:200
  • Windows: 2 terminals: python loop.py -- custom script running the test_finalize_running_thread() script in a loop
  • Linux: ./python -m test test_threading -m test_finalize_running_thread -F -j50

I tried for 15-20 minutes, without reproducing the crash :-(

@vstinner vstinner merged commit b3f0c15 into python:main Mar 18, 2024
53 of 54 checks passed
@vstinner
Copy link
Member

Nice fix @mpage. I confirm that it does fix the leak: #116915 (comment)

@vstinner
Copy link
Member

If anyone see the test_threading.test_finalize_running_thread() crash again, please open a new issue.

vstinner pushed a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
Even though it has no internal references to Python objects it still
has a reference to its type by virtue of being a heap type. We need
to provide a traverse function that visits the type, but we do not
need to provide a clear function.
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
Even though it has no internal references to Python objects it still
has a reference to its type by virtue of being a heap type. We need
to provide a traverse function that visits the type, but we do not
need to provide a clear function.
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
Even though it has no internal references to Python objects it still
has a reference to its type by virtue of being a heap type. We need
to provide a traverse function that visits the type, but we do not
need to provide a clear function.
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.

None yet

6 participants