Project

General

Profile

Actions

Bug #9105

closed

callcc による不整合(例:Hash)

Added by tarui (Masaya Tarui) over 10 years ago. Updated over 10 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 2.1.0dev (2013-11-11 trunk 43647) [x86_64-linux]
[ruby-dev:47803]

Description

=begin
以下のコードを実行すると、
equire 'continuation'
h = {1=>2,3=>4}
c = nil
f = false
h.each { callcc {|c2| c = c2 } }
unless f
f = true
c.call
end
h.each {|i| h.delete(1); p i}

以下のような結果になります。

[1, 2]
[false, false]
test.rb:10:in each': hash modified during iteration (RuntimeError) from test.rb:10:in '

[false, false]自体はst_foreach_checkで全く機能していない
/* call func with error notice */
retval = (*func)(0, 0, arg, 1);
を消せばいいのですが、
RuntimeErrorが発生するのはcallccによりhash_foreach_ensureの実行が2重に行われて、
RHASH_ITER_LEV(hash)の管理が破綻してしまっている為です。

他の所でも容易に起こりうると思うのですが、未調査です。
&そもそもどう対処すべきでしょう?

=end


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #14472: `File::open` closes the file too early when used with callccRejectedActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r43675.
Masaya, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


hash.c: restore iter_lev

  • hash.c (hash_foreach_ensure): restore iter_lev to the previous
    value, not just decrement. [ruby-dev:47803] [Bug #9105]

Updated by nagachika (Tomoyuki Chikanaga) over 10 years ago

  • Status changed from Closed to Open

r43675 はマルチスレッドで同一の Hash を使っている場合に RHASH_ITER_LEV(hash) の値が不正になることがあると思います。
以下のようなスクリプトを実行すると RHASH_ITER_LEV() が 0 に戻らなくなるので要素追加に失敗します。

h = {1=>2}
th1 = Thread.start{ h.each{ sleep 1 } }
sleep 0.1
th2 = Thread.start{ h.each { sleep 3 } }
th1.join
th2.join

h[5] = 6 # => can't add a new key into hash during iteration (RuntimeError)
Actions #3

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Status changed from Open to Closed

This issue was solved with changeset r43683.
Masaya, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


hash.c: iteration level with reentering

  • hash.c (hash_foreach_iter, hash_foreach_ensure, rb_hash_foreach):
    deal with iteration level when reentering by callcc. temporary
    measure until rollback of ensure is introduced. [ruby-dev:47807]
    [Bug #9105]
Actions #4

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

  • Related to Bug #14472: `File::open` closes the file too early when used with callcc added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0