title: Responding with a stream sometimes result in "Connection reset by peer" error
body: <p>In our application we have routes that are streaming JSON documents. Here is an example:</p>
  
  <pre><code>/** GET api/1/tenant/(tenantId)/ads/ */
  def getAllAdsByOwner(advertiserId: AdvertiserId): Route =
    get {
      httpRequiredSession { username =&gt;
        getAllTenantAds(username, advertiserId) { (adSource: Source[AdView, Any]) =&gt;
          complete(adSource)
        }
      }
    }
  </code></pre>
  
  <p>Most of the time it works as expected, but sometimes, especially when there are many simultaneous requests, the server starts resetting connection just after the headers have been sent.
  I tested with a script that requests this route with curl in a loop and aborting if the request failed. It was running for about 2 minutes before stopping. Trace when request fails is the following:</p>
  
  <pre><code>&lt;= Recv header, 17 bytes (0x11)
  0000: HTTP/1.1 200 OK
  &lt;= Recv header, 54 bytes (0x36)
  0000: Access-Control-Allow-Origin: https://&lt;...&gt;
  &lt;= Recv header, 135 bytes (0x87)
  0000: Access-Control-Expose-Headers: Content-Type, Authorization, Refr
  0040: esh-Token, Set-Authorization, Set-Refresh-Token, asset-content-l
  0080: ength
  &lt;= Recv header, 40 bytes (0x28)
  0000: Access-Control-Allow-Credentials: true
  &lt;= Recv header, 24 bytes (0x18)
  0000: Content-Encoding: gzip
  &lt;= Recv header, 23 bytes (0x17)
  0000: X-Frame-Options: DENY
  &lt;= Recv header, 33 bytes (0x21)
  0000: X-Content-Type-Options: nosniff
  &lt;= Recv header, 26 bytes (0x1a)
  0000: Content-Security-Policy: .
  &lt;= Recv header, 20 bytes (0x14)
  0000: default-src 'self';.
  &lt;= Recv header, 63 bytes (0x3f)
  0000: style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;.
  &lt;= Recv header, 59 bytes (0x3b)
  0000: font-src 'self' 'unsafe-inline' https://fonts.gstatic.com;.
  &lt;= Recv header, 99 bytes (0x63)
  0000: script-src 'self' 'unsafe-inline' 'unsafe-eval' https://*.google
  0040: apis.com https://maps.gstatic.com;.
  &lt;= Recv header, 69 bytes (0x45)
  0000: img-src 'self' data: https://*.googleapis.com https://*.gstatic.
  0040: com;.
  &lt;= Recv header, 8 bytes (0x8)
  0000:
  &lt;= Recv header, 26 bytes (0x1a)
  0000: Server: akka-http/10.1.3
  &lt;= Recv header, 37 bytes (0x25)
  0000: Date: Wed, 27 Jun 2018 15:20:24 GMT
  &lt;= Recv header, 28 bytes (0x1c)
  0000: Transfer-Encoding: chunked
  &lt;= Recv header, 32 bytes (0x20)
  0000: Content-Type: application/json
  &lt;= Recv header, 2 bytes (0x2)
  0000:
  == Info: Recv failure: Connection reset by peer
  == Info: stopped the pause stream!
  == Info: Closing connection 0
  curl: (56) Recv failure: Connection reset by peer
  </code></pre>
  
  <p>The same request inspected in Wireshark:</p>
  
  <p><a href="https://user-images.githubusercontent.com/1911270/42023670-7eeccf70-7ad1-11e8-8c49-dfcc4cd10cc1.png" rel="nofollow noreferrer">screen shot</a></p>
  
  <p>Reading logs didn't give any hint about probable source of the problem. Response logged as successful:</p>
  
  <pre><code>[27-06-2018 19:44:52.837][INFO] access: 'GET /api/1/tenant/ca764a91-8616-409c-8f08-c64a40d3fc07/ads' 200 596ms
  </code></pre>
  
  <p>Versions of used software:</p>
  
  <ul>
  <li>Scala: 2.11.11</li>
  <li>akka: 2.5.13</li>
  <li>akka-http: 10.1.3</li>
  </ul>
  
  <p>Configuration:</p>
  
  <ul>
  <li><a href="https://pastebin.com/raw/57PvCPb5" rel="nofollow noreferrer">akka.conf</a></li>
  <li><a href="https://pastebin.com/raw/3TkTvaka" rel="nofollow noreferrer">akka-http-core.conf</a></li>
  </ul>
  
  <p>I tried increasing <code>akka.http.host-connection-pool.max-connections</code> to 128 but it didn't help. Maybe someone has an idea if this is a bug in akka-http or configuration problem?</p>