Skip to content

global objects created in some module are not destroyed when last reference to that module is released #54277

Closed

Description

@ValeryLesin
BPO 10068
Nosy @amauryfa, @pitrou, @vstinner, @benjaminp

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-10-13.01:05:25.377>
created_at = <Date 2010-10-12.10:09:26.450>
labels = ['interpreter-core', 'type-bug']
title = 'global objects created in some module are not destroyed when last reference to that module is released'
updated_at = <Date 2010-10-13.01:05:25.375>
user = 'https://bugs.python.org/ValeryLesin'

bugs.python.org fields:

activity = <Date 2010-10-13.01:05:25.375>
actor = 'benjamin.peterson'
assignee = 'none'
closed = True
closed_date = <Date 2010-10-13.01:05:25.377>
closer = 'benjamin.peterson'
components = ['Interpreter Core']
creation = <Date 2010-10-12.10:09:26.450>
creator = 'Valery.Lesin'
dependencies = []
files = []
hgrepos = []
issue_num = 10068
keywords = []
message_count = 9.0
messages = ['118408', '118412', '118418', '118474', '118475', '118481', '118494', '118495', '118496']
nosy_count = 5.0
nosy_names = ['amaury.forgeotdarc', 'pitrou', 'vstinner', 'benjamin.peterson', 'Valery.Lesin']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10068'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

Activity

  1. ValeryLesin commented on Oct 12, 2010

    ValeryLesinmannequin
    MannequinAuthor

    Interpreter: Python 3.1.2

    Sample:

    ===== first.py =====

    import sys
    import second 
    
    if 'second' in sys.modules:
      print ('in sys modules')
      del sys.modules['second']

    del second

    ===== second.py =====

    class A:
     def __init__(self):
       print('created')
    
     def __del__(self):
       print('destroyed')
    
    a = A()

    Result: 'destroyed' isn't printed
    With Python 2.6.5 it worked fine

  2. added
    interpreter-core
    type-bug
    on Oct 12, 2010
  3. amauryfa commented on Oct 12, 2010

    @amauryfa
    Contributor

    This also reproduces in 2.7.
    2.6 and 2.7 have a different behaviour.

  4. pitrou commented on Oct 12, 2010

    @pitrou
    Member

    Probably bpo-7140, which will disable clearing of the module dict if it's caught in a reference cycle (which it is here, since A.__init__ and A.__del__ will reference it as their globals dict).

  5. benjaminp commented on Oct 12, 2010

    @benjaminp
    Contributor

    Tricky. I think the only way to do this properly is to call _PyModule_Clear when the dict is destroyed. However, there's no good way to flag a dictionary as a "module" dict. Therefore, I propose we remove the Py_REFCNT == 1 guard in module_dealloc, and simply leave as an open bug that modules will clear their dictionaries on deallocation. Thoughts?

  6. pitrou commented on Oct 12, 2010

    @pitrou
    Member

    Therefore, I propose we remove the Py_REFCNT == 1 guard in
    module_dealloc, and simply leave as an open bug that modules will clear
    their dictionaries on deallocation.

    Yes, I think it's the best solution. People can easily copy the dict if they want to keep it around after the module gets destroyed.

  7. benjaminp commented on Oct 12, 2010

    @benjaminp
    Contributor

    r85392.

  8. vstinner commented on Oct 13, 2010

    @vstinner
    Member

    r85393 introduced a regression in test_runpy of Python 2.7.

  9. vstinner commented on Oct 13, 2010

    @vstinner
    Member

    test_runpy fails also on Python 3.2.

  10. benjaminp commented on Oct 13, 2010

    @benjaminp
    Contributor

    r85398

  11. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions