• Tamar Christina's avatar
    Add delete retry loop. [ci skip] · 1f366b8d
    Tamar Christina authored
    On Windows we have to retry the delete a couple of times.
    The reason for this is that a `FileDelete` command just marks a
    file for deletion. The file is really only removed when the last
    handle to the file is closed. Unfortunately there are a lot of
    system services that can have a file temporarily opened using a shared
    readonly lock, such as the built in AV and search indexer.
    We can't really guarantee that these are all off, so what we can do is
    whenever after a `rmtree` the folder still exists to try again and wait a bit.
    Based on what I've seen from the tests on CI server, is that this is relatively rare.
    So overall we won't be retrying a lot. If after a reasonable amount of time the folder is
    still locked then abort the current test by throwing an exception, this so it won't fail
    with an even more cryptic error.
    The issue is that these services often open a file using `FILE_SHARE_DELETE` permissions.
    So they can seemingly be removed, and for most intended purposes they are, but recreating
    the file with the same name will fail as the FS will prevent data loss.
    The MSDN docs for `DeleteFile` says:
    The DeleteFile function marks a file for deletion on close.
    Therefore, the file deletion does not occur until the last handle
    to the file is closed. Subsequent calls to CreateFile to open the
    file fail with ERROR_ACCESS_DENIED.
    Retrying seems to be a common pattern, SQLite has it in their driver
    The only way to avoid this is to run each way of a test in it's own folder.
    This would also have the added bonus of increased parallelism.
    Reviewers: austin, bgamari
    Reviewed By: bgamari
    Subscribers: thomie, #ghc_windows_task_force
    Differential Revision: https://phabricator.haskell.org/D2936
    GHC Trac Issues: #12661, #13162
testlib.py 69.1 KB