Professional Web Applications Themes

Catching thread exceptions - Ruby

This is a multi-part message in MIME format. ------=_NextPart_000_00DF_01C35DA8.55D95060 Content-Type: text/plain; cht="iso-8859-1" Content-Transfer-Encoding: 7bit Recently I was helping a colleague who was new to Ruby with a program that was using threads. It seemed to him that for some reason that some of the thread's were not running. First he tried adding sleeps to the main thread to "give time" to the other threads to run, but this didn't work. It turns out that by joining the threads in question, that there were errors in the program that were throwing exceptions that killed the thread -- SILENTLY. I don't know ...

  1. #1

    Default Catching thread exceptions

    This is a multi-part message in MIME format.

    ------=_NextPart_000_00DF_01C35DA8.55D95060
    Content-Type: text/plain;
    cht="iso-8859-1"
    Content-Transfer-Encoding: 7bit

    Recently I was helping a colleague who was new to Ruby with a program
    that was using threads. It seemed to him that for some reason that some
    of the thread's were not running. First he tried adding sleeps to the
    main thread to "give time" to the other threads to run, but this didn't
    work. It turns out that by joining the threads in question, that there
    were errors in the program that were throwing exceptions that killed the
    thread -- SILENTLY.

    I don't know if there is a better way to do this, but this is the
    solution we came up with to print out any exceptions that occur in a
    thread (uncaught) and exit the program.

    require "thread"

    class MyThread < Thread
    def initialize(*args, &block)
    super(*args) do
    begin
    block.call
    rescue Exception => e
    $stderr.print "Exception occured: #{e.message} at " +
    e.backtrace.join("\n")
    exit! 1
    end
    end
    end
    end

    Is there a better way?

    Steve Tuckner

    ------=_NextPart_000_00DF_01C35DA8.55D95060
    Content-Type: text/html;
    cht="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>



    <META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR></HEAD>
    <BODY>
    <DIV><FONT face=3DArial size=3D2><SPAN =
    class=3D009342117-08082003>Recently I was=20
    helping a colleague who was new to Ruby with a program that was using =
    threads.=20
    It seemed to him that for some reason that some of&nbsp;the thread's =
    were not=20
    running. First he tried adding sleeps to the main thread to "give time" =
    to the=20
    other threads to run, but this didn't work. It turns out that by =
    joining the=20
    threads in question, that there were errors in the program that were =
    throwing=20
    exceptions that killed the thread -- SILENTLY.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D009342117-08082003></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>I =
    don't know if=20
    there is a better way to do this, but this is the solution we came up =
    with to=20
    print out any exceptions that occur in a thread (uncaught) and exit the =

    program.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D009342117-08082003></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
    class=3D009342117-08082003>require=20
    "thread"</SPAN></FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>class =
    MyThread &lt;=20
    Thread<BR>&nbsp;def initialize(*args, =
    &amp;block)<BR>&nbsp;&nbsp;super(*args)=20
    do<BR>&nbsp;&nbsp;&nbsp;begin<BR>&nbsp;&nbsp;&nbsp ;&nbsp;block.call<BR>&=
    nbsp;&nbsp;&nbsp;rescue=20
    Exception =3D&gt; e<BR>&nbsp;&nbsp;&nbsp;&nbsp;$stderr.print "Exception =
    occured:=20
    #{e.message} at " + =
    e.backtrace.join("\n")<BR>&nbsp;&nbsp;&nbsp;&nbsp; exit!=20
    1<BR>&nbsp;&nbsp;&nbsp;end<BR>&nbsp;&nbsp;end<BR>& nbsp;end<BR>end</SPAN>=
    </FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D009342117-08082003></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>Is =
    there a better=20
    way?</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D009342117-08082003></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D009342117-08082003>Steve =

    Tuckner</SPAN></FONT></DIV></BODY></HTML>

    ------=_NextPart_000_00DF_01C35DA8.55D95060--

    Steve Tuckner Guest

  2. #2

    Default Re: Catching thread exceptions

    >>>>> "S" == Steve Tuckner <STUCKNERMULTITECH.COM> writes:

    S> Is there a better way?

    Thread.abort_on_exception = true

    svg% ruby -e 'Thread.new { raise "aa" }; p "after"'
    "after"
    svg%

    svg% ruby -e 'Thread.abort_on_exception = true; Thread.new { raise "aa" }; p "after"'
    -e:1: aa (RuntimeError)
    from -e:1:in `initialize'
    from -e:1:in `new'
    from -e:1
    svg%


    Guy Decoux

    ts Guest

  3. #3

    Default Re: Catching thread exceptions

    On Sat, Aug 09, 2003 at 02:34:48AM +0900, ts wrote:
    > >>>>> "S" == Steve Tuckner <STUCKNERMULTITECH.COM> writes:
    >
    > S> Is there a better way?
    >
    > Thread.abort_on_exception = true
    described at
    [url]http://www.rubycentral.com/book/tut_threads.html[/url]
    and
    [url]http://www.rubycentral.com/book/ref_c_thread.html#Thread.abort_on_exception[/url]

    Regards,

    Brian.

    Brian Candler Guest

  4. #4

    Default Re: Catching thread exceptions


    Although a little late I try to answer that one.

    "Steve Tuckner" <STUCKNERMULTITECH.COM> schrieb im Newsbeitrag
    news:00ed01c35ddb$fe568590$6904a8c0multitech.prv. ..
    > Why is false by default? In what case would you want an exception to
    > propagate to the top of a thread and kill it SILENTLY and then keep
    running?

    IMHO this is the setting that minimally affects stability of a program.
    Imagine a server that spawns threads for requests. If any of those
    threads dies due to some error you don't want the whole server to stop.
    The proper way to do it is IMHO to handle errors from within the thread:

    Thread.new do
    begin
    raise "aa"
    rescue => e
    $stderr.puts "ERROR: #{e}"
    else
    $stdout.puts "fine"
    end
    end
    > Why doesn't it at least print something out if it isn't going to kill
    the
    > program?
    That's another story and possibly a good suggestion: have ruby print a
    message to STDERR if a thread terminates with an uncaught exception.

    Regards

    robert

    Robert Klemme Guest

Similar Threads

  1. Newbie:Using ASP.NET thread pool thread to dispatch TCP data, etc.
    By Navin Mishra in forum ASP.NET Web Services
    Replies: 0
    Last Post: March 22nd, 06:45 PM
  2. [PHP-DEV] Ifx - Help ! losing my mind, thread by thread
    By Corne' cornelius in forum PHP Development
    Replies: 0
    Last Post: August 21st, 09:56 AM
  3. Catching F1 keypress
    By Dell Stinnett in forum ASP
    Replies: 0
    Last Post: August 18th, 07:16 PM
  4. Catching this own exception
    By Julien [CNSX] in forum ASP.NET Web Services
    Replies: 1
    Last Post: July 11th, 07:15 AM
  5. catching exceptions from web user controls
    By Marina in forum ASP.NET General
    Replies: 2
    Last Post: July 8th, 04:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139