Pasquale Carangelo created NIFI-16639:
-----------------------------------------

             Summary: HTTP Site-to-Site Remote Group Port hangs forever in 
HttpClient.close() when the peer stops responding during a send transaction
                 Key: NIFI-16639
                 URL: https://issues.apache.org/jira/browse/NIFI-16639
             Project: Apache NiFi
          Issue Type: Bug
          Components: MiNiFi, NiFi API
    Affects Versions: 2.9.0
            Reporter: Pasquale Carangelo


h3. Summary

With HTTP Site-to-Site, when the remote NiFi node stops responding in the 
middle of a send transaction without closing the connection, the Remote Group 
Port thread blocks forever in {{java.net.http.HttpClient.close()}}. No error is 
logged, the port is never penalized or yielded, the flow shows as running, and 
sending never resumes, even after the remote node is back. Only a restart of 
the client unblocks the port.

The communications timeout of the Remote Process Group (30 seconds) does not 
protect from this.

This is the HTTP counterpart of what NIFI-16624 describes for RAW: a silent 
peer blocks the Remote Group Port task thread beyond the communications 
timeout. Here the block has no upper bound.

h3. Environment

* Client: MiNiFi Java 2.9.0 ({{nifi-site-to-site-client-2.9.0.jar}}), Java 
21.0.10, Linux
* Server: NiFi 2.9.0, 2-node cluster on Kubernetes, behind ingress-nginx 
(client to ingress over HTTP/2, proxy read timeout 600 seconds)
* Remote Process Group: transport protocol HTTP, communications timeout 30 sec, 
remote input port with 1 concurrent task, batch size 400 KB / batch duration 5 
sec
* 15 MiNiFi instances sending to the same input port

h3. What happened

The Kubernetes node hosting one of the two NiFi pods froze for about 4 minutes 
(memory pressure, kubelet down). The pod was not terminated, so its connections 
were not closed. It was recreated on another node about 10 minutes later.

* 2 of the 15 MiNiFi instances had a send transaction open towards that node at 
that instant. Both stopped sending and never resumed. The other 13 recovered on 
their own.
* On the 2 blocked instances the Remote Process Group kept refreshing normally 
(flow contents and peer list of 2 peers), but the Remote Group Port logged 
neither a successful send nor an error from that moment on.
* About 13 hours later the client still had one TCP connection to the ingress 
in state ESTABLISHED: request body fully sent and acknowledged, nothing 
received after the TLS/HTTP2 setup, no FIN, no RST. The 504 that ingress-nginx 
produced after its 600 seconds read timeout never reached the client, because 
the idle flow had been dropped silently on the network path in the meantime.
* FlowFiles accumulated in the queue in front of the port until MiNiFi was 
restarted.

Earlier the same day the other NiFi node failed in a way that closed its 
connections: the client got {{IOException: Read end dead}} and a 503 from 
nginx, and sent to the other node one second later. So the ordinary failure 
path works; the hang needs a peer that goes silent.

h3. Thread dump

Taken about 11 hours after the event, identical on both instances. The 
Timer-Driven thread of the port:

{code}
java.lang.Thread.join
jdk.internal.net.http.HttpClientImpl.awaitTermination
java.net.http.HttpClient.close
org.apache.nifi.remote.util.SiteToSiteRestApiClient.close(SiteToSiteRestApiClient.java:166)
org.apache.nifi.remote.protocol.http.HttpClientTransaction.close(HttpClientTransaction.java:172)
org.apache.nifi.remote.AbstractTransaction.error(AbstractTransaction.java:85)
org.apache.nifi.remote.AbstractTransaction.confirm(AbstractTransaction.java:269)
org.apache.nifi.remote.StandardRemoteGroupPort.transferFlowFiles(StandardRemoteGroupPort.java:352)
{code}

It is waiting for the {{HttpClient-SelectorManager}} thread, which is alive in 
{{epoll}} because one request is still in flight.

h3. Root cause

In {{org.apache.nifi.remote.util.SiteToSiteRestApiClient}} 
(nifi-commons/nifi-site-to-site-client), as refactored to 
{{java.net.http.HttpClient}} in NIFI-15214:

# {{openConnectionForSend()}} sends the flow-files POST with 
{{httpClient.sendAsync(request, ...)}} and does not set 
{{requestBuilder.timeout(...)}}. All other requests go through 
{{sendRequest()}}, which sets 
{{requestBuilder.timeout(Duration.ofMillis(readTimeoutMillis))}}.
# {{finishTransferFlowFiles()}} applies the communications timeout only to 
{{transactionFuture.get(readTimeoutMillis, TimeUnit.MILLISECONDS)}}. On 
{{TimeoutException}} it throws an {{IOException}} but does not cancel 
{{transactionFuture}}, so the HTTP exchange stays in flight.
# The exception goes through {{AbstractTransaction.confirm()}} -> {{error()}} 
-> {{HttpClientTransaction.close()}} -> {{SiteToSiteRestApiClient.close()}}, 
which does:
{code:java}
stopExtendingTransaction();
httpClient.shutdown();
httpClient.close();
{code}
On Java 21 {{HttpClient.close()}} is an orderly shutdown that waits, without 
limit, for all in-flight exchanges to complete. The exchange of point 2 never 
completes because nothing ever arrives on its connection, so {{close()}} never 
returns.

Since {{error()}} never returns, the {{IOException}} is never rethrown or 
logged, {{StandardRemoteGroupPort}} never penalizes the peer, and the 
concurrent task of the port is held forever.

The same code is present on tag {{rel/nifi-2.9.0}} and on {{main}} (the file 
differs only by unrelated cleanups from NIFI-16073 and NIFI-16162).

h3. Steps to reproduce

# NiFi 2.9.0 with an input port, reachable over HTTP Site-to-Site, optionally 
through a reverse proxy with a long read timeout.
# A NiFi or MiNiFi 2.9.0 client with a Remote Process Group on transport 
protocol HTTP, sending continuously.
# While a transaction is in flight, make the server go silent without closing 
its connections: {{kill -STOP}} of the NiFi JVM, or a firewall rule that 
silently drops (not rejects) packets between client and server.
# Wait longer than the communications timeout, then restore the server ({{kill 
-CONT}} or remove the rule; if the connection has been dropped by a stateful 
device in between, no response will ever arrive).

Expected: after the communications timeout the port logs the failed 
transaction, penalizes the peer and resumes sending, to another peer or to the 
same one once it is back.

Actual: the port thread stays in {{HttpClient.close()}} with nothing logged. If 
no response, FIN or RST ever reaches the client, it stays there until the 
process is restarted. If one does arrive later (for example a 504 from the 
proxy), the thread is released only then, which can be far beyond the 
communications timeout.

h3. Suggested fix

In {{SiteToSiteRestApiClient}}:

* In {{finishTransferFlowFiles()}}, call {{transactionFuture.cancel(true)}} 
when {{get()}} times out or is interrupted, so the exchange is aborted before 
the transaction is closed.
* In {{close()}}, do not wait without limit: use {{httpClient.shutdownNow()}}, 
or {{shutdown()}} followed by {{awaitTermination(Duration)}} bounded by the 
communications timeout and then {{shutdownNow()}}. The same unbounded 
{{httpClient.close()}} is used in two other places of the class.
* Optionally set a request timeout on the POST of {{openConnectionForSend()}} 
as {{sendRequest()}} does. Note that {{HttpRequest.timeout()}} counts from the 
start of the request, so for this streaming POST it would also limit the 
duration of the upload; the two points above are sufficient on their own.

I can provide a pull request.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to