diff --git a/mercurial/vfs.py b/mercurial/vfs.py --- a/mercurial/vfs.py +++ b/mercurial/vfs.py @@ -269,6 +269,8 @@ for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror): yield (dirpath[prefixlen:], dirs, files) + threaddata = threading.local() + @contextlib.contextmanager def backgroundclosing(self, ui, expectedcount=-1): """Allow files to be closed asynchronously. @@ -277,19 +279,17 @@ to ``__call__``/``open`` to result in the file possibly being closed asynchronously, on a background thread. """ - # This is an arbitrary restriction and could be changed if we ever - # have a use case. vfs = getattr(self, 'vfs', self) - if getattr(vfs, '_backgroundfilecloser', None): + if getattr(vfs.threaddata, '_backgroundfilecloser', None): raise error.Abort( - _('can only have 1 active background file closer')) + _('can only have 1 active background file closer per thread')) with backgroundfilecloser(ui, expectedcount=expectedcount) as bfc: try: - vfs._backgroundfilecloser = bfc + vfs.threaddata._backgroundfilecloser = bfc yield bfc finally: - vfs._backgroundfilecloser = None + vfs.threaddata._backgroundfilecloser = None class vfs(abstractvfs): '''Operate files relative to a base directory @@ -414,12 +414,12 @@ fp = checkambigatclosing(fp) if backgroundclose: - if not self._backgroundfilecloser: + if not self.threaddata._backgroundfilecloser: raise error.Abort(_('backgroundclose can only be used when a ' 'backgroundclosing context manager is active') ) - fp = delayclosedfile(fp, self._backgroundfilecloser) + fp = delayclosedfile(fp, self.threaddata._backgroundfilecloser) return fp