[Fix] Fix data race risk of cache_randomness (#2927)

This commit is contained in:
Haobo Yuan 2023-09-07 15:20:20 +08:00 committed by GitHub
parent c0268ad974
commit 4f65f91db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved. # Copyright (c) OpenMMLab. All rights reserved.
import copy
import functools import functools
import inspect import inspect
import weakref import weakref
@ -80,7 +81,10 @@ class cache_randomness:
def __get__(self, obj, cls): def __get__(self, obj, cls):
self.instance_ref = weakref.ref(obj) self.instance_ref = weakref.ref(obj)
return self # Return a copy to avoid multiple transform instances sharing
# one `cache_randomness` instance, which may cause data races
# in multithreading cases.
return copy.copy(self)
def avoid_cache_randomness(cls): def avoid_cache_randomness(cls):