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

Removing Popen log files in threads is racy on Windows #77550

Open
pbos mannequin opened this issue Apr 26, 2018 · 3 comments
Open

Removing Popen log files in threads is racy on Windows #77550

pbos mannequin opened this issue Apr 26, 2018 · 3 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@pbos
Copy link
Mannequin

pbos mannequin commented Apr 26, 2018

BPO 33369
Nosy @pfmoore, @tjguk, @pbos, @zware, @zooba
Files
  • racy_windows.py: Repro script
  • 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 = None
    created_at = <Date 2018-04-26.22:40:56.117>
    labels = ['type-bug', 'OS-windows']
    title = 'Removing Popen log files in threads is racy on Windows'
    updated_at = <Date 2018-04-27.16:56:30.455>
    user = 'https://github.com/pbos'

    bugs.python.org fields:

    activity = <Date 2018-04-27.16:56:30.455>
    actor = 'pbos'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Windows']
    creation = <Date 2018-04-26.22:40:56.117>
    creator = 'pbos'
    dependencies = []
    files = ['47552']
    hgrepos = []
    issue_num = 33369
    keywords = []
    message_count = 3.0
    messages = ['315822', '315825', '315850']
    nosy_count = 5.0
    nosy_names = ['paul.moore', 'tim.golden', 'pbos', 'zach.ware', 'steve.dower']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33369'
    versions = ['Python 2.7']

    @pbos
    Copy link
    Mannequin Author

    pbos mannequin commented Apr 26, 2018

    OS: Windows 10, version 1607 (OS Build 14393.2189)
    Python --version: Python 2.7.3

    Removing logfiles files just after Popen().wait() outside an open() block is racy on Windows. This seems to require multiple parallel workers to pull off, not sure where the synchronization issue lies (interpreter or os).

    This report is spun off from the following pull request where we ran into the issue (we always log to log files instead of memory pipes but sometimes discard them after the worker has finished): google/gtest-parallel#59 (comment)

    Attaching repro script, in it essentially all workers do this:

      command = ['ping', '-n', '1', '127.0.0.1']
      with open(file_name, 'w') as file_obj:
        task = subprocess.Popen(command, stdout=file_obj, stderr=file_obj)
        task.wait()

    print 'Removing %s\n' % file_name
    os.remove(file_name)

    Here os.remove() fails fairly often with an exception even though there should be no handles left to the file: WindowsError: [Error 32] The process cannot access the file because it is being used by another process: '25436.log'

    Run (this seems to repro pretty reliably, more than half of the time on my machine):

    C:\src>del *.log

    C:\src>python racy_windows.py
    Logging to 24788.log
    Logging to 1052.log

    Logging to 32320.log
    Logging to 29316.log

    Removing 1052.log

    Exception in thread Thread-2:
    Traceback (most recent call last):
      File "C:\python_27_amd64\files\lib\threading.py", line 551, in __bootstrap_inner
        self.run()
      File "C:\python_27_amd64\files\lib\threading.py", line 504, in run
        self.__target(*self.__args, **self.__kwargs)
      File "racy_windows.py", line 19, in __call__
        os.remove(file_name)
    WindowsError: [Error 32] The process cannot access the file because it is being used by another process: '1052.log'

    Removing 24788.log

    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "C:\python_27_amd64\files\lib\threading.py", line 551, in __bootstrap_inner
        self.run()
      File "C:\python_27_amd64\files\lib\threading.py", line 504, in run
        self.__target(*self.__args, **self.__kwargs)
      File "racy_windows.py", line 19, in __call__
        os.remove(file_name)
    WindowsError: [Error 32] The process cannot access the file because it is being used by another process: '24788.log'

    Removing 29316.log
    Removing 32320.log

    @pbos pbos mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Apr 26, 2018
    @zware
    Copy link
    Member

    zware commented Apr 27, 2018

    You will get no support for Python 2.7.3 out of us (that patch release is over 6 years old! At least try 2.7.14, or wait a week for 2.7.15) and most of us have lost interest in 2.7 altogether, particularly on Windows. However, this sounds very much like other issues I've seen around the timing of Windows file handle handling. Try adding a retry loop with a short sleep around your os.remove call.

    @pbos
    Copy link
    Mannequin Author

    pbos mannequin commented Apr 27, 2018

    Wow that's old, yeah that's fair (I wouldn't expect this to be backported to any old releases).

    Our current "workaround" is to try os.remove, sleep, repeat X times. This seems still racy and not something we'd like to have in our script if it can be resolved upstream. Retried on 2.7.15rc1:

    C:\src>del *.log
    Could Not Find C:\src\*.log

    C:\src>C:\Python2.7.15rc1\python.exe --version
    Python 2.7.15rc1

    C:\src>C:\Python2.7.15rc1\python.exe racy_windows.py
    Logging to 5656.log

    Logging to 13068.log

    Logging to 27620.log

    Logging to 18384.log

    Removing 5656.log

    ERxception in thread Thread-1:
    Traceback (most recent call last):
      File "C:\Python2.7.15rc1\lib\threading.py", line 801, in __bootstrap_inner
        self.run()
      File "C:\Python2.7.15rc1\lib\threading.py", line 754, in run
        self.__target(*self.__args, **self.__kwargs)
      File "racy_windows.py", line 19, in __call__
        os.remove(file_name)
    WindowsError: [Error 32] The process cannot access the file because it is being used by another process: '5656.log'
    emoving 13068.log
    R
    emoving 27620.log
    
    
    Exception in thread Thread-3:
    Traceback (most recent call last):
      File "C:\Python2.7.15rc1\lib\threading.py", line 801, in __bootstrap_inner
        self.run()
      File "C:\Python2.7.15rc1\lib\threading.py", line 754, in run
        self.__target(*self.__args, **self.__kwargs)
      File "racy_windows.py", line 19, in __call__
        os.remove(file_name)
    WindowsError: [Error 32] The process cannot access the file because it is being used by another process: '27620.log'
    Exception in thread Thread-2:
    Traceback (most recent call last):
      File "C:\Python2.7.15rc1\lib\threading.py", line 801, in __bootstrap_inner
        self.run()
      File "C:\Python2.7.15rc1\lib\threading.py", line 754, in run
        self.__target(*self.__args, **self.__kwargs)
      File "racy_windows.py", line 19, in __call__
        os.remove(file_name)
    WindowsError: [Error 32] The process cannot access the file because it is being used by another process: '13068.log'

    Removing 18384.log

    @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
    OS-windows type-bug An unexpected behavior, bug, or error
    Projects
    Status: No status
    Development

    No branches or pull requests

    1 participant