jira-importer opened a new issue, #999:
URL: https://github.com/apache/httpcomponents-core/issues/999

   **[Nick 
Cabatoff](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=ncabatoff)**
 opened 
**[HTTPCORE-483](https://issues.apache.org/jira/browse/HTTPCORE-483?redirect=false)**
 and commented
   
   I wanted persistent connections with keep-alives, and I discovered that the 
reason I wasn't getting them was because my responses didn't have a 
content-length or transfer-encoding: chunked.  So I did body.setChunked( true 
), passed that to response.setEntity(), and it worked.  But I noticed that my 
app was pegged at 100% CPU in top.  I found that the app was fine when it 
started, but after servicing a single HTTPS request with chunked responses, it 
got stuck in this 100% cpu state.  It turns out that the reactor thread was 
busy looping:  despite there being no connections open after servicing that one 
request, in AbstractIOReactor.execute() the selector.select() call continued to 
return non-zero, and processEvent() always found a key that was writable.  
Stepping through the code I noticed that SSLIOSession.updateEventMask() wasn't 
closing the session:
   
   ```
   // Graceful session termination
   if (this.status == CLOSING && this.sslEngine.isOutboundDone()
           && (this.endOfStream || this.sslEngine.isInboundDone())) {
       this.status = CLOSED;
   }
   ```
   
   The status was CLOSING and endOfStream was true, but 
sslEngine.isOutboundDone() returned false.
   
   I looked at httpcore 5 and found that a new check had been added just above:
   
   ```
   diff --git 
a/httpcore-nio/src/main/java/org/apache/http/nio/reactor/ssl/SSLIOSession.java 
b/httpcore-nio/src/main/java/org/apache/http/nio/reactor/ssl/SSLIOSession.java
   --- 
a/httpcore-nio/src/main/java/org/apache/http/nio/reactor/ssl/SSLIOSession.java
   +++ 
b/httpcore-nio/src/main/java/org/apache/http/nio/reactor/ssl/SSLIOSession.java
   @@ -371,6 +371,11 @@
    
        private void updateEventMask() {
            // Graceful session termination
   +        if (this.status == CLOSING && !this.outEncrypted.hasData()) {
   +            this.sslEngine.closeOutbound();
   +        }
   +
   +        // Graceful session termination
            if (this.status == CLOSING && this.sslEngine.isOutboundDone()
                    && (this.endOfStream || this.sslEngine.isInboundDone())) {
                this.status = CLOSED;
   ```
   
   I applied this patch to my httpcore 4.4.6 and the problem went away.  Now 
after servicing a request with chunked responses the session gets closed 
properly and the reactor is properly idle.
   
   There's still another problem outstanding though.  Request processing now 
consumes way more CPU than it used to.  Doing one request per second without 
chunked responses, my app consumes ~20% of one core; with chunked responses 
it's closer to 100%.
   
   I'm attaching flame graphs (see 
http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html): (1) my starting 
point without chunked respones; (2) with chunked respones; (3) after applying 
the above patch, without chunked responses; (4) after applying patch, with 
chunked responses.  Note the similarity between (2) and (4): in both cases, 
much of the Java code (green) is mostly consumed with epoll calls.
   
   
   ---
   
   **Affects:** 4.4.6
   
   **Attachments:**
   - 
[fg1-initial-unchunked.svg](https://issues.apache.org/jira/secure/attachment/12883344/fg1-initial-unchunked.svg)
 (_1.53 MB_)
   - 
[fg2-initial-chunked.svg](https://issues.apache.org/jira/secure/attachment/12883343/fg2-initial-chunked.svg)
 (_1.90 MB_)
   - 
[fg3-patched-unchunked.svg](https://issues.apache.org/jira/secure/attachment/12883342/fg3-patched-unchunked.svg)
 (_1.48 MB_)
   - 
[fg4-patched-chunked.svg](https://issues.apache.org/jira/secure/attachment/12883341/fg4-patched-chunked.svg)
 (_1.50 MB_)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to