Title: No garbage collection at end of main thread
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.4
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: François.Trahan, iritkatriel, pitrou
Priority: normal Keywords:

Created on 2015-02-04 21:42 by François.Trahan, last changed 2021-06-16 21:47 by iritkatriel. This issue is now closed.

Messages (3)
msg235406 - (view) Author: François Trahan (François.Trahan) Date: 2015-02-04 21:42
When reaching the end of a script, there is no garbage collection done if another thread is running.

If you have cyclic references between objects that would be elligible for collection under one of which a __del__ would terminate that thread, execution will hang at the end of the "main thread".

We ended up in this situation; adding "gc.collect" at the end of our script solves the problem, but this is a library and it is not reasonnable to assume all our clients will properly ensure every execution path properly forces a garbage collection.

Suggestion: at the end of a thread, or at least main thread, force a collection.
msg235413 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-02-04 23:56
Can you post a simple reproducer so that we can more easily see what you are talking about? Thank you.
msg395933 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-16 15:21
For the use case you describe, you could add a close() function to your library so that callers can make shutdown explicit, or if you want it to remain implicit you could use daemon threads which terminate when the main thread exits.

Relying on GC to release objects which then close non-daemonic threads from __del__ doesn't sound like a robust design to me.
Date User Action Args
2021-06-16 21:47:17iritkatrielsetstatus: open -> closed
resolution: rejected
stage: resolved
2021-06-16 15:21:24iritkatrielsetnosy: + iritkatriel
messages: + msg395933
2015-02-04 23:56:28pitrousetnosy: + pitrou
messages: + msg235413
2015-02-04 21:42:16François.Trahancreate