Catching thread exceptions (Net::IMAP timeout)

Discussion in 'Ruby' started by anselm, Mar 21, 2007.

  1. anselm

    anselm Guest

    Hello :)

    I have a question : is it possible to catch exceptions that are raised
    on a thread by another thread ? Say if you have two threads, A and B
    and thread A does

    B.raise "bug"

    Is there a way for thread B to catch and process that exception, and
    resume it's execution where it was at ?

    Specifcally, I am asking this because of the Net::IMAP library.
    Because responses are asynchronous, the Net::IMAP library runs on two
    threads. The main thread, and another thread which is there to recieve
    data from the IMAP server. In most cases this works fine, because
    exceptions only get raised when you are calling the Net::IMAP library
    (so you can have your begin...rescue block and deal with it
    appropriately).

    However, as specified in the Imap4rev1 RFC, the IMAP server is allowed
    to send you data without having been prompted for anything -- and one
    case where it does this is when it sends a timeout message
    (ByeResponseError). When the Net::IMAP library gets this message, it
    raises an exception on the main thread -- and this can happen at any
    time.

    I can deal with the timeouts -- but I don't know how to catch that
    exception in a way that allows me to resume my thread of execution !

    Thanks for any help,
    Anselm
     
    anselm, Mar 21, 2007
    #1
    1. Advertising

  2. On Thu, Mar 22, 2007 at 02:55:12AM +0900, anselm wrote:
    > I have a question : is it possible to catch exceptions that are raised
    > on a thread by another thread ? Say if you have two threads, A and B
    > and thread A does
    >
    > B.raise "bug"
    >
    > Is there a way for thread B to catch and process that exception, and
    > resume it's execution where it was at ?


    Well, one option is for B to start a new thread C, where it does all its
    work. The original thread B hangs around in an infinite loop just catching
    and processing exceptions.

    require 'thread'
    A = Thread.new { sleep 1; B.raise "bug" }
    B = Thread.new {
    C = Thread.new {
    loop do
    puts "Ho hum..."
    sleep 3
    end
    }
    begin
    sleep 10000 while true
    rescue Exception => e
    puts "Hey, I got a #{e}"
    retry
    end
    }

    A.join
    B.join
    C.join
     
    Brian Candler, Mar 21, 2007
    #2
    1. Advertising

  3. anselm

    anselm Guest

    On Mar 21, 9:03 pm, Brian Candler <> wrote:
    > On Thu, Mar 22, 2007 at 02:55:12AM +0900, anselm wrote:
    > > I have a question : is it possible to catch exceptions that are raised
    > > on a thread by another thread ? Say if you have two threads, A and B
    > > and thread A does

    >
    > > B.raise "bug"

    >
    > > Is there a way for thread B to catch and process that exception, and
    > > resume it's execution where it was at ?

    >
    > Well, one option is for B to start a new thread C, where it does all its
    > work. The original thread B hangs around in an infinite loop just catching
    > and processing exceptions.


    Yes, this is pretty good thank you ! Maybe in my particular case I
    could do it the other way round : start the imap thread from a new
    thread wich will then just wait for the exception. This should then re-
    raise exceptions that are not the timeout back on the main thread
    (since in those cases the imap library blocks while waiting for
    responses, this should not be a problem -- the exception should still
    appear to come out of the imap call)

    mainthread = Thread.current
    imap = nil
    Thread.new {
    begin
    limap = Net::IMAP.new('example.com')
    limap.login('username', 'password')
    imap = limap
    sleep 10000 while true
    rescue Exception => e
    if e.kind_of? Net::IMAP::ByeResponseError
    # deal with timeout
    else
    mainthread.raise e
    end
    end
    }

    # Wait for connection to be established
    begin
    sleep 1 while imap.nil?
    rescue Exception => e
    # Connection error
    end

    begin
    imap.select('', 'INBOX')
    rescue Net::IMAP::Error
    # Synchronous exceptions should still be caught here
    end

    Thanks,
    Anselm
     
    anselm, Mar 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik Ormåsen

    Curier-IMAP and imap.create()

    Henrik Ormåsen, Aug 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    183
    Henrik Ormåsen
    Aug 19, 2006
  2. Jon Fi
    Replies:
    4
    Views:
    574
    Kashia Buch
    Oct 21, 2006
  3. jasonnaylor
    Replies:
    1
    Views:
    259
    jasonnaylor
    Apr 16, 2008
  4. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    4
    Views:
    206
    Eric Hodel
    Jul 4, 2009
  5. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    0
    Views:
    244
    Abhishiv Saxena
    Jul 3, 2009
Loading...

Share This Page