# How to kill a Thread into GServer "def server(io)" method?

**URL:** <https://www.ruby-forum.com/t/how-to-kill-a-thread-into-gserver-def-server-io-method/133099>\
**Category:** Ruby\
**Created:** [March 27, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/how-to-kill-a-thread-into-gserver-def-server-io-method/133099 "2008-03-27T18:32:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [March 27, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/how-to-kill-a-thread-into-gserver-def-server-io-method/133099/1 "2008-03-27T18:32:52Z")

</div>

Hi, I’m using GServer and in “serve” method I read a string from a TCP  
socket:

line = io.gets("\n")

but it can occur that the TCP connection is closed so “gets” return NIL.  
In this case I just one to terminate this thread, no more.

The only way I get it working is doing:

line = io.gets("\n")  
Thread.current.terminate if !line # terminate = kill = exit

Is it the appropiate way? I read the method doc but don’t understand  
what exactly it does:

thr.terminate  
Terminates thr and schedules another thread to be run,  
returning the terminated Thread. If  
this is the main thread, or the last thread, exits the process.

Thanks for any explanation.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [March 27, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/how-to-kill-a-thread-into-gserver-def-server-io-method/133099/2 "2008-03-27T19:57:39Z")

</div>

On Thu, Mar 27, 2008 at 11:31 AM, IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The only way I get it working is doing:
> 
> line = io.gets(“\n”)  
> Thread.current.terminate if !line # terminate = kill = exit

Looking at the code in:

[http://ruby-doc.org/stdlib/libdoc/gserver/rdoc/classes/GServer.html#M000757](http://ruby-doc.org/stdlib/libdoc/gserver/rdoc/classes/GServer.html#M000757)

I think you can just return from the method and be happy. You can  
also raise an exception which will be caught and logged as an error if  
you set the debug attribute to a true value.

Marcelo

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [March 27, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/how-to-kill-a-thread-into-gserver-def-server-io-method/133099/3 "2008-03-27T22:13:36Z")

</div>

El Jueves, 27 de Marzo de 2008, Marcelo escribiÃ³:

> I think you can just return from the method and be happy.  
> Yes, that’s true. A simple return works well 🙂

Thanks.
