diff --git a/s3transfer/crt.py b/s3transfer/crt.py index b11c3682..f2897696 100644 --- a/s3transfer/crt.py +++ b/s3transfer/crt.py @@ -11,6 +11,7 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. import logging +import os import re import threading from io import BytesIO @@ -67,6 +68,22 @@ def acquire_crt_s3_process_lock(name): # lock is set as a global so that it is not unintentionally garbage # collected/released if reference of the lock is lost. global CRT_S3_PROCESS_LOCK + + def after_in_child(): + global CRT_S3_PROCESS_LOCK + if CRT_S3_PROCESS_LOCK is not None: + # the lock is not belong to the forked child process. + # Release the lock after fork in child process. + CRT_S3_PROCESS_LOCK.release() + CRT_S3_PROCESS_LOCK = None + + # Check if we've already registered using a function attribute + if not getattr( + acquire_crt_s3_process_lock, '_fork_handler_registered', False + ): + os.register_at_fork(after_in_child=after_in_child) + acquire_crt_s3_process_lock._fork_handler_registered = True + if CRT_S3_PROCESS_LOCK is None: crt_lock = awscrt.s3.CrossProcessLock(name) try: