diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java index 5c895cf354..1d655bcf3b 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java @@ -1058,8 +1058,32 @@ private void informWriterError(NHttpClientConnection conn) { } } + /** + * Invoked when the backend terminates the outbound HTTP connection unexpectedly. + * Logs diagnostics, marks the connection CLOSED, shuts it down, and routes the error + * to the standard fault handler. + * + * @param conn the target {@link NHttpClientConnection} that ended input + * @throws IOException if an error occurs while closing the connection + */ public void endOfInput(NHttpClientConnection conn) throws IOException { - conn.close(); + + ProtocolState state = TargetContext.getState(conn); + log.warn("Connection ended unexpectedly" + + ", " + getConnectionLoggingInfo(conn) + + ", State: " + state + "."); + TargetContext.updateState(conn, ProtocolState.CLOSED); + targetConfiguration.getConnections().shutdownConnection(conn, true); + MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx(); + if (state != ProtocolState.RESPONSE_DONE && requestMsgCtx != null) { + requestMsgCtx.setProperty(PassThroughConstants.INTERNAL_EXCEPTION_ORIGIN, + PassThroughConstants.INTERNAL_ORIGIN_ERROR_HANDLER); + targetErrorHandler.handleError(requestMsgCtx, + ErrorCodes.SND_IO_ERROR, + "Error in Sender", + null, + state); + } } public void exception(NHttpClientConnection conn, Exception ex) {