--mimepart_4d33cb544776c_1dc2b349de68982
Content-Type: text/plain
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline

Bug #4283: Timeout.timeout may cause application exit unintetionally
http://redmine.ruby-lang.org/issues/show/4283

Author: Motohiro KOSAKI
Status: Open, Priority: Normal
Category: lib, Target version: 1.9.3
ruby -v: ruby 1.9.3dev (2010-12-22 trunk 30291) [x86_64-linux]

This issue was discovered during [Bug#4266] discussion.
Current timeout is racy.

Now, timeout module has following code.
-------------------------------------------------------------------------------
  def timeout()
    begin
      x = Thread.current
      y = Thread.start {
        begin
          sleep sec
        rescue => e
          x.raise e
        else
          x.raise exception, "execution expired" if x.alive?
        end
      }
      return yield(sec)
    rescue exception => e
      raise Error, e.message, e.backtrace
    ensure
      if y and y.alive?
        // (1)
        y.kill
        y.join # make sure y is dead.
      end
    end
  end
-------------------------------------------------------------------------------

Then, A following race can occur.


  CPU0(thread x)                    CPU1(thread y)         remark
---------------------------------------------------------------------------
  begin
  Thread.start
                                     sleep sec
  evaluate [user-defined-block]
  y.alive?                                                  return true
                                     wakeup from sleep
                                     x.raise


Now, x is running at (1). Then ExitException which y raised can't be handled
above rescue block. Then eventually, ExitException leak to caller and makes
application exit.


----------------------------------------
http://redmine.ruby-lang.org

--mimepart_4d33cb544776c_1dc2b349de68982
Content-Type: text/plain; name=timeout-race-fix.patch
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=timeout-race-fix.patch

RnJvbSBmNjE4MDM4OWQyYTIwNWJjNjU1NjA2ZDY1YzQ1OGIwNWZiNTVlNDRk
IE1vbiBTZXAgMTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBLT1NBS0kgTW90b2hp
cm8gPGtvc2FraS5tb3RvaGlyb0BqcC5mdWppdHN1LmNvbT4KRGF0ZTogRnJp
LCAxMSBGZWIgMjAxMSAxNjo0MDoyNyArMDkwMApTdWJqZWN0OiBbUEFUQ0hd
IHRpbWVvdXQgZml4CgpDdXJyZW50IHRpbWVvdXQgaXMgc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JvPwog
ICAgIGV4Y2VwdGlvbiA9IGtsYXNzIHx8IENsYXNzLm5ldyhFeGl0RXhjZXB0
aW9uKQogICAgIGJlZ2luCi0gICAgICB4ID0gVGhyZWFkLmN1cnJlbnQKLSAg
ICAgIHkgPSBUaHJlYWQuc3RhcnQgewotICAgICAgICBiZWdpbgotICAgICAg
ICAgIHNsZWVwIHNlYwotICAgICAgICByZXNjdWUgPT4gZQotICAgICAgICAg
IHgucmFpc2UgZQotICAgICAgICBlbHNlCi0gICAgICAgICAgeC5yYWlzZSBl
eGNlcHRpb24sICJleGVjdXRpb24gZXhwaXJlZCIgaWYgeC5hbGl2ZT8KKyAg
ICAgIGJlZ2luCisgICAgICAgIHggPSBUaHJlYWQuY3VycmVudAorICAgICAg
ICB5ID0gVGhyZWFkLnN0YXJ0IHsKKyAgICAgICAgICBiZWdpbgorICAgICAg
ICAgICAgc2xlZXAgc2VjCisgICAgICAgICAgcmVzY3VlID0+IGUKKyAgICAg
ICAgICAgIHgucmFpc2UgZQorICAgICAgICAgIGVsc2UKKyAgICAgICAgICAg
IHgucmFpc2UgZXhjZXB0aW9uLCAiZXhlY3V0aW9uIGV4cGlyZWQiCisgICAg
ICAgICAgZW5kCisgICAgICAgIH0KKyAgICAgICAgcmV0dXJuIHlpZWxkKHNl
YykKKyAgICAgIGVuc3VyZQorICAgICAgICBpZiB5CisgICAgICAgICAgeS5r
aWxsCisgICAgICAgICAgeS5qb2luICMgbWFrZSBzdXJlIHkgaXMgZGVhZC4K
ICAgICAgICAgZW5kCi0gICAgICB9Ci0gICAgICByZXR1cm4geWllbGQoc2Vj
KQorICAgICAgZW5kCiAgICAgcmVzY3VlIGV4Y2VwdGlvbiA9PiBlCiAgICAg
ICByZWogPSAvXEEje1JlZ2V4cC5xdW90ZShfX0ZJTEVfXyl9OiN7X19MSU5F
X18tNH1cei9vCiAgICAgICAoYnQgPSBlLmJhY2t0cmFjZSkucmVqZWN0ISB7
fG18IHJlaiA9fiBtfQpAQCAtNjYsMTEgKzczLDYgQEAgbW9kdWxlIFRpbWVv
dXQKICAgICAgIHJhaXNlIGlmIGtsYXNzICAgICAgICAgICAgIyBpZiBleGNl
cHRpb24gY2xhc3MgaXMgc3BlY2lmaWVkLCBpdAogICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAjIHdvdWxkIGJlIGV4cGVjdGVkIG91dHNpZGUu
CiAgICAgICByYWlzZSBFcnJvciwgZS5tZXNzYWdlLCBlLmJhY2t0cmFjZQot
ICAgIGVuc3VyZQotICAgICAgaWYgeSBhbmQgeS5hbGl2ZT8KLSAgICAgICAg
eS5raWxsCi0gICAgICAgIHkuam9pbiAjIG1ha2Ugc3VyZSB5IGlzIGRlYWQu
Ci0gICAgICBlbmQKICAgICBlbmQKICAgZW5kCiAKLS0gCjEuNi41LjIKCg
--mimepart_4d33cb544776c_1dc2b349de68982--