[PATCH 3/3] UIDMaps: correctly protect from concurrent writes

Nicolas Sebrecht nicolas.s-dev at laposte.net
Wed Jul 6 00:09:49 BST 2016


On Tue, Jul 05, 2016 at 11:38:14PM +0200, Łukasz Żarnowiecki wrote:
> > --- a/offlineimap/folder/UIDMaps.py
> > +++ b/offlineimap/folder/UIDMaps.py
> > @@ -78,10 +78,19 @@ class MappedIMAPFolder(IMAPFolder):
> >  
> >      def _savemaps(self):
> >          mapfilename = self._getmapfilename()
> > -        with open(mapfilename + ".tmp", 'wt') as mapfilefd:
> > -            for (key, value) in self.diskl2r.items():
> > -                mapfilefd.write("%d:%d\n"% (key, value))
> > -        os.rename(mapfilename + '.tmp', mapfilename)
> > +        mapfilenamelock = "%s.lock"% mapfilename
> > +        with open(mapfilenamelock, 'w') as mapfilelock:
> > +            # The "account" lock already prevents from multiple access by
> > +            # different processes. However, we still need to protect for
> > +            # multiple access from different threads.
> > +            try:
> > +                fnctl.lockf(mapfilelock, fnctl.LOCK_EX) # Blocks until acquired.
> > +            except NameError:
> > +                pass # Windows...
> > +            with open(mapfilename, 'wt') as mapfilefd:
> > +                for (key, value) in self.diskl2r.items():
> 
> Not necessary parenthesis.

Yep. Thanks for your reviews!

-- 
Nicolas Sebrecht




More information about the OfflineIMAP-project mailing list