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

Race condition during pickle.load() #80954

Closed
papadp mannequin opened this issue May 2, 2019 · 5 comments
Closed

Race condition during pickle.load() #80954

papadp mannequin opened this issue May 2, 2019 · 5 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@papadp
Copy link
Mannequin

papadp mannequin commented May 2, 2019

BPO 36773
Nosy @ronaldoussoren, @pitrou, @taleinat, @serhiy-storchaka, @papadp
Files
  • reproduce.tar.gz
  • 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 2019-08-05.08:26:43.132>
    created_at = <Date 2019-05-02.08:24:59.927>
    labels = ['3.7', 'library', 'type-crash']
    title = 'Race condition during pickle.load()'
    updated_at = <Date 2019-08-05.08:26:43.119>
    user = 'https://github.com/papadp'

    bugs.python.org fields:

    activity = <Date 2019-08-05.08:26:43.119>
    actor = 'taleinat'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-05.08:26:43.132>
    closer = 'taleinat'
    components = ['Library (Lib)']
    creation = <Date 2019-05-02.08:24:59.927>
    creator = 'papad'
    dependencies = []
    files = ['48294']
    hgrepos = []
    issue_num = 36773
    keywords = []
    message_count = 5.0
    messages = ['341257', '341265', '341424', '341431', '349035']
    nosy_count = 5.0
    nosy_names = ['ronaldoussoren', 'pitrou', 'taleinat', 'serhiy.storchaka', 'papad']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue36773'
    versions = ['Python 3.7']

    @papadp
    Copy link
    Mannequin Author

    papadp mannequin commented May 2, 2019

    There seems to be a race condition when unpickling the same object from different threads (My guess is it's related to imports that are related to the unpickled object).

    I have used the following files for reproduction (File contents are in the attached archive, also pasted at the bottom of the issue report):
    import_me.py - Containing the object we will pickle
    pickle_object.py - the code to pickle the object
    trigger.py - a piece of code to unpickle the object from different threads simultaneously and trigger the crash

    I have used the files in the attached archive (containing the 3 files mentioned above)

    to reproduce run the following commands:

    pickle_object.py
    trigger.py

    Running trigger.py will crash on about 50% of the runs with the following error:

    Traceback (most recent call last):
      File "./trigger.py", line 16, in pickle_load_thread
        pickle.load(h)
    AttributeError: Can't get attribute 'PickleMe' on <module 'import_me' from '/data/import_me.py'>

    I have tested this on the following software stacks:

    1. python:3.7.3 docker

    2. my laptop

    I'm uncertain if this is a bug related to pickle or python module importing.
    Similar issues I've found: https://bugs.python.org/issue12680 and https://bugs.python.org/issue30891.

    ---------------------------------------------------------------------------------------
    filename: import_me.py
    #! /usr/bin/python3

    class PickleMe(object):
    
        def __init__(self):
            self.a = "a"

    filename: pickle_object.py
    #! /usr/bin/python3

    import pickle
    from import_me import PickleMe
    
    p = PickleMe()
    
    with open('pickled', 'wb') as h:
        pickle.dump(p, h)

    filename: trigger.py
    #! /usr/bin/python3

    import threading
    import pickle
    
    import logging
    
    threads = []
    
    def pickle_load_thread():
    
        logging.error("Thread %d loading", threading.get_ident())
    try:
        with open('pickled', 'rb') as h:
            pickle.load(h)
    except:
        logging.exception("Exception in loading")
    
    def start_pickle_load_thread():
    
        for x in range(2):
            load_thread = threading.Thread(target=pickle_load_thread)
            load_thread.daemon = True
            threads.append(load_thread)
    
        for x in threads:
            x.start()
    
    
    if __name__ == '__main__':
    
        start_pickle_load_thread()
    
        for t in threads:
            t.join()

    @papadp papadp mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels May 2, 2019
    @ronaldoussoren
    Copy link
    Contributor

    This could be a duplicate of bpo-34572

    @papadp
    Copy link
    Mannequin Author

    papadp mannequin commented May 5, 2019

    In what version should this be fixed? I see https://bugs.python.org/issue34572 says the fix is in version 3.7, while I'm experiencing this on python 3.7.3, which version should I check?

    @serhiy-storchaka
    Copy link
    Member

    @taleinat
    Copy link
    Contributor

    taleinat commented Aug 5, 2019

    On Windows 10, this reproduces consistently with 3.6 and 3.7.0, but not with 3.7.4, 3.8.0b3 and current master. So this definitely seems to be fixed.

    @taleinat taleinat closed this as completed Aug 5, 2019
    @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
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants