Skip to content

Server Crash & Client StopIteration on Invalid Unary Return Values #55

Description

@sivasurya05

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions