From a1ecc42d9f9214e8b2289a226861f6e6eb47fc4f Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Mon, 9 Dec 2024 22:09:47 +0300 Subject: [PATCH 1/4] fix: Handle failure to reset request body streams after writing request body --- .../com/microsoft/kiota/http/OkHttpRequestAdapter.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index b634cf7da..7083b3644 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -904,7 +904,15 @@ public long contentLength() throws IOException { public void writeTo(@Nonnull BufferedSink sink) throws IOException { sink.writeAll(Okio.source(requestInfo.content)); if (!isOneShot()) { - requestInfo.content.reset(); + try { + requestInfo.content.reset(); + } catch (Exception ex) { + spanForAttributes.recordException(ex); + // we don't want to fail the request if reset() fails + // reset() was a measure to prevent draining the request + // body by an interceptor before + // the final network request + } } } }; From 597e55429c5141996e2aab76bfdb8c05cb075eb5 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Mon, 9 Dec 2024 23:01:34 +0300 Subject: [PATCH 2/4] Use content length to set a mark before attempting the reset --- .../java/com/microsoft/kiota/http/OkHttpRequestAdapter.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index 7083b3644..69b7b8049 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -902,9 +902,15 @@ public long contentLength() throws IOException { @Override public void writeTo(@Nonnull BufferedSink sink) throws IOException { + // stored in variable before writing to the sink due to + // available()'s definition + long contentLength = contentLength(); sink.writeAll(Okio.source(requestInfo.content)); if (!isOneShot()) { try { + if (contentLength > 0) { + requestInfo.content.mark((int) contentLength); + } requestInfo.content.reset(); } catch (Exception ex) { spanForAttributes.recordException(ex); From 1241af82bc59a025e3a5ad09bf74ad01b4ac1413 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Mon, 9 Dec 2024 23:34:53 +0300 Subject: [PATCH 3/4] Set a mark at the beginning of the stream before reading it --- .../java/com/microsoft/kiota/http/OkHttpRequestAdapter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index 69b7b8049..fc292222b 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -905,12 +905,12 @@ public void writeTo(@Nonnull BufferedSink sink) throws IOException { // stored in variable before writing to the sink due to // available()'s definition long contentLength = contentLength(); + if (contentLength() > 0) { + requestInfo.content.mark((int) contentLength); + } sink.writeAll(Okio.source(requestInfo.content)); if (!isOneShot()) { try { - if (contentLength > 0) { - requestInfo.content.mark((int) contentLength); - } requestInfo.content.reset(); } catch (Exception ex) { spanForAttributes.recordException(ex); From 2bf7173f321fde91f86929928b6c6c4dabe6fdd2 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Mon, 9 Dec 2024 23:39:52 +0300 Subject: [PATCH 4/4] Remove comment --- .../java/com/microsoft/kiota/http/OkHttpRequestAdapter.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index fc292222b..9408d1e1c 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -902,10 +902,8 @@ public long contentLength() throws IOException { @Override public void writeTo(@Nonnull BufferedSink sink) throws IOException { - // stored in variable before writing to the sink due to - // available()'s definition long contentLength = contentLength(); - if (contentLength() > 0) { + if (contentLength > 0) { requestInfo.content.mark((int) contentLength); } sink.writeAll(Okio.source(requestInfo.content));