summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test/ruby/bug-13526.rb20
-rw-r--r--test/ruby/test_autoload.rb5
-rw-r--r--variable.c40
-rw-r--r--version.h2
4 files changed, 58 insertions, 9 deletions
diff --git a/test/ruby/bug-13526.rb b/test/ruby/bug-13526.rb
new file mode 100644
index 0000000000..f42e1913ce
--- /dev/null
+++ b/test/ruby/bug-13526.rb
@@ -0,0 +1,20 @@
+# From https://bugs.ruby-lang.org/issues/13526#note-1
+
+sleep if $load
+$load = true
+
+n = 10
+threads = Array.new(n) do
+ Thread.new do
+ begin
+ autoload :Foo, "#{File.dirname($0)}/#{$0}"
+ Thread.pass
+ Foo
+ ensure
+ Thread.pass
+ end
+ end
+end
+
+Thread.pass while threads.all?(&:stop?)
+100.times { Thread.pass }
diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb
index b2d8d660fe..961825e0d0 100644
--- a/test/ruby/test_autoload.rb
+++ b/test/ruby/test_autoload.rb
@@ -241,6 +241,11 @@ p Foo::Bar
end
end
+ def test_bug_13526
+ script = File.join(__dir__, 'bug-13526.rb')
+ assert_ruby_status([script], '', '[ruby-core:81016] [Bug #13526]')
+ end
+
def add_autoload(path)
(@autoload_paths ||= []) << path
::Object.class_eval {autoload(:AutoloadTest, path)}
diff --git a/variable.c b/variable.c
index fadc8d1763..7f8cef94e0 100644
--- a/variable.c
+++ b/variable.c
@@ -2142,7 +2142,7 @@ autoload_reset(VALUE arg)
VALUE th = cur->thread;
cur->thread = Qfalse;
- list_del(&cur->waitq.node);
+ list_del_init(&cur->waitq.node); /* idempotent */
/*
* cur is stored on the stack of cur->waiting_th,
@@ -2155,6 +2155,34 @@ autoload_reset(VALUE arg)
return 0; /* ignored */
}
+static VALUE
+autoload_sleep(VALUE arg)
+{
+ struct autoload_state *state = (struct autoload_state *)arg;
+
+ /*
+ * autoload_reset in other thread will resume us and remove us
+ * from the waitq list
+ */
+ do {
+ rb_thread_sleep_deadly();
+ } while (state->thread != Qfalse);
+
+ return Qfalse;
+}
+
+static VALUE
+autoload_sleep_done(VALUE arg)
+{
+ struct autoload_state *state = (struct autoload_state *)arg;
+
+ if (state->thread != Qfalse && rb_thread_to_be_killed(state->thread)) {
+ list_del_init(&state->waitq.node); /* idempotent */
+ }
+
+ return Qfalse;
+}
+
VALUE
rb_autoload_load(VALUE mod, ID id)
{
@@ -2192,13 +2220,9 @@ rb_autoload_load(VALUE mod, ID id)
}
else {
list_add_tail(&ele->state->waitq.head, &state.waitq.node);
- /*
- * autoload_reset in other thread will resume us and remove us
- * from the waitq list
- */
- do {
- rb_thread_sleep_deadly();
- } while (state.thread != Qfalse);
+
+ rb_ensure(autoload_sleep, (VALUE)&state,
+ autoload_sleep_done, (VALUE)&state);
}
/* autoload_data_i can be deleted by another thread while require */
diff --git a/version.h b/version.h
index d26798f74c..fd82bf4551 100644
--- a/version.h
+++ b/version.h
@@ -1,6 +1,6 @@
#define RUBY_VERSION "2.4.2"
#define RUBY_RELEASE_DATE "2017-05-10"
-#define RUBY_PATCHLEVEL 130
+#define RUBY_PATCHLEVEL 131
#define RUBY_RELEASE_YEAR 2017
#define RUBY_RELEASE_MONTH 5