You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Renato Weiner <re...@yahoo.com> on 2001/06/29 23:28:36 UTC

Killing endless loop servlet - howto ? killing JVM or unload class ?

 Hi all,
I'm rolling out a successful Tomcat instalation in a shared environment ( it's a great software ! ). But I have a concern. 
I created a servlet that loops forever ( a very stupid one, by the way ). When I executed it, it allocates a Tomcat thread and it just runs forever. If I try to kill it after it consumed, let's say, 30 seconds of processing, it ended up killing the whole JVM. 
First, I don't know if it's killing a thread is the right approach. Should I do that, without shutdown Tomcat ? Is there a way to set a 'time-out' for a Servlet ? What I can do in this situation ? Is there an Interceptor that can unload this class somehow ?
Thanks in advance 
Renato - Brazil
P.S. I'm running Linux, kernel 2.4.3, Tomcat 3.2.2



---------------------------------
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 a year!
http://personal.mail.yahoo.com/

Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Dmitri Colebatch <di...@nuix.com.au>.
Hi,

On Thu, 12 Jul 2001 18:50, Endre Stølsvik wrote:
> How would you stop this thread?
>
> while(true);

something like this:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet 
{

    private Test test;

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException
    {
        
        String action = request.getParameter("action");
        String output = null;
        
        if ("start".equals(action))
        {
            if (test != null)
                output = "thread already running.";
            else 
            {
                test = new Test();
                test.start();
                output = "starting thread.";
            }
        }
        else if ("stop".equals(action))
        {
            if (test == null) 
            {
                output = "thread doesn't exist.";
            }
            else 
            {
                test.interrupt();
                test = null;
                output = "stopping thread.";
            }
        }
        else 
        {
            output = "unknown action.";
        }
        

        // this bit sets the content type
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        out.println(output);

    }

    private class Test extends Thread
    {
        public void run()
        {
            log("starting.");
            int i=0;
            while (true) 
            {
                log("running " + (++i));
                try
                {
                    sleep(1000);
                }
                catch (InterruptedException ie)
                {
                    log("interrupted.");
                    break;
                }

                if (isInterrupted()) 
                {
                    log("exiting.");
                    break;
                }
                
            }
            
        }
    }
}


cheers
dim


Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Renato Weiner <re...@yahoo.com>.
 Can we register the running thread in the service() method of the ServletWrapper object and then call a thread.interrupt() in the servlet.destroy method ? So we could stop the thread where unloading the context. Should this do the work ? 
  Roland Carlsson <ro...@swetravel.se> wrote: 
----- Original Message ----- 
From: "Endre St�lsvik" 
To: 
Sent: Thursday, July 12, 2001 10:50 AM
Subject: Re: Killing endless loop servlet - howto ? killing JVM or unload class ?


> On Thu, 12 Jul 2001, Dmitri Colebatch wrote:
> 
> | On Thu, 12 Jul 2001 16:28, you wrote:
> | > I thought the problem with threads was that you actually _cannot_ kill
> | > them, even "in Java". I find this stupid, and hope I'm wrong! But
> | > apparently you can only "ask" the thread to interrupt and check for status
> | > by using the thread.interrupt(), and isInterrupted() inside the thread.
> | The purpose of this is to force a two-phase termination, and give the thread
> | a chance to clean up... its up to the thread programmer to be aware of any
> | requests for interruption. When you say you can only "ask", you are correct.
> | I dont see why this prohibits my earlier suggestion though...
> 
> How would you stop this thread?
> 
> while(true);
> 
> 
> -- 
> Mvh,
> Endre
> 

Hi!

public booelan running;


public void run(){
while(running){
}
}

Another thread can then stop the thread by altering running to false. Then the thread will have the possibility to clean after itself.

You must ofcourse register the tread somewhere so you can find it but that is another question. Perhpas something to implement at serverlevel so a admin can look for running threads and kill them off...????

Regards
Roland Carlsson





---------------------------------
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 a year!
http://personal.mail.yahoo.com/

Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Roland Carlsson <ro...@swetravel.se>.
----- Original Message ----- 
From: "Endre Stølsvik" <En...@Stolsvik.com>
To: <to...@jakarta.apache.org>
Sent: Thursday, July 12, 2001 10:50 AM
Subject: Re: Killing endless loop servlet - howto ? killing JVM or unload class ?


> On Thu, 12 Jul 2001, Dmitri Colebatch wrote:
> 
> | On Thu, 12 Jul 2001 16:28, you wrote:
> | > I thought the problem with threads was that you actually _cannot_ kill
> | > them, even "in Java". I find this stupid, and hope I'm wrong! But
> | > apparently you can only "ask" the thread to interrupt and check for status
> | > by using the thread.interrupt(), and isInterrupted() inside the thread.
> | The purpose of this is to force a two-phase termination, and give the thread
> | a chance to clean up... its up to the thread programmer to be aware of any
> | requests for interruption.  When you say you can only "ask", you are correct.
> |  I dont see why this prohibits my earlier suggestion though...
> 
> How would you stop this thread?
> 
> while(true);
> 
> 
> -- 
> Mvh,
> Endre
> 

Hi!

public booelan running;


public void run(){
    while(running){
    }
}

Another thread can then stop the thread by altering running to false. Then the thread will have the possibility to clean after itself.

You must ofcourse register the tread somewhere so you can find it but that is another question. Perhpas something to implement at serverlevel so a admin can look for running threads and kill them off...????

Regards
Roland Carlsson




Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Endre Stølsvik <En...@Stolsvik.com>.
On Thu, 12 Jul 2001, Dmitri Colebatch wrote:

| On Thu, 12 Jul 2001 16:28, you wrote:
| > I thought the problem with threads was that you actually _cannot_ kill
| > them, even "in Java". I find this stupid, and hope I'm wrong! But
| > apparently you can only "ask" the thread to interrupt and check for status
| > by using the thread.interrupt(), and isInterrupted() inside the thread.
| The purpose of this is to force a two-phase termination, and give the thread
| a chance to clean up... its up to the thread programmer to be aware of any
| requests for interruption.  When you say you can only "ask", you are correct.
|  I dont see why this prohibits my earlier suggestion though...

How would you stop this thread?

while(true);


-- 
Mvh,
Endre


Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Dmitri Colebatch <di...@nuix.com.au>.
On Thu, 12 Jul 2001 16:28, you wrote:
> I thought the problem with threads was that you actually _cannot_ kill
> them, even "in Java". I find this stupid, and hope I'm wrong! But
> apparently you can only "ask" the thread to interrupt and check for status
> by using the thread.interrupt(), and isInterrupted() inside the thread.
The purpose of this is to force a two-phase termination, and give the thread 
a chance to clean up... its up to the thread programmer to be aware of any 
requests for interruption.  When you say you can only "ask", you are correct. 
 I dont see why this prohibits my earlier suggestion though...

cheers
dim


AW: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Thorsten Schwarz <to...@startserver.de>.

> I thought the problem with threads was that you actually _cannot_ kill
> them, even "in Java". I find this stupid, and hope I'm wrong! But

That's the point. There is NO WAY in a JVM to stop or kill an arbitrary
thread from outside. This is one of the main disadvantages in Java!!
Even unloading the class by garbage collecting the classloader that loaded
the running thread class wouldn't help because the running class is only
garbage collected if its in no use by any thread (in this case by itself).


Thorsten


Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Endre Stølsvik <En...@Stolsvik.com>.
On Thu, 12 Jul 2001, Dmitri Colebatch wrote:

| Does it intentionally loop forever?  If so, creating a new thread and having
| that do the work (hence returning tomcat's thread to the server) should do
| the trick.

How would you do that?

I thought the problem with threads was that you actually _cannot_ kill
them, even "in Java". I find this stupid, and hope I'm wrong! But
apparently you can only "ask" the thread to interrupt and check for status
by using the thread.interrupt(), and isInterrupted() inside the thread.

-- 
Mvh,
Endre


Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Renato Weiner <re...@yahoo.com>.
Hi,
Actually I'm testing some time-out features of Tomcat. Users eventually ended up doing endless loops ( like, forgetting to move to next line in a database record set... ). So, what I'm trying to see if there is a patch that could kill the 'offending' threads after a defined time interval.
Thanks
Renato.
 
 
  Dmitri Colebatch <di...@nuix.com.au> wrote: Does it intentionally loop forever? If so, creating a new thread and having 
that do the work (hence returning tomcat's thread to the server) should do 
the trick.

cheers
dim


On Sat, 30 Jun 2001 07:28, you wrote:
> Hi all,
> I'm rolling out a successful Tomcat instalation in a shared environment (
> it's a great software ! ). But I have a concern. I created a servlet that
> loops forever ( a very stupid one, by the way ). When I executed it, it
> allocates a Tomcat thread and it just runs forever. If I try to kill it
> after it consumed, let's say, 30 seconds of processing, it ended up killing
> the whole JVM. First, I don't know if it's killing a thread is the right
> approach. Should I do that, without shutdown Tomcat ? Is there a way to set
> a 'time-out' for a Servlet ? What I can do in this situation ? Is there an
> Interceptor that can unload this class somehow ? Thanks in advance
> Renato - Brazil
> P.S. I'm running Linux, kernel 2.4.3, Tomcat 3.2.2
>
>
>
> ---------------------------------
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35 a year!
> http://personal.mail.yahoo.com/

----------------------------------------
Content-Type: text/html; charset="us-ascii"; name="Attachment: 1"
Content-Transfer-Encoding: 7bit
Content-Description: 
----------------------------------------


---------------------------------
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 a year!
http://personal.mail.yahoo.com/

Re: Killing endless loop servlet - howto ? killing JVM or unload class ?

Posted by Dmitri Colebatch <di...@nuix.com.au>.
Does it intentionally loop forever?  If so, creating a new thread and having 
that do the work (hence returning tomcat's thread to the server) should do 
the trick.

cheers
dim


On Sat, 30 Jun 2001 07:28, you wrote:
>  Hi all,
> I'm rolling out a successful Tomcat instalation in a shared environment (
> it's a great software ! ). But I have a concern. I created a servlet that
> loops forever ( a very stupid one, by the way ). When I executed it, it
> allocates a Tomcat thread and it just runs forever. If I try to kill it
> after it consumed, let's say, 30 seconds of processing, it ended up killing
> the whole JVM. First, I don't know if it's killing a thread is the right
> approach. Should I do that, without shutdown Tomcat ? Is there a way to set
> a 'time-out' for a Servlet ? What I can do in this situation ? Is there an
> Interceptor that can unload this class somehow ? Thanks in advance
> Renato - Brazil
> P.S. I'm running Linux, kernel 2.4.3, Tomcat 3.2.2
>
>
>
> ---------------------------------
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35 a year!
> http://personal.mail.yahoo.com/

----------------------------------------
Content-Type: text/html; charset="us-ascii"; name="Attachment: 1"
Content-Transfer-Encoding: 7bit
Content-Description: 
----------------------------------------