Fixing autoload issues with jruby-rack

I’m looking at how to fix the case where multiple servlet requests hit
around the same time on startup, which invariably causes classes to
not get autoloaded correctly when multiple threads are involved.

The approach I’m looking at right now is to use a atomic global
integer counter to track the state of the first call to the servlet
init method.

  • If the counter is 0, set it to 1 and let the init run. When init is
    finished, increment the counter to 2.
  • If the counter is 1 on entry, wait until it has incremented to 2,
    then run init.
  • If the counter is 2 on entry, let init run normally

One thing I don’t know is what tomcat’s behavior is if I just raise an
exception in init instead of waiting. I’m assuming it might just kill
the thread, start a new one, and call init on it. This could cause a
ton of cpu usage until init stops throwing exceptions as tomcat spins
up one thread after another.

Any suggestions/advice appreciated…

Also, I’m working with 1.0.9. Later versions broke custom logging and
I don’t have time to sort that one out right now.

Chris

On Mon, Feb 27, 2012 at 4:28 PM, snacktime [email protected] wrote:

I’m looking at how to fix the case where multiple servlet requests hit
around the same time on startup, which invariably causes classes to
not get autoloaded correctly when multiple threads are involved.

Is this still a problem with JRuby 1.6.6+, where we make concurrent
hits to the same autoload block concurrent threads (so-called
“thread-safe” autoload)?

The approach I’m looking at right now is to use a atomic global
integer counter to track the state of the first call to the servlet
init method.

  • If the counter is 0, set it to 1 and let the init run. When init is
    finished, increment the counter to 2.
  • If the counter is 1 on entry, wait until it has incremented to 2,
    then run init.
  • If the counter is 2 on entry, let init run normally

If these are requires of the same file, threads should not be allowed
to run the same file at the same time. The first thread will actually
start loading the file, and other threads will block waiting for it to
complete. Isn’t that the case?

One thing I don’t know is what tomcat’s behavior is if I just raise an
exception in init instead of waiting. I’m assuming it might just kill
the thread, start a new one, and call init on it. This could cause a
ton of cpu usage until init stops throwing exceptions as tomcat spins
up one thread after another.

Any suggestions/advice appreciated…

Also, I’m working with 1.0.9. Later versions broke custom logging and
I don’t have time to sort that one out right now.

I need more concrete details, at least a hypothetical set of files
(matching your scenario) that get hit concurrently.

  • Charlie

Is this still a problem with JRuby 1.6.6+, where we make concurrent
hits to the same autoload block concurrent threads (so-called
“thread-safe” autoload)?

Actually, now that we have been running 1.6.7, I can’t find a case of
this. We had a couple of times when someone assumed it was still
happening, but on further investigation it was something else.

Chris