From 594b76418b604279d5a4e75b9a999388ed59b9cd Mon Sep 17 00:00:00 2001 From: Joonas Bergius Date: Wed, 6 Nov 2024 09:27:41 -0600 Subject: [PATCH] fix: close body before attempting to call finish (#36) Signed-off-by: Joonas Bergius --- net/wasihttp/roundtripper.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/net/wasihttp/roundtripper.go b/net/wasihttp/roundtripper.go index 6819429..848e130 100644 --- a/net/wasihttp/roundtripper.go +++ b/net/wasihttp/roundtripper.go @@ -92,14 +92,23 @@ func (r *Transport) RoundTrip(incomingRequest *http.Request) (*http.Response, er return nil, fmt.Errorf("failed to copy body: %v", err) } + if err := adaptedBody.Close(); err != nil { + return nil, fmt.Errorf("failed to close body: %v", err) + } + outTrailers := types.NewFields() if err := HTTPtoWASIHeader(incomingRequest.Trailer, outTrailers); err != nil { return nil, err } - outFinish := types.OutgoingBodyFinish(*body, cm.Some(outTrailers)) + maybeTrailers := cm.None[types.Fields]() + if len(incomingRequest.Trailer) > 0 { + maybeTrailers = cm.Some(outTrailers) + } + + outFinish := types.OutgoingBodyFinish(*body, maybeTrailers) if outFinish.IsErr() { - return nil, fmt.Errorf("failed to set trailer: %v", outFinish.Err()) + return nil, fmt.Errorf("failed to finish body: %v", outFinish.Err()) } }