Skip to content

threadsafety: enumerate.__reduce__ #153932

Description

@johng

Bug report

Bug description:

During enumerate.reduce there is an unprotected read of ->en_index

result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en->en_index);

TSAN flagged this, likely not a high impact python issue though

import itertools
import threading

en = enumerate(itertools.count())   # infinite inner iterator: next() never ends
stop = threading.Event()


def advance():
    while not stop.is_set():
        next(en)                    # enum_next: atomic write of en_index


def read():
    while not stop.is_set():
        en.__reduce__()             # enum_reduce: plain read of en_index


threads = [threading.Thread(target=advance), threading.Thread(target=read)]
for t in threads:
    t.start()
threading.Event().wait(2.0)
stop.set()
for t in threads:
    t.join()

SUMMARY: ThreadSanitizer: data race enumobject.c:283 in enum_reduce

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions