# RestController send response w/o closing channel if keep alive

**URL:** <https://discuss.elastic.co/t/restcontroller-send-response-w-o-closing-channel-if-keep-alive/84081>\
**Category:** Elasticsearch\
**Created:** [April 29, 2017, 9:22am UTC](https://discuss.elastic.co/t/restcontroller-send-response-w-o-closing-channel-if-keep-alive/84081 "2017-04-29T09:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simone\_Scarduzio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simone_scarduzio/32/17128_2.png) [@Simone\_Scarduzio](https://discuss.elastic.co/u/Simone_Scarduzio)\
**Post date:** [April 29, 2017, 9:22am UTC](https://discuss.elastic.co/t/restcontroller-send-response-w-o-closing-channel-if-keep-alive/84081/1 "2017-04-29T09:22:39Z")

</div>

Hello,  
My plugin needs to return a 401 HTTP response code at times.

So what I do today is using the channel and send a response:  
[https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin/blob/master/src/main/java/org/elasticsearch/plugin/readonlyrest/IndexLevelActionFilter.java#L156](https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin/blob/master/src/main/java/org/elasticsearch/plugin/readonlyrest/IndexLevelActionFilter.java#L156)

So this is what makes the netty thread hang:

1. Client sends a HTTP request with Connection:Keep-Alive
2. I respond 401 (controller is closed, netty channel is not)
3. Client continues in the same connection with a new request
4. My plugin never get called again (because rest controller was closed, I guess)

See the snoop from Wireshark:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/d/3/d3d1760c14cbc7553fda9fc86ed2c25b44001868.png)

Digging on ES code, I noticed there's no keep alive check here when we close the controller,

[https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/rest/RestController.java#L401](https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/rest/RestController.java#L401)

But you do correctly check if the header is set when you go about sending the response at the channel level

> <https://github.com/elastic/elasticsearch/blob/master/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpChannel.java#L137>

Is this a bug on your side or should I send a 401 response differently from my plugin?

---

<div class="post-metadata">

**Author:** ![jasontedor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasontedor/32/66992_2.png) [@jasontedor](https://discuss.elastic.co/u/jasontedor)\
**Post date:** [April 29, 2017, 12:48pm UTC](https://discuss.elastic.co/t/restcontroller-send-response-w-o-closing-channel-if-keep-alive/84081/2 "2017-04-29T12:48:36Z")

</div>

No, that's not closing the controller (it's not closable) nor the connection, that's only closing the resource channel so the circuit breaker accounting is correct. The bug is on your side, keep alive works fine.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 27, 2017, 12:56pm UTC](https://discuss.elastic.co/t/restcontroller-send-response-w-o-closing-channel-if-keep-alive/84081/3 "2017-05-27T12:56:38Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
