comparison mercurial/vfs.py @ 51883:1edac12af730

vfs: modernize the detection of the main thread There weren't a lot of good choices when py27 was supported, but starting with py34, `threading.main_thread()` is available. This gets us away from an undocumented, internal symbol, and drops a pytype suppression statement. It is also apparently no longer reliable after a process fork.[1][2] [1] https://stackoverflow.com/a/23207116 [2] https://github.com/python/cpython/blob/v3.6.3/Lib/threading.py#L1334
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 19 Sep 2024 21:03:10 -0400
parents f4733654f144
children f79f98733a5b
comparison
equal deleted inserted replaced
51882:2391a5fa111e 51883:1edac12af730
371 asynchronously, on a background thread. 371 asynchronously, on a background thread.
372 """ 372 """
373 # Sharing backgroundfilecloser between threads is complex and using 373 # Sharing backgroundfilecloser between threads is complex and using
374 # multiple instances puts us at risk of running out of file descriptors 374 # multiple instances puts us at risk of running out of file descriptors
375 # only allow to use backgroundfilecloser when in main thread. 375 # only allow to use backgroundfilecloser when in main thread.
376 if not isinstance( 376 if threading.current_thread() is not threading.main_thread():
377 threading.current_thread(),
378 threading._MainThread, # pytype: disable=module-attr
379 ):
380 yield 377 yield
381 return 378 return
382 vfs = getattr(self, 'vfs', self) 379 vfs = getattr(self, 'vfs', self)
383 if getattr(vfs, '_backgroundfilecloser', None): 380 if getattr(vfs, '_backgroundfilecloser', None):
384 raise error.Abort( 381 raise error.Abort(
573 ) 570 )
574 % mode 571 % mode
575 ) 572 )
576 fp = checkambigatclosing(fp) 573 fp = checkambigatclosing(fp)
577 574
578 if backgroundclose and isinstance( 575 if (
579 threading.current_thread(), 576 backgroundclose
580 threading._MainThread, # pytype: disable=module-attr 577 and threading.current_thread() is threading.main_thread()
581 ): 578 ):
582 if ( 579 if (
583 not self._backgroundfilecloser # pytype: disable=attribute-error 580 not self._backgroundfilecloser # pytype: disable=attribute-error
584 ): 581 ):
585 raise error.Abort( 582 raise error.Abort(