Propagate LinesCodecError in parse_sse_event instead of swallowing it with .ok()? #8

Closed
opened 2026-10-26 18:11:34 +00:00 by andreban · 0 comments
Owner

Summary

In src/client.rs, GeminiClient::parse_sse_event converts Err(LinesCodecError) into None via .ok()? inside .filter_map(...):

fn parse_sse_event(
    event_result: std::result::Result<ServerSentEvent, tokio_util::codec::LinesCodecError>,
) -> Option<Result<GenerateContentResponseResult>> {
    let data = event_result.map_err(Error::from).ok()?.data?;
    Some(
        serde_json::from_str::<GenerateContentResponse>(&data)
            .map_err(Error::from)
            .and_then(|r| r.into_result()),
    )
}

This is the remaining item from #5 (the decode-loop part was fixed in #7).

Impact

If the HTTP body stream hits a mid-stream transport error (connection reset, read timeout), stream_response maps the reqwest error to an io::Error (src/network/event_source.rs), and FramedRead yields Err(LinesCodecError::Io(_)). The same happens when the body ends partway through an event: ServerSentEventsCodec doesn't override decode_eof, so tokio-util's default returns an I/O error when bytes remain in the buffer at EOF.

(LinesCodecError::MaxLineLengthExceeded can't occur today, since the codec uses LinesCodec::new(), which has no length limit.) Because .ok()? turns that into None, .filter_map(Self::parse_sse_event) silently drops the error and the stream ends as if the model completed normally. Downstream consumers (e.g. VertexModel::generate_stream in devrel-pr-reviewer) then treat a truncated response as a completed turn.

Proposed fix

Propagate the error, and only return None when a valid event has data: None (e.g. an empty keep-alive frame):

fn parse_sse_event(
    event_result: std::result::Result<ServerSentEvent, tokio_util::codec::LinesCodecError>,
) -> Option<Result<GenerateContentResponseResult>> {
    match event_result {
        Ok(event) => {
            let data = event.data?;
            Some(
                serde_json::from_str::<GenerateContentResponse>(&data)
                    .map_err(Error::from)
                    .and_then(|r| r.into_result()),
            )
        }
        Err(err) => Some(Err(Error::from(err))),
    }
}
## Summary In `src/client.rs`, `GeminiClient::parse_sse_event` converts `Err(LinesCodecError)` into `None` via `.ok()?` inside `.filter_map(...)`: ```rust fn parse_sse_event( event_result: std::result::Result<ServerSentEvent, tokio_util::codec::LinesCodecError>, ) -> Option<Result<GenerateContentResponseResult>> { let data = event_result.map_err(Error::from).ok()?.data?; Some( serde_json::from_str::<GenerateContentResponse>(&data) .map_err(Error::from) .and_then(|r| r.into_result()), ) } ``` This is the remaining item from #5 (the decode-loop part was fixed in #7). ## Impact If the HTTP body stream hits a mid-stream transport error (connection reset, read timeout), `stream_response` maps the `reqwest` error to an `io::Error` (`src/network/event_source.rs`), and `FramedRead` yields `Err(LinesCodecError::Io(_))`. The same happens when the body ends partway through an event: `ServerSentEventsCodec` doesn't override `decode_eof`, so tokio-util's default returns an I/O error when bytes remain in the buffer at EOF. (`LinesCodecError::MaxLineLengthExceeded` can't occur today, since the codec uses `LinesCodec::new()`, which has no length limit.) Because `.ok()?` turns that into `None`, `.filter_map(Self::parse_sse_event)` silently drops the error and the stream ends as if the model completed normally. Downstream consumers (e.g. `VertexModel::generate_stream` in `devrel-pr-reviewer`) then treat a truncated response as a completed turn. ## Proposed fix Propagate the error, and only return `None` when a valid event has `data: None` (e.g. an empty keep-alive frame): ```rust fn parse_sse_event( event_result: std::result::Result<ServerSentEvent, tokio_util::codec::LinesCodecError>, ) -> Option<Result<GenerateContentResponseResult>> { match event_result { Ok(event) => { let data = event.data?; Some( serde_json::from_str::<GenerateContentResponse>(&data) .map_err(Error::from) .and_then(|r| r.into_result()), ) } Err(err) => Some(Err(Error::from(err))), } } ```
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: andreban/gemini-rs#8