Move _write_result_batch() inside the try ... except block in _serve_unary() to ensure serialization failures are properly handled and returned to the client as structured error batches.
Problem
Currently, _write_result_batch() is called outside the try ... except block in _server.py.
If result serialization fails due to an incompatible return type, shared-memory allocation failure, or external storage upload failure, the exception is not caught.
As a result:
_write_error_batch() is never called.
- The server thread exits the serving loop without sending a response.
- The client encounters an unexpected
StopIteration.
This differs from _app_unary.py, where result serialization is performed inside the try ... except block.
Reproduced Code (Before Fix)
try:
result = getattr(self.implementation_for(info), info.name)(**kwargs)
_validate_result(info.name, result, info.result_type)
except Exception as exc:
_hook_exc = exc
status = "error"
error_type = _log_method_error(
protocol_name, info.name, self._server_id, exc
)
error_message = str(exc)
_write_error_batch(writer, schema, exc, server_id=self._server_id)
return
_write_result_batch(
writer,
info.result_schema,
result,
self._external_config,
shm=shm,
result_type=info.result_type,
)
Proposed Fix
Move _write_result_batch() inside the existing try ... except block so serialization failures are caught and handled through the existing error-handling mechanism.
Fixed Code (After Fix)
try:
result = getattr(self.implementation_for(info), info.name)(**kwargs)
_validate_result(info.name, result, info.result_type)
_write_result_batch(
writer,
info.result_schema,
result,
self._external_config,
shm=shm,
result_type=info.result_type,
)
except Exception as exc:
_hook_exc = exc
status = "error"
error_type = _log_method_error(
protocol_name, info.name, self._server_id, exc
)
error_message = str(exc)
_write_error_batch(writer, schema, exc, server_id=self._server_id)
return
Expected Outcome
- Serialization failures are returned to the client as structured error batches.
- The server avoids prematurely terminating the serving loop due to uncaught exceptions.
- Error handling in
_serve_unary() is consistent with _app_unary.py.
Move
_write_result_batch()inside thetry ... exceptblock in_serve_unary()to ensure serialization failures are properly handled and returned to the client as structured error batches.Problem
Currently,
_write_result_batch()is called outside thetry ... exceptblock in_server.py.If result serialization fails due to an incompatible return type, shared-memory allocation failure, or external storage upload failure, the exception is not caught.
As a result:
_write_error_batch()is never called.StopIteration.This differs from
_app_unary.py, where result serialization is performed inside thetry ... exceptblock.Reproduced Code (Before Fix)
Proposed Fix
Move
_write_result_batch()inside the existingtry ... exceptblock so serialization failures are caught and handled through the existing error-handling mechanism.Fixed Code (After Fix)
Expected Outcome
_serve_unary()is consistent with_app_unary.py.