Skip to content

workaround fork for CRT #4473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions boto3/crt.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ def get_crt_s3_client(client, config):
global CRT_S3_CLIENT
global BOTOCORE_CRT_SERIALIZER

import os
import gc
from awscrt.common import join_all_native_threads

def before_fork():
global CRT_S3_CLIENT
try:
if CRT_S3_CLIENT is not None:
# The client is not safe to use after fork, so we need to release it.
# make sure the client is shutdown properly before fork
# also wait for every thread to be joined, incase of some thread is in the middle of cleanup.
CRT_S3_CLIENT = None
gc.collect()
if join_all_native_threads(timeout_sec=0.5) is False:
# generate warning about the background threads still runnning.
# It's possible when CRT used spearately from user, but it's user's responsbility to clean those up.
print("WARNING: Background threads still running, dead lock or crash could happen. Check for CRT resources held before forking.")

except:
exit(-1)


with CLIENT_CREATION_LOCK:
if CRT_S3_CLIENT is None:
serializer, s3_client = _initialize_crt_transfer_primatives(
Expand All @@ -104,6 +126,10 @@ def get_crt_s3_client(client, config):
BOTOCORE_CRT_SERIALIZER = serializer
CRT_S3_CLIENT = s3_client

os.register_at_fork(before=before_fork)
# Only register the handler once.
if not getattr(get_crt_s3_client, '_fork_handler_registered', False):
get_crt_s3_client._fork_handler_registered = True
return CRT_S3_CLIENT


Expand Down