Title: shutil.rmtree() FileNotFoundError race condition
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, giampaolo.rodola, iritkatriel, tarek
Priority: normal Keywords: patch

Created on 2019-06-13 01:10 by Jeffrey.Kintscher, last changed 2021-06-20 14:54 by iritkatriel.

Pull Requests
URL Status Linked Edit
PR 14064 open Jeffrey.Kintscher, 2019-06-13 20:00
Messages (3)
msg345445 - (view) Author: Jeffrey Kintscher (Jeffrey.Kintscher) * Date: 2019-06-13 01:10
shutil.rmtree() is susceptible to a race condition that can needlessly raise OSError:

1. os.scandir() returns the list of entries in a directory
2. while iterating over the list, another thread or process deletes one or more of the entries not yet iterated
3. os.unlink() or stat() raises OSError for the already deleted entry

It should check for and ignore, at a minimum, FileNotFoundError because we were going to delete the entry anyways.  For comparison, the 'rm -r' shell command handles this race condition by ignoring entries deleted from under it.

I will submit a PR when I work out some test cases to include.
msg345719 - (view) Author: Jeffrey Kintscher (Jeffrey.Kintscher) * Date: 2019-06-16 00:13
The PR is ready for review.
msg396176 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-20 14:54
see also issue29699
Date User Action Args
2021-06-20 14:54:37iritkatrielsetnosy: + iritkatriel
messages: + msg396176
2019-06-16 00:13:31Jeffrey.Kintschersetmessages: + msg345719
2019-06-13 20:00:07Jeffrey.Kintschersetkeywords: + patch
stage: patch review
pull_requests: + pull_request13924
2019-06-13 01:10:41Jeffrey.Kintschercreate