os.remove… or not

There was a post on python-list yesterday which had someone asking whether, under Win32, an os.remove could return (without an exception) without having removed the file. The OP was experiencing an exception in the following circumstances:

import os
os.remove ("blah.txt")
if os.path.isfile ("blah.txt"):
  raise RuntimeError, "blah.txt still exists"

The initial reaction was that a race condition was the most likely bet, and the OP said that it was possible but unlikely. Eventually, Roger Upole pointed out that it is possible for a delete to succeed without the file actually disappearing — if another process has the file open with FILE_SHARE_DELETE. In that situation the DeleteFile call succeeds but the file won’t actually disappear until the process with the share/delete handle closes the file! It turned out that TortoiseSVN’s cache mechanism was sometimes creating such a handle which meant that the check failed.

Amazing what you learn.