diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-12 21:52:04 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-12 21:52:04 +0000 |
commit | 94a7a4e908f3020045d1ae64c7ee4e70b14f2a5e (patch) | |
tree | c3c0e49c6f3c42d2457d7649ca5632fdbde8bc6b /variable.c | |
parent | 98746acff3ee24d63b31549da5399df7ff32bc72 (diff) |
autoload: always wait on loading thread
We cannot assume autoload_provided/rb_feature_provided returning
TRUE means it is safe to proceed without waiting. Another
thread may call rb_provide_feature before setting the constant
(via autoload_const_set). So we must wait until autoload is
completed by another thread.
Note: this patch was tested with an explicit rb_thread_schedule
in rb_provide_feature to make the race condition more apparent
as suggested by <[email protected]>:
> --- a/load.c
> +++ b/load.c
> @@ -563,6 +563,7 @@ rb_provide_feature(VALUE feature)
> rb_str_freeze(feature);
>
> rb_ary_push(features, rb_fstring(feature));
> +rb_thread_schedule();
> features_index_add(feature, INT2FIX(RARRAY_LEN(features)-1));
> reset_loaded_features_snapshot();
> }
* variable.c (check_autoload_required): do not assume a provided
feature means autoload is complete, always wait if autoload is
being performed by another thread.
[ruby-core:81105] [Bug #11384] Thanks to <[email protected]>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58696 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/variable.c b/variable.c index 6e883c7041..63c5f2aeab 100644 --- a/variable.c +++ b/variable.c @@ -1987,6 +1987,17 @@ check_autoload_required(VALUE mod, ID id, const char **loadingpath) if (!RSTRING_LEN(file) || !*RSTRING_PTR(file)) { rb_raise(rb_eArgError, "empty file name"); } + + /* + * if somebody else is autoloading, we MUST wait for them, since + * rb_provide_feature can provide a feature before autoload_const_set + * completes. We must wait until autoload_const_set finishes in + * the other thread. + */ + if (ele->state && ele->state->thread != rb_thread_current()) { + return load; + } + loading = RSTRING_PTR(file); safe = rb_safe_level(); rb_set_safe_level_force(0); |