Skip to content

defaultdict.__missing__ has data races #142495

@nascheme

Description

@nascheme

Bug report

Bug description:

Ideally, defaultdict would provide atomic "compute and add only if key is absent" behavior. When __missing__ is called, another thread might insert the key. Or, it can be inserted between the call to the factory function and the PyObject_SetItem() call.

It would help to call PyDict_SetDefaultRef() so that an existing value is not replaced. That still has the issue that the factory function is called when a value already exists for that key but that is arguably a less serious problem. We should not be replacing an existing value.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions