Skip to content

Commit 30c0a94

Browse files
authored
fix: ensure closing Executor at shutdown in HTTP Deployment (jina-ai#5906)
1 parent 91bfa76 commit 30c0a94

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

jina/orchestrate/deployments/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def __init__(
494494
):
495495
raise RuntimeError(
496496
f'It is not supported to have {ProtocolType.WEBSOCKET.to_string()} deployment for '
497-
f'Deployments with more than one shard'
497+
f'Deployments'
498498
)
499499
is_mac_os = platform.system() == 'Darwin'
500500
is_windows_os = platform.system() == 'Windows'

jina/serve/runtimes/worker/request_handling.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,18 @@ def _http_fastapi_default_app(self,
165165
def call_handle(request):
166166
return self.process_single_data(request, None)
167167

168-
return get_fastapi_app(
168+
app = get_fastapi_app(
169169
request_models_map=request_models_map,
170170
caller=call_handle,
171171
**kwargs
172172
)
173173

174+
@app.on_event('shutdown')
175+
async def _shutdown():
176+
await self.close()
177+
178+
return app
179+
174180
async def _hot_reload(self):
175181
import inspect
176182

@@ -793,12 +799,14 @@ async def endpoint_discovery(self, empty, context) -> jina_pb2.EndpointsProto:
793799
if inner_dict['input']['model'].schema() == legacy_doc_schema:
794800
inner_dict['input']['model'] = legacy_doc_schema
795801
else:
796-
inner_dict['input']['model'] = _create_aux_model_doc_list_to_list(inner_dict['input']['model']).schema()
802+
inner_dict['input']['model'] = _create_aux_model_doc_list_to_list(
803+
inner_dict['input']['model']).schema()
797804

798805
if inner_dict['output']['model'].schema() == legacy_doc_schema:
799806
inner_dict['output']['model'] = legacy_doc_schema
800807
else:
801-
inner_dict['output']['model'] = _create_aux_model_doc_list_to_list(inner_dict['output']['model']).schema()
808+
inner_dict['output']['model'] = _create_aux_model_doc_list_to_list(
809+
inner_dict['output']['model']).schema()
802810
else:
803811
for endpoint_name, inner_dict in schemas.items():
804812
inner_dict['input']['model'] = inner_dict['input']['model'].schema()

tests/integration/docarray_v2/test_v2.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,6 @@ def search(self, docs: DocList[TextDocWithId], **kwargs) -> DocList[ResultTestDo
990990

991991

992992
def test_issue_shards_missmatch_endpoint():
993-
994993
class MyDoc(BaseDoc):
995994
text: str
996995
embedding: NdArray[128]
@@ -1014,3 +1013,25 @@ def foo(self, docs: DocList[MyDoc], **kwargs) -> DocList[MyDocWithMatchesAndScor
10141013
with d:
10151014
res = d.post(on='/', inputs=DocList[MyDoc]([MyDoc(text='hey ha', embedding=np.random.rand(128))]))
10161015
assert len(res) == 1
1016+
1017+
1018+
@pytest.mark.parametrize('protocol', ['grpc', 'http'])
1019+
def test_closing_executor(tmpdir, protocol):
1020+
class ClosingExec(Executor):
1021+
1022+
def __init__(self, file_path, *args, **kwargs):
1023+
super().__init__(*args, **kwargs)
1024+
self._file_path = file_path
1025+
1026+
def close(self) -> None:
1027+
with open(self._file_path, 'w') as f:
1028+
f.write('I closed')
1029+
1030+
file_path = f'{str(tmpdir)}/file.txt'
1031+
d = Deployment(uses=ClosingExec, uses_with={'file_path': file_path}, protocol=protocol)
1032+
with d:
1033+
pass
1034+
1035+
with open(file_path, 'r') as f:
1036+
r = f.read()
1037+
assert r == 'I closed'

0 commit comments

Comments
 (0)