Search code examples
spring-bootokhttpminio

How do I get rid of okhttp3 "A connection to ... was was leaked" warning when returning a minio stream as spring ResponseEntity?


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.


Solution

  • 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.