awslabs / aws-sdk-kotlin

Multiplatform AWS SDK for Kotlin
Apache License 2.0
409 stars 49 forks source link

ByteStreamJVM.writeToOutputStream is documented as not closing the provided stream, but does close it #1444

Open cloudshiftchris opened 3 days ago

cloudshiftchris commented 3 days ago

Describe the bug

ByteStreamJVM.writeToOutputStream is documented as This method does not flush or close the given OutputStream. but does close the stream (indirectly).

outputStream.sink() ultimately creates an okio.OutputStreamSink that has override fun close() = out.close()

We ran across this writing S3 objects (in a loop) to a ZipOutputStream that was closed after the first call to body.writeOutputStream(...).

Regression Issue

Expected behavior

Expecting that writing to a stream does not close it. One may wish to write further data to the stream, as is the case with a ZipOutputStream with multiple entries.

Current behavior

writeOutputStream closes the stream, contradicting the documented behaviour.

Steps to Reproduce

Pass a stream into any ByteStream.writeToOutputStream and check its state after the call.

Possible Solution

Wrap in some form of close-suppressing sink, or a close-suppressing OutputStream.

We worked around this by doing body.toInputStream().copyTo(outputStream) (which isn't great, that InputStream may or may not need to be closed...)

Context

Good for now - pls restore the documented functionality!

AWS SDK for Kotlin version

1.3.52 (unclear where the regression happened, or if this ever worked as documented)

Platform (JVM/JS/Native)

JVM

Operating system and version

any

0marperez commented 2 days ago

Hi, thanks for the report. I've set this as high priority and will begin working on it as soon I'm finished with unrelated work as part of my on call shift.