The Maildir.add and Maildir.__setitem__ methods in the mailbox module attempts to change the file mtime after moving the file into the new directory. This allows a race condition since other programs are can move or otherwise modify the file as soon as it is placed in the new directory. If the file is moved from the new directory before the mtime is set this results in an unexpected OSError exception. The fix seems to simply be setting the file mtime before moving the file into its final position.
I can certainly write a patch if wanted. It should be simply moving and modifying two lines in each of the two methods. My understanding is that it should be against 2.7 so it can be applied there first then merged forward? Unfortunately while I can consistently reproduce the problem, with a script using pyinotify, I'm pretty lost how to write a test that would catch this.
Hopefully a 2.7 patch would also apply to 3.3, so yes, start there. For the test, I was thinking that in 3.3+ we could use mock to introduce a delay. But looking at the code again it isn't obvious that there is a meaningful way to do it that is worth the effort required. So let's forget about the test for this one...a comment about why the time set is done before the move would be worthwhile, though.
Janzert: Thanks for the patch. A contributor agreement is not needed for this patch, since it just moves code around, but you might want to submit one in case you make any other contributions. Also let us know what name to use in the Misc/ACKS file.
Thanks for committing the fix. Figured I should finally get it done and signed the online CLA when I submitted this issue. I assume it just takes a while for someone to go through and apply the appropriate flag? This change seems rather insubstantial for an ACKS line so I certainly don't mind waiting. Since a certain Birkenfeld became Brandl I know names are a bit sensitive for some python core devs. So while my personal preference would go to Janzert* the one that fits Python custom is Brian Haskin. * Janzert is the most widely used and distinct identifier for my open source and online activity.
History
Date
User
Action
Args
2022-04-11 14:57:51
admin
set
github: 63237
2013-09-18 16:16:19
janzert
set
messages: +
2013-09-18 12:39:59
r.david.murray
set
status: open -> closedresolution: fixedstage: resolved