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

Store weak references in modules_by_index #62874

Closed
pitrou opened this issue Aug 6, 2013 · 10 comments
Closed

Store weak references in modules_by_index #62874

pitrou opened this issue Aug 6, 2013 · 10 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@pitrou
Copy link
Member

pitrou commented Aug 6, 2013

BPO 18674
Nosy @loewis, @brettcannon, @ncoghlan, @pitrou, @ericsnowcurrently
Files
  • wr_module_state.patch
  • 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 2013-09-30.20:31:34.748>
    created_at = <Date 2013-08-06.20:38:51.298>
    labels = ['interpreter-core', 'performance']
    title = 'Store weak references in modules_by_index'
    updated_at = <Date 2013-09-30.20:31:34.746>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2013-09-30.20:31:34.746>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-09-30.20:31:34.748>
    closer = 'pitrou'
    components = ['Interpreter Core']
    creation = <Date 2013-08-06.20:38:51.298>
    creator = 'pitrou'
    dependencies = []
    files = ['31180']
    hgrepos = []
    issue_num = 18674
    keywords = ['patch']
    message_count = 10.0
    messages = ['194571', '194583', '194584', '194587', '194595', '194598', '194624', '198611', '198622', '198735']
    nosy_count = 6.0
    nosy_names = ['loewis', 'brett.cannon', 'ncoghlan', 'pitrou', 'sbt', 'eric.snow']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue18674'
    versions = ['Python 3.4']

    @pitrou
    Copy link
    Member Author

    pitrou commented Aug 6, 2013

    modules_by_index is a near-eternal store for extension modules. It is only collected at the end of interpreter shutdown, which is much too late for regular garbage collection. This patch proposes instead to store weak references in modules_by_index, so that extension modules can be collected in a normal way when they are removed from sys.modules.

    The only gotcha is that PyState_FindModule returns a borrowed reference. With this change, it becomes really important to incref the returned reference as soon as possible.

    @pitrou pitrou added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Aug 6, 2013
    @brettcannon
    Copy link
    Member

    Won't that change to PyState_FindModule() break code? And is it part of the stable ABI?

    @pitrou
    Copy link
    Member Author

    pitrou commented Aug 6, 2013

    Theoretically, people *should* already incref the result from PyState_FindModule. On the other hand, the object currently wouldn't be lost unless something else calls PyState_RemoveModule(), which is hardly every used AFAICT.

    The only saving grace is that PyState_FindModule() is py3-specific, and only used for extension modules which have a positive m_size (probably not many of them yet).

    (I think this issue teaches us that borrowed ref-returning APIs are a bad idea)

    Unfortunately, without this change, we also make it difficult or impossible to reclaim extension modules using the GC. At least I cannot think of another way.

    @brettcannon
    Copy link
    Member

    Sounds like it needs to be changed with a notice in What's New.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Aug 7, 2013

    It seems to me that the more appropriate change here would be to redefine PyState_FindModule as return a *new* ref rather than a borrowed ref and have it do the Py_INCREF before returning.

    Code using it would then need to add an appropriate Py_DECREF. A reference leak is generally a less dangerous bug than an early free.

    @pitrou
    Copy link
    Member Author

    pitrou commented Aug 7, 2013

    It seems to me that the more appropriate change here would be to
    redefine PyState_FindModule as return a *new* ref rather than a
    borrowed ref and have it do the Py_INCREF before returning.

    Code using it would then need to add an appropriate Py_DECREF. A
    reference leak is generally a less dangerous bug than an early free.

    I hadn't thought about that. Code must add Py_DECREF only on 3.4+, then.

    @pitrou
    Copy link
    Member Author

    pitrou commented Aug 7, 2013

    (and of course, with module states not being PyObjects, we have the same lifetimes issues as with Py_buffers not being PyObjects....)

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 29, 2013

    I think the "new new module initialization scheme", if it takes steam, is much more promising to solve the underlying issue. I'm inclined to close this entry as "won't fix", what do you think?

    @brettcannon
    Copy link
    Member

    Fine by me.

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 30, 2013

    Ok, rejecting my own patch because of compatibility issues.

    @pitrou pitrou closed this as completed Sep 30, 2013
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants