0

I am reading a file with minio and have a REST controller that returns the inputstream given by minio as an InputStreamResource. Here is my code:

@GetMapping("/download")
fun download(): ResponseEntity<InputStreamResource> {

  // read file from minio
  // getObjectResponse is an InputStream
  ...
  val getObjectResponse = minioClient.getObject(getObjectArgs)

  return ResponseEntity.ok().body(InputStreamResource(getObjectResponse))
}

According to this question wrapping an InputStream into a InputStreamResource is correct, and spring is supposed to close the underlying InputStream after the reponse is delivered. Yet I still get the infamous

okhttp3.OkHttpClient: A connection to ... was leaked. Did you forget to close a response body?

What are my options here? I would rather not need to copy and buffer the minio content into memory as these files tend to be very large.

schrom
  • 1,372
  • 1
  • 22
  • 36

2 Answers2

0

As long as you close any of Response, ResponseBody or a stream like ResponseBody.inputStream() then the OkHttp response will be cleaned up.

You are reliant on the caller who accepts your InputStreamResource to close it correctly.

I'd suggest wrapping/decorating the input stream you pass into InputStreamResource and logging when and where call is closed.

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
0

By using Yuri Schimkes suggestion to improve logging and some debugging I found out that spring is closing the inputstream only when a HTTP 200 is returned and the inpustream is actually read and delivered. In my case sometimes caching happened (spring magic with etags) and spring returned a HTTP 304 without consuming and closing the inputstream.

schrom
  • 1,372
  • 1
  • 22
  • 36