From 20715b6e5dda6705d95aea196207c576ad3f0d27 Mon Sep 17 00:00:00 2001
From: Tiangang Song <git@bitsea.link>
Date: Thu, 14 Sep 2023 13:16:19 -0700
Subject: [PATCH] Fix runtime_client blocking main thread

runtime_client.next is calling into the C extension which blocks the
main thread. Moving it to a separate thread enables the main thread
to process signal, see this issue for more details: #105
---
 awslambdaric/lambda_runtime_client.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/awslambdaric/lambda_runtime_client.py b/awslambdaric/lambda_runtime_client.py
index 2066f6c..b268abc 100644
--- a/awslambdaric/lambda_runtime_client.py
+++ b/awslambdaric/lambda_runtime_client.py
@@ -5,6 +5,7 @@
 import http
 import http.client
 import sys
+from concurrent.futures import ThreadPoolExecutor
 from awslambdaric import __version__
 
 
@@ -65,7 +66,9 @@ def post_init_error(self, error_response_data):
             raise LambdaRuntimeClientError(endpoint, response.code, response_body)
 
     def wait_next_invocation(self):
-        response_body, headers = runtime_client.next()
+        with ThreadPoolExecutor() as e:
+            fut = e.submit(runtime_client.next)
+        response_body, headers = fut.result()
         return InvocationRequest(
             invoke_id=headers.get("Lambda-Runtime-Aws-Request-Id"),
             x_amzn_trace_id=headers.get("Lambda-Runtime-Trace-Id"),