--0016364eef7ab794c304b2eaad7b
Content-Type: text/plain; charset=ISO-8859-1

Yehuda Katz
(ph) 718.877.1325


On Tue, Nov 29, 2011 at 2:43 AM, Charles Oliver Nutter
<headius / headius.com>wrote:

> On Mon, Nov 21, 2011 at 4:18 AM, Hiroshi Nakamura <nahi / ruby-lang.org>
> wrote:
> > I see.  Maybe I misunderstood 'a single lock' concept of 1).  1) is not
> > needed, and 2) should be enough for safe classloading normally.
> > Initialization of classes (and interfaces) are defined in JLS[1]
> > carefully, and no deadlock occurred in normal classloading scheme
> > (parent to child.)
> >
> > Customized classloader can cause a deadlock, and Java SE 7 introduced a
> > manual way to avoid this for classloader developer.
> >
> > [1]
> >
> http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2
> > [2]
> http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html
>
> Yes, custom classloaders that delegate in odd ways (cross-hierarchy,
> where you have crossing child/parent relationships) could certainly
> deadlock. It was a bit of an odd situation, though, since normally
> classloaders follow a normal child/parent relationship and just go up
> a hierarchy. For the cases where you need to do this more complicated
> loading, it looks like they've fixed the locking to consider both the
> class under load and the classloader loading it, so you don't end up
> with a deadlock.
>
> > Sure. We cannot control code execution order of initializers (that's
> > just a chunk of code in Ruby) and deadlock is inherently unavoidable if
> > developer writes a 'wrong code' such as;
> ...
> > As the existing warning 'circular require considered harmful', current
> > Ruby treats it as a developer's job to avoid this 'wrong code'.  I was
> > trying to solve threading issues of require and autoload, and I think we
> > solved it at #921 unless developers don't write 'wrong code'.
> >
> > I understand that Yehuda and you are saying that it's not a 'wrong
> > code.'  It's hard to define 'wrong code' I agree.  I still wanna see
> > concrete examples which are not 'wrong code' but I think it's OK to fix
> > the issue inherently by introducing global (per VM) file loading lock if
> > it doesn't cause serious degradation in 2.0.
> >
> > Isn't there an application which uses Threads + parallel execution by
> > "load" method?  I don't have concrete example :)
>
> You replied later that you meant "require" not "load". I think
> Yehuda's case is not too exotic, and could conceivably happen. For the
> simple case of two threads autoloading at the same time, if autoload
> being made single-threaded (single global lock for all autoloads)
> would be enough. If autoloads and normal requires fire at the same
> time, however, it could still deadlock. This would happen if an
> autoload executes concurrently with another normal require. That's a
> bit more exotic.
>
> I'm stil at a loss to describe a scenario where parallel 'require' is
> what you *want* to happen, but I recognize it would be a visible
> change from today.
>

If you have to choose between making requires threadsafe and enabling
parallel requires, making requires threadsafe clearly wins.


>
> >> I think we could claim autoload is thread-safe in itself if it always
> >> just used a single lock. In other words, only one thread can be
> >> processing autoloads at a time. This doesn't fix the parallel require
> >> issue, but it would fix parallel autoloading.
> >>
> >> I think that's an ok fix, no?
> >
> > Agreed.  At least it's worth trying.
> >
> > I'll create a new ticket for the change (single lock require.).  This
> > ticket is for backporting thread-safe autoload (unless you don't write
> > 'wrong code' :-) by Mike.  I'll wait for the report if the existing
> > thread-safe autoload on trunk works for him or not.
>
> Agreed. At least we can say "concurrent autoloads won't happen" and as
> a result they're 100% safe. Concurrent requires still have concerns,
> but they're less likely to happen because people genreally don't do
> lazy requires in threads unless they're triggered by autoloads.
>

For reasons I described earlier, Rails emulates autoload via requires, so
any threadsafety fix that relies on autoload would either need to mitigate
our need to emulate autoload (by allowing something like autoload
"Foo::Bar", "foo/bar") or also support threadsafe require.

That said, I don't see why we couldn't support threadsafe require for cases
where lazy require makes sense.


> I think an additional warning about doing requires lazily might be
> useful...perhaps any require that happens on some non-main thread that
> *isn't* in response to a require should elicit a warning? Or perhaps
> when we detect concurrent non-autooad requires (i.e. two threads doing
> separate requires at the same time, regardless of the files they're
> loading) we should warn?
>

See above. I would rather just get a single lock around require and make
things safe.


>
> - Charlie
>
>

--0016364eef7ab794c304b2eaad7b
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br clear="all">Yehuda Katz<br>(ph) 718.877.1325<br>
<br><br><div class="gmail_quote">On Tue, Nov 29, 2011 at 2:43 AM, Charlesliver Nutter <span dir="ltr">&lt;headius / headius.com&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On Mon, Nov 21, 2011 at 4:18 AM, Hiroshi Nakamura &lt;nahi / ruby-lang.org&gt; wrote:<br>
&gt; I see.   念  릣  &gt; needed, and 2) should be enough for safe classloading normally.<br>
&gt; Initialization of classes (and interfaces) are defined in JLS[1]<br>
&gt; carefully, and no deadlock occurred in normal classloading scheme<br>
&gt; (parent to child.)<br>
&gt;<br>
&gt; Customized classloader can cause a deadlock, and Java SE 7 introduced a<br>
&gt; manual way to avoid this for classloader developer.<br>
&gt;<br>
&gt; [1]<br>
&gt; <a href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2" target="_blank">http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2</a><br>
&gt; [2] <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html" target="_blank">http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html</a><br>
<br>
</div>Yes, custom classloaders that delegate in odd ways (cross-hierarchy,<br>
where you have crossing child/parent relationships) could certainly<br>
deadlock. It was a bit of an odd situation, though, since normally<br>
classloaders follow a normal child/parent relationship and just go up<br>
a hierarchy. For the cases where you need to do this more complicated<br>
loading, it looks like they&#39;ve fixed the locking to consider both the<br>
class under load and the classloader loading it, so you don&#39;t end up<br>
with a deadlock.<br>
<div class="im"><br>
&gt; Sure. We cannot control code execution order of initializers (that&#39;s<br>
&gt; just a chunk of code in Ruby) and deadlock is inherently unavoidable if<br>
&gt; developer writes a &#39;wrong code&#39; such as;<br>
</div>...<br>
<div class="im">&gt; As the existing warning &#39;circular require considered harmful&#39;, current<br>
&gt; Ruby treats it as a developer&#39;s job to avoid this &#39;wrong code&#39;.   &gt; trying to solve threading issues of require and autoload, and I think we<br>
&gt; solved it at #921 unless developers don&#39;t write &#39;wrong code&#39;.<br>
&gt;<br>
&gt; I understand that Yehuda and you are saying that it&#39;s not a &#39;wrong<br>
&gt; code.&#39;      妣   &gt; concrete examples which are not &#39;wrong code&#39; but I think it&#39;s OK to fix<br>
&gt; the issue inherently by introducing global (per VM) file loading lock if<br>
&gt; it doesn&#39;t cause serious degradation in 2.0.<br>
&gt;<br>
&gt; Isn&#39;t there an application which uses Threads + parallel executiony<br>
&gt; &quot;load&quot; method?     캩
<br>
</div>You replied later that you meant &quot;require&quot; not &quot;load&quot;. I think<br>
Yehuda&#39;s case is not too exotic, and could conceivably happen. For the<br>
simple case of two threads autoloading at the same time, if autoload<br>
being made single-threaded (single global lock for all autoloads)<br>
would be enough. If autoloads and normal requires fire at the same<br>
time, however, it could still deadlock. This would happen if an<br>
autoload executes concurrently with another normal require. That&#39;s a<br>
bit more exotic.<br>
<br>
I&#39;m stil at a loss to describe a scenario where parallel &#39;require&#39; is<br>
what you *want* to happen, but I recognize it would be a visible<br>
change from today.<br></blockquote><div><br></div><div>If you have to choose between making requires threadsafe and enabling parallel requires, makingequires threadsafe clearly wins.</div><div>/div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


<div class="im"><br>
&gt;&gt; I think we could claim autoload is thread-safe in itself if it always<br>
&gt;&gt; just used a single lock. In other words, only one thread can be<br>
&gt;&gt; processing autoloads at a time. This doesn&#39;t fix the parallel require<br>
&gt;&gt; issue, but it would fix parallel autoloading.<br>
&gt;&gt;<br>
&gt;&gt; I think that&#39;s an ok fix, no?<br>
&gt;<br>
&gt; Agreed.   箼&gt;<br>
&gt; I&#39;ll create a new ticket for the change (single lock require.).  &gt; ticket is for backporting thread-safe autoload (unless you don&#39;t write<br>
&gt; &#39;wrong code&#39; :-) by Mike.  ¦   
&gt; thread-safe autoload on trunk works for him or not.<br>
<br>
</div>Agreed. At least we can say &quot;concurrent autoloads won&#39;t happen&quot; and as<br>
a result they&#39;re 100% safe. Concurrent requires still have concerns,<br>
but they&#39;re less likely to happen because people genreally don&#39;t do<br>
lazy requires in threads unless they&#39;re triggered by autoloads.<br></blockquote><div><br></div><div>For reasons I described earlier, Rails emulates autoload via requires, so any threadsafety fix that relies on autoload would either need to mitigate our need to emulate autoload (by allowing something like autoload &quot;Foo::Bar&quot;, &quot;foo/bar&quot;) or also support threadsafe require.</div>

<div><br></div><div>That said, I don&#39;t see why we couldn&#39;t support threadsafe require for cases where lazy require makes sense.</div><div>/div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

I think an additional warning about doing requires lazily might be<br>
useful...perhaps any require that happens on some non-main thread that<br>
*isn&#39;t* in response to a require should elicit a warning? Or perhaps<br>
when we detect concurrent non-autooad requires (i.e. two threads doing<br>
separate requires at the same time, regardless of the files they&#39;re<br>
loading) we should warn?<br></blockquote><div><br></div><div>See above. I would rather just get a single lock around require and make things safe.</div><div>/div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


<br>
- Charlie<br>
<br>
</blockquote></div><br>

--0016364eef7ab794c304b2eaad7b--