Skip to content

Commit cffd2e3

Browse files
zak-cloudncAstraea Quinn S
authored andcommitted
fix(lambda-runtime): ensure if a client disconnects while streaming, the handler does not panic
1 parent 1bfa699 commit cffd2e3

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

lambda-runtime/src/requests.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,24 @@ where
109109
let (mut tx, rx) = Body::channel();
110110

111111
tokio::spawn(async move {
112-
tx.send_data(metadata_prelude.into()).await.unwrap();
113-
tx.send_data("\u{0}".repeat(8).into()).await.unwrap();
112+
if tx.send_data(metadata_prelude.clone().into()).await.is_err() {
113+
return;
114+
}
115+
116+
if tx.send_data("\u{0}".repeat(8).into()).await.is_err() {
117+
return;
118+
}
114119

115120
while let Some(chunk) = response.stream.next().await {
116121
let chunk = match chunk {
117122
Ok(chunk) => chunk.into(),
118123
Err(err) => err.into().to_tailer().into(),
119124
};
120-
tx.send_data(chunk).await.unwrap();
125+
126+
if tx.send_data(chunk).await.is_err() {
127+
// Consumer has gone away; nothing else to do.
128+
break;
129+
}
121130
}
122131
});
123132

@@ -206,4 +215,26 @@ mod tests {
206215
None => false,
207216
});
208217
}
218+
219+
#[tokio::test]
220+
async fn streaming_send_data_error_is_ignored() {
221+
use crate::StreamResponse;
222+
223+
let stream = tokio_stream::iter(vec![Ok::<Bytes, Error>(Bytes::from_static(b"chunk"))]);
224+
225+
let stream_response: StreamResponse<_> = stream.into();
226+
let response = FunctionResponse::StreamingResponse(stream_response);
227+
228+
let req: EventCompletionRequest<'_, _, (), _, _, _> = EventCompletionRequest::new("id", response);
229+
230+
let http_req = req.into_req().expect("into_req should succeed");
231+
232+
// immediate drop simulates client disconnection
233+
drop(http_req);
234+
235+
// force the task to run
236+
tokio::task::yield_now().await;
237+
238+
// at this point the inner task will panic if errors are unwrapped.
239+
}
209240
}

0 commit comments

Comments
 (0)