mattn / mruby-thread

48 stars 31 forks source link

Can't kill thread when thread is sleeping #43

Open skrbhn opened 8 years ago

skrbhn commented 8 years ago
class TimerDemo
  def start()
    @timer = Thread.new() {
      Thread.sleep 10
    }
  end

  def stop()
    @timer.kill()
  end
end

th = TimerDemo.new()
th.start()
Thread.sleep 7
th.stop()
while true
  Thread.sleep 7
end

After 10 seconds, it's closed by a unkown error which is from os.

Dan2552 commented 4 years ago

I'm not 100% sure if related to this issue, or another issue entirely -- I'm not seeing any unknown error

but as far as I can tell, either I'm misunderstanding the purpose of kill, or it simply doesn't work


def timeout(time, &blk)
  t2 = Thread.new(&blk)

  t1 = Thread.new do
    Thread.sleep(time)
    t2.kill
    puts "t2 should be killed; alive = #{t2.alive?}"
  end

  t2.join
  puts "t2 finished"
  t1.kill
end

timeout(2) do
  puts "waiting for char"
  char = STDIN.getc
  puts "this shouldn't run"
end
puts "after timeout"

prints:

waiting for char
t2 should be killed; alive = true

and if the enter key is pressed after waiting:

waiting for char
t2 should be killed; alive = true

this shouldn't run
t2 finished
after timeout
kvtb commented 8 hours ago

You may be using https://github.com/ksss/mruby-signal, it conflicts with mruby-thread

Even if you do not use mruby-signal functions, the very fact it is compiled into your mruby breaks killing threads