Thread#raise, Thread#kill, and timeout.rb are unsafe

ruby

    Next

  • 1. File.exists? broken in svn
    The following one-liner works with release versions of ruby, but fails in the current version from svn: ruby -e 'print File.exists?("a.a")' This isn't intentional, is it? I'm just trying to get a running copy of ruby with the fancier regexes that 1.9 has. I'm running Linux. Should I be trying the stable snapshot? I think it's also possible to compile 1.8 with the newer regexes, but I'm not sure how to do that on Linux. (On FreeBSD, the port asks you specifically if you want to do that.) TIA! -Ben

Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Charles Oliver Nutter » Tue, 26 Feb 2008 16:18:01 GMT

I wrote up an article on Thread#raise, Thread#kill, and timeout.rb that 
I hope can start making the rounds. Long story short, neither 
Thread#raise nor Thread#kill is safe to use, and as a result all 
libraries that call them are also unsafe (including timeout.rb, net/*, 
and many other libraries in the wider world).

Have a look, add comments, discuss. Hopefully we can find a way to fix 
this, because it's going to hold Ruby back until we do.

 http://www.**--****.com/ 

- Charlie


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Yukihiro Matsumoto » Sat, 15 Mar 2008 01:02:52 GMT

Hi,

In message "Re: Thread#raise, Thread#kill, and timeout.rb are unsafe"
    on Mon, 25 Feb 2008 16:18:01 +0900, Charles Oliver Nutter < XXXX@XXXXX.COM > writes:

|I wrote up an article on Thread#raise, Thread#kill, and timeout.rb that 
|I hope can start making the rounds. Long story short, neither 
|Thread#raise nor Thread#kill is safe to use, and as a result all 
|libraries that call them are also unsafe (including timeout.rb, net/*, 
|and many other libraries in the wider world).

Unlike Java, Thread#raise etc. should be treated similar to
KeyboardInterrupt in Ruby.  No realtime exception posting is expected.
If you handle KeyboardInterrupt safely, do same thing for Thread#raise
etc., e.g. just turning on flags to reserve exception, then raise
exception at the safe place, as MRI does.  Nothing more is required.

I admit that timeout.rb is inefficient, so that it should not be used
when performance or completeness matters.  I agree with removing use
of timeout from net/* libraries.  It's just for casual use.  Maybe
it's good to add a note in CAPITAL LETTERS in the document of
timeout.rb.

							matz.


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Robert Klemme » Sat, 15 Mar 2008 01:54:06 GMT

2008/2/25, Charles Oliver Nutter < XXXX@XXXXX.COM >:

There is a typo: your second example misses the "main." before the raise. :-)

I have to think a bit more about this, but one remark: IMHO the
locking should be outside the begin end block.  Reason is, that if
locking fails for a reason you would not want / need to unlock.

At the moment I'm pondering implications of these options:

main = Thread.current
timer = Thread.new { sleep 5; main.raise }
lock(some_resource)
begin
  do_some_work
ensure
  unlock_some_resource
  timer.kill  # moved downwards
end


main = Thread.current
timer = Thread.new { sleep 5; main.raise }
begin
  lock(some_resource)
  begin
    do_some_work
  ensure
    unlock_some_resource
  end
ensure
  timer.kill  # separate ensure
end

The last one has the added benefit that you can implement it in a
method with a block because timer code is not interleaved with work
code.

But both have the disadvantage that you risk getting a timeout
exception during unlock - which would be unfortunate.  OTOH, killing
the timer before the lock release can violate the timing constraint if
the unlocking takes longer - not very likely.

{*filter*} issues; we had some similar issues with changed timeout handling
of a TX manager in JBoss.  Basically the old TX manager was able to
bust an otherwise perfect transaction by timing out in the 2PC
process. :-))

Kind regards

robert

-- 
use.inject do |as, often| as.you_can - without end


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Paul Brannan » Sat, 15 Mar 2008 06:00:54 GMT



I'm not convinced KeyboardInterrupt can be handled safely, either, in a
script.  What happens if a KeyboardInterrupt exception is raised inside
an ensure block?  It could easily result in resource leaks.

I think the right way to handle signals and timeouts is either in an
event loop or a dedicated thread.  Asynchronous exceptions are
convenient but error-prone.

Paul



Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Yukihiro Matsumoto » Sat, 15 Mar 2008 07:43:28 GMT

Hi,

In message "Re: Thread#raise, Thread#kill, and timeout.rb are unsafe"
    on Fri, 14 Mar 2008 06:00:54 +0900, Paul Brannan < XXXX@XXXXX.COM > writes:

|I'm not convinced KeyboardInterrupt can be handled safely, either, in a
|script.  What happens if a KeyboardInterrupt exception is raised inside
|an ensure block?  It could easily result in resource leaks.

You are right, but keyboard interrupt has been there so long in the
history, and we hardly see such leak problems, so I assume it's safer
than you might think.  At least, we cannot not removed the interrupts
from the language.

							matz.


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Paul Brannan » Sat, 15 Mar 2008 22:32:33 GMT



For simple scripts, KeyboardInterrupt is probably the right behavior.

For applications that use an event loop, we can use trap('INT') to
disable KeyboardInterrupt.

I wonder if there should be an equivalent trap mechanism for other
asynchronous exceptions?

Paul



Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Yukihiro Matsumoto » Sat, 15 Mar 2008 23:42:37 GMT

Hi,

In message "Re: Thread#raise, Thread#kill, and timeout.rb are unsafe"
    on Fri, 14 Mar 2008 22:32:33 +0900, Paul Brannan < XXXX@XXXXX.COM > writes:

|I wonder if there should be an equivalent trap mechanism for other
|asynchronous exceptions?

Probably.  Does anyone have any idea?

							matz.


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Roger Pack » Sun, 16 Mar 2008 01:00:49 GMT



It was the problems with timeout that make me wish we had an 
EnsureCritical (an Ensure that gets Thread.critical=true set to it 
before entering it), so that timeouts can end properly.
Just my $.02
-- 
Posted via  http://www.**--****.com/ 


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Tanaka Akira » Sun, 16 Mar 2008 02:48:33 GMT

In article < XXXX@XXXXX.COM >,
  Yukihiro Matsumoto < XXXX@XXXXX.COM > writes:


The problem is, where is safe point.

You said "just turning on flags to reserve exception, then
raise exception at the safe place, as MRI does."

Your "safe" is for interpreter.  Ruby shouldn't SEGV, etc.

But Charles's "safe" is for application.  acquired lock
should be released later, etc.

Your "safe" is not enough for application.

We need a mechanism to delay asynchronous exceptions until a
safe point.

trap is bad way to do it.  trap is similar to Unix signal
handler.  Unix signal handler makes it difficult to avoid
race conditions around blocking operations such as I/O.
With trap, applications need to re-implement the race
condition handling in Ruby level.  It is very difficult if
it is possible.

It is preferable to have a way to declare safe points
directly.

The safe points vary according to applications.  Even in a
application, different kinds of exceptions may have
different safe points.

For example, there are applications KeyboardInterrupt is
safe as you said.  I think a filter, such as grep, is a kind
of the applications.  It means that any points in the
applications are safe points. 

Another example: movemail is a program to move mbox in a
mail spool.  It needs to lock a mbox.  Assume we implement
movemail in Ruby and the lock scheme is file lock.  The lock
file is removed in ensure clause.  But asynchronous
exception in the ensure block may cause to fail removing the
lock file.  So the ensure block is not safe points.

Yet another example: Apache can be stopped with apachectl
"stop" and "graceful-stop".  "stop" aborts open connections
but "graceful-stop" doesn't.  Assume we implement a
threading http server with keep-alive and similar stopping
feature in Ruby.  "stop" can be implemented by killing
threads for each connections by (dangerous) Thread.kill.  It
means that any points in the threads are safe points.
"graceful-stop" need to wait the threads until they waits
requests.  Since keep-alive is assumed, the threads
doesn't terminate after the first request on a connection.
This means the safe points are the request waiting points of
the threads.

I think the safe points declaration mechanism should able to
define safe points for each exception and easy to handle
blocking operations without race conditions.
-- 
Tanaka Akira


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby MenTaLguY » Sun, 16 Mar 2008 03:45:07 GMT



Anything using Thread.critical has its own problems, particularly
when native threads are involved.  It can easily result in deadlocks
if the running thread inadvertently tries to use something which another
stopped thread is using (imagine a thread stopped halfway through
releasing a lock that the ensure block needs to acquire, for example
-- something which would have worked fine with normal use of locks, but
becomes a problem once threads can "stop the world").

-mental



Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby MenTaLguY » Sun, 16 Mar 2008 03:54:15 GMT



The simplest way to "tame" asynchronous exceptions would be to enqueue such
exceptions until the recieving thread makes a blocking call, or else it calls
a special method like Thread.check_interrupts.

The main difficulty with the idea is defining clearly what a "blocking call"
is.  There probably also needs to be some mechanism (which does not "stop
the world" like Thread.critical) to mask asynchronous exceptions even during
blocking calls, for particularly critical code.

-mental



Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Yukihiro Matsumoto » Sun, 16 Mar 2008 11:29:30 GMT

Hi,

In message "Re: Thread#raise, Thread#kill, and timeout.rb are unsafe"
    on Sat, 15 Mar 2008 02:48:33 +0900, Tanaka Akira < XXXX@XXXXX.COM > writes:

|But Charles's "safe" is for application.  acquired lock
|should be released later, etc.

I know.

|Your "safe" is not enough for application.
|
|We need a mechanism to delay asynchronous exceptions until a
|safe point.

Hmm.

|I think the safe points declaration mechanism should able to
|define safe points for each exception and easy to handle
|blocking operations without race conditions.

What should safe point declaration mechanism look like?

							matz.


Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Roger Pack » Wed, 19 Mar 2008 00:06:47 GMT


Tongue in cheek I would say something like

uninterruptible do

end

or

ensureUninterruptible

end

or

uninterruptible do
# some stuff
handle_exceptions_that_have_been_thrown
end
# where handle_exceptions is where they can all get handled.
Kind of scary still, though.

:)

Anyway my latest thought on the whole timeout difficulty is that there
are a few difficulties with it that might be overcomeable:

Unfortunately, as a previous poster noted, using raise seems itself
almost always inherently dangerous, so these would probably all be
band-aids to a bigger problem.

1) if you have two nested timeouts then "you don't know where the
timeout came from"
timeout 5 do
timeout 5 do
begin
sleep
rescue Exception
# which one is it? What did I just rescue?
end
end
end


You could overcome this by just ensuring that it throws distinct classes
per timeout (i.e. if different timeouts).
timeout 5 do # will by default throw class Timeout::ErrorDepth1 <
Timeout::Error
timeout 5 do # will by default throw class Timeout::ErrorDepth2 <
Timeout::Error

I suppose this would help people decipher what is happening when
timeouts are thrown, though not be helpful otherwise.

1.5) It creates a new thread once per call to 'timeout' which is less
than efficient.

While I typically don't care about efficiency when using timeout, it
might be possible to just have a "single timer thread" that handles all
the raise'ings on the others, to save on thread creation
(theoretically).


So something like this

(sorry if the syntax is wrong--this is just pseudo-code for now)

$timer_thread = nil # just so I remember the name--you wouldn't have to
use a global, of course

class TimerThread < Thread
def handle_any_requests
# if any thread requests a timeout, add it to some list
if some_thread_is_waiting
sleep_until_should_wake_next_thread
if waiting_thread_is_still_running_its_timeout_block
waiting_thread.raise Timeout::Error
end
else
sleep -1
end
end

def TimerThread.add_me_to_the_queue_to_interrupt_me_in seconds, &block
$timer_thread.add_it_to_queue Thread.current, seconds
$timer_thread.wakeup # might need to synchronize this call so that
we don't get confusion
block.call # if we get interrupted between this line and the next we
are DEAD. Exactly the same problem that we had before--an exception
could be raised "far in the future" to interrupt it, when it has already
left and it shouldn't be raised.
$timer_thread.take_me_off_the_queue_I_am_done Thread.current # might
need to synchronize this call, too
end

end



# begin program. Note that you could just fire this up the first time
the script calls Timeout::timeout
$timer_thread = Thread.new {
loop do
handle_any_requests
end
}


Now when you want timeouts you do something like
TimerThread.add_me_to_the_queue_to_interrupt_me_in 5 do # seconds

# do some stuff for a long time

end

That would help with efficiency, since you wouldn't have to spawn a new
thread for each call (though it would, sigh, leave one running always in
the background). It would alleviate some concurrency problems, and
still leave at least one in there. Just throwing it out there.

2) nested timeouts can interrupt each other and leave and 'errant
thread' running which raises a

timeout 5 do
timeout 50 do
begin
sleep
en

Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby MenTaLguY » Wed, 19 Mar 2008 01:30:17 GMT



In a language with side-effects, I've become convinced that the only safe
way to handle asynchronous exceptions is for code to be uninterruptible by
default, and only explicitly allow interrupts at specific points (not broad
regions of code).

This gets back to the fundamental principle that, in order to write a
correct multithreaded program, any interaction between threads needs to be
by explicit mutual agreement at specific places.  One thread needs to
explicitly initiate the interaction (in this case, by calling Thread#raise),
and the other thread needs to explicitly accept it (e.g. by calling a method
which is known to permit the delivery of any pending interrupts when it is
called).  Otherwise, the resulting nondeterminism is impossible to control.

-mental



Re: Thread#raise, Thread#kill, and timeout.rb are unsafe

Postby Tanaka Akira » Wed, 19 Mar 2008 14:04:40 GMT

In article < XXXX@XXXXX.COM >,
  Yukihiro Matsumoto < XXXX@XXXXX.COM > writes:


Basically, asynchronous events should be queued.

* make a queue for each thread.
* Thread#raise(exc) enqueue exc to the queue.
* Thread.check_interrupt(klass) checks an exception which is
  a kind of klass in the queue of the current thread.  If it
  exists, the exception is raised.
* other queue examining mechanism, such as sigpending, etc,
  may be useful.

Thread.check_interrupt is a safe point.

However safe points is not only Thread.check_interrupt.
Blocking operations, such as I/O, may or may not be safe
points.  It is because Thread.check_interrupt with blocking
operations causes race conditions.  So application must
choose that make a blocking operation interruption safe or
uninterruptible.

* Thread.blocking_interruptible(klass) { ... }
* Thread.blocking_uninterruptible(klass) { ... }

Blocking operations in Thread.blocking_interruptible are
safe points.

If a thread has no resources to release, it is safe to kill
asynchronously.  Other than that, safe points should be
declared explicitly.  When a resource is acquired,
application should declare it.

* Thread.delay_interrupt(klass) { ... }

It is safe points that out of outermost
Thread.delay_interrupt.

Another idea is about ensure clause.  Since an ensure
clause is used to release a resource, it may delay
asynchronous events as in Thread.delay_interrupt.
-- 
Tanaka Akira


Similar Threads:

1.Killing a thread

2.Why don't threads get killed in this case?

Here is the code that is running on OpenBSD 4.2

#!/usr/local/bin/ruby

threads=[]

4.times do |i|
        threads[i]=Thread.new do
        %x{/usr/libexec/getty std.19200 tty00}
        end
end

threads.each{|thr| thr.join}

I run it as a unix background job, I get the following

-bash-3.2$ ./whore.rb &
[1] 11801
-bash-3.2$ ps waux | grep djdoboy
djdoboy  15158  0.0  0.3   844  1540 pi  Ss     9:41PM    0:00.05 -
bash
(bash)
djdoboy  11801  0.0  0.4   996  1872 pi  S     10:03PM    0:00.01
/usr/local/bin/ruby ./whore.rb
djdoboy   4493  0.0  0.1   372   556 pi  S     10:03PM    0:00.02
/usr/libexec/getty std.19200 tty00
djdoboy  19020  0.0  0.1   332   556 pi  S     10:03PM    0:00.01
/usr/libexec/getty std.19200 tty00
djdoboy    456  0.0  0.1   364   564 pi  S     10:03PM    0:00.02
/usr/libexec/getty std.19200 tty00
djdoboy  11853  0.0  0.1   260   584 pi  S     10:03PM    0:00.01
/usr/libexec/getty std.19200 tty00
djdoboy  23871  0.0  0.1   532   408 pi  R+    10:03PM    0:00.00 ps
-waux
djdoboy  27426  0.0  0.1   220   524 pi  S+    10:03PM    0:00.00 grep
djdoboy

Now I get and relogin
-bash-3.2$ exit
logout
Connection closed by foreign host.
[cdalten@localhost ~]$ telnet

-bash-3.2$ ps waux | grep djdoboy
djdoboy   6724  0.0  0.3  1012  1548 p6  Ss    10:04PM    0:00.02 -
bash
(bash)
djdoboy  25303  0.0  0.1   576   448 p6  R+    10:04PM    0:00.00 ps
-waux
djdoboy   8516  0.0  0.1  1012   408 p6  R+    10:04PM    0:00.00 grep
djdoboy (bash)
djdoboy  11801  0.0  0.4   996  1872 pi- S     10:03PM    0:00.01
/usr/local/bin/ruby ./whore.rb
djdoboy  11853  0.0  0.1   260   584 pi- I     10:03PM    0:00.01
/usr/libexec/getty std.19200 tty00
djdoboy    456  0.0  0.1   364   564 pi- I     10:03PM    0:00.02
/usr/libexec/getty std.19200 tty00
djdoboy   4493  0.0  0.1   372   556 pi- I     10:03PM    0:00.02
/usr/libexec/getty std.19200 tty00
djdoboy  19020  0.0  0.1   332   556 pi- I     10:03PM    0:00.01
/usr/libexec/getty std.19200 tty00

And the threads are still there. How come ruby threads don't get
killed
on exit? And more to the point, how do I kill them on exit?

Chad

3.ability to kill a Thread "immediately"?

4.Killing Threads & Processes on Windows

x = Thread.new { system("c:/program files/internet explorer/iexplore.exe") }
x.alive # Works

why doesnt x.kill actually "kill" internet explorer? When I think of
kill, I think of "gone" as in *nix gone.

Yet in Windows, I get:
irb(main):004:0> x.kill
=> #<Thread:0x2cfd680 dead>
irb(main):005:0>

---and IE remains.

x = IO.popen("c:/program files/internet explorer/iexplore.exe")
x.close

--Just hangs until IE is manually closed.

From a to z, how can this be done?

If another module is needed, can it be installed via gems? Thanks guys.


5.Best way to kill a thread started from a TkButton push

6. arguments to Thread#raise ?

7. raise problem in Thread

8. Non-Threaded Timeout?



Return to ruby

 

Who is online

Users browsing this forum: No registered users and 19 guest