title: OpenShift Kestral causing Socket Hangup error
body: <p>We have containerized .NET Core application and running in OpenShift.</p>
  <p>While running our application from UI we are seeing an error</p>
  <blockquote>
  <p>Underlying connection was closed</p>
  </blockquote>
  <p>Our API is registered with WSO2 and when I run our application using WSO2 gateway through PostMan I see SocketHang Up Error. Below is the error Log</p>
  <pre><code>ID: [] [] [2021-11-12 20:07:17,341] ERROR {org.apache.synapse.transport.passthru.SourceHandler} -  Content-Length header already present {org.apache.synapse.transport.passthru.SourceHandler}
  org.apache.http.ProtocolException: Content-Length header already present
                    at org.apache.http.protocol.ResponseContent.process(ResponseContent.java:101)
                    at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:139)
                    at org.apache.synapse.transport.passthru.SourceResponse.start(SourceResponse.java:179)
                    at org.apache.synapse.transport.passthru.SourceHandler.responseReady(SourceHandler.java:310)
                    at org.apache.http.impl.nio.DefaultNHttpServerConnection.produceOutput(DefaultNHttpServerConnection.java:309)
                    at org.apache.synapse.transport.http.conn.LoggingNHttpServerConnection.produceOutput(LoggingNHttpServerConnection.java:127)
                    at org.apache.synapse.transport.passthru.ServerIODispatch.onOutputReady(ServerIODispatch.java:87)
                    at org.apache.synapse.transport.passthru.ServerIODispatch.onOutputReady(ServerIODispatch.java:39)
                    at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:150)
                    at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:180)
                    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:342)
                    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:316)
                    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:277)
                    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
                    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:586)
                    at java.lang.Thread.run(Thread.java:748)
  TID: [] [] [2021-11-12 20:07:17,342]  INFO {org.apache.synapse.transport.passthru.SourceHandler} -  Writer null when calling informWriterError {org.apache.synapse.transport.passthru.SourceHandler}
  TID: [] [] [2021-11-12 20:07:17,342] ERROR {org.apache.synapse.transport.passthru.SourceHandler} -  Unexpected I/O error: java.nio.channels.ClosedChannelException {org.apache.synapse.transport.passthru.SourceHandler}
  java.nio.channels.ClosedChannelException
                    at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:270)
                    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:461)
                    at org.apache.http.nio.reactor.ssl.SSLIOSession.sendEncryptedData(SSLIOSession.
  
  TID: [] [] [2021-11-12 20:07:17,334] DEBUG {org.apache.synapse.transport.http.headers} -  http-outgoing-14 &lt;&lt; HTTP/1.1 204 No Content {org.apache.synapse.transport.http.headers}
  TID: [] [] [2021-11-12 20:07:17,334] DEBUG {org.apache.synapse.transport.http.headers} -  http-outgoing-14 &lt;&lt; Date: Fri, 12 Nov 2021 12:07:17 GMT {org.apache.synapse.transport.http.headers}
  TID: [] [] [2021-11-12 20:07:17,334] DEBUG {org.apache.synapse.transport.http.headers} -  http-outgoing-14 &lt;&lt; Server: Kestrel {org.apache.synapse.transport.http.headers}
  TID: [] [] [2021-11-12 20:07:17,334] DEBUG {org.apache.synapse.transport.http.headers} -  http-outgoing-14 &lt;&lt; Content-Length: 0 {org.apache.synapse.transport.http.headers}
  TID: [] [] [2021-11-12 20:07:17,334] DEBUG {org.apache.synapse.transport.http.headers} -  http-outgoing-14 &lt;&lt; Set-Cookie: cd1550ebc2ca7854f34f209e62d8deed=a483899cdd89e134e6a0fe6a0f3209f6; path=/; HttpOnly {org.apache.synapse.transport.http.headers}
  TID: [] [] [2021-11-12 20:07:17,334] DEBUG {org.apache.synapse.transport.http.headers} -  http-outgoing-14 &lt;&lt; Cache-control: private {org.apache.synapse.transport.http.headers}
  TID: [] [] [2021-11-12 20:07:17,341] ERROR {org.apache.synapse.transport.passthru.SourceHandler} -  Content-Length header already present {org.apache.synapse.transport.passthru.SourceHandler}
  org.apache.http.ProtocolException: Content-Length header already present
                    at org.apache.http.protocol.ResponseContent.process(ResponseContent.java:101)
  </code></pre>
  <p>Main method in the API</p>
  <pre><code>[HttpGet(&quot;Settings/{identifier}/Timestamp&quot;)]
  public async Task&lt;ActionResult&lt;DateTimeOffset?&gt;&gt; MethodName(string identifier, CancellationToken token)
  {
      return await _service.MethodName(identifier, token);
  }
  
  public async Task&lt;DateTimeOffset?&gt; MethodName(string username, string identifier, CancellationToken token)
  {
      // code the get userSetting from DB
      return userSetting?.somevariable.WithTimeZone(TimeZone);
  }
  
  public static DateTimeOffset WithTimeZone(this DateTime time, TimeZoneInfo timeZone)
  {
      var offset = timeZone.GetUtcOffset(time);
                
      time = time.Kind == DateTimeKind.Unspecified ? time
                              : new DateTime(time.Ticks, DateTimeKind.Unspecified);
      
      return time - DateTime.MaxValue &gt; offset
                          ? DateTimeOffset.MaxValue.ToOffset(offset)
                          : new DateTimeOffset(time, offset);
  }
  </code></pre>
  <p>Below are the possible causes we analyzed</p>
  <ul>
  <li>This API during sometime returns 204 No Content</li>
  <li>Also backend returning a header called Content-Length:0</li>
  <li>This error is caused only when our app is running on OpenShift. In IIS it works fine.</li>
  </ul>
  <p>Is it possible to remove the <code>Content-Length</code> from the response?</p>
  <p>Are there any other causes for this Socket hangup error?</p>