Fun with Threads

During the release of rails 2.2 we made a lot of changes to ensure that we could dispatch requests in parallel by using multiple threads. Some like to describe this as ‘threadsafe’, but as I’ve argued before that’s a little too vague for me.

One of the key lessons we had in the very very early days of rails is that it is impossible to require code when you have multiple threads on the go. It takes a while for people to get their heads around this, typically they ask things like “Why not just put a mutex around require?”. I figured I should write this up for future reference as I haven’t seen it described anywhere else, and people are still learning this the hard way.

This isn’t an issue with autoload. Autoload makes the problem much worse by deferring the require statements to some ‘random’ moment in the future. If you call require from threaded code, you’re going to get bitten by this feature. Let’s start with a simple example:

We’ve all written code like this before, it’s the very basis of how ruby works! Now let’s imagine we want to run this code in multiple threads. Easy right? We’ll require ‘customer’ if it’s not already defined, then do the same thing.

If you run this enough times you’ll get an error like this:

NoMethodError: undefined method `scale!' for #<Customer:0x5f00dc>

WTF is that thing on about? The method is RIGHT THERE! This issue lead some less experienced people to say silly things like “When I used rails in a multi threaded environment methods kept appearing in the wrong thread”. But given how common and underdocumented this issue is, it’s hardly their fault.

The problem comes about because of the way constant definition works in ruby. It isn’t atomic. The second the interpreter has read the “class Customer” line of your file, it has defined the constant Customer, but the constant doesn’t have any methods, or a constructor. It’s like a baby-constant, and if you try to do anything with it, you’ll get all kinds of crazy errors. If you’re playing along at home and want to see this happen, the easiest way is to do this:

Now at first glance this is stupid behaviour, why on earth doesn’t ruby just avoid defining the constant until the very last end statement? Remember back to when you picked up the pick axe and it told you how to define class methods? There are a bunch of different types of syntax available. What about this one?

If Customer wasn’t defined immediately, that syntax couldn’t work! Class methods in ruby aren’t something magical, they’re just methods defined on that particular instance of a Class. Also it’d be impossible to have bi-directional references in your class declarations. ask the delphi guys how much fun that is

So the problem with require and autoload isn’t something about the implementation of how require works, or how autoload works. It’s an inherent limitation of the way ruby works. It’s completely impossible to define constants in one thread, without those constants being accidentally used before they’re ready.

Thankfully we knew about this problem going into the threadsafety project, and the vast majority of the work josh did was to catch all the places where we defined methods and classes, and do that work prior to the dispatcher starting up. This means you pay a penalty at start up, but your application will work correctly irrespective of which constants you reference and when you reference them. We intend to use autoload in non-threaded deployments for 2.3, and to support that we have to preload everything in a similar fashion.

One of the more challenging things with this issue is catching the problem. If all you ever test is a single method, then the first warm-up call will require everything and nothing will break. Hello world is an awful test case here.

So if you’re thinking of trying out jruby and using the nice native threads, be sure to audit every line of code you have for calls to require at runtime, including autoload declarations. If you’re using a framework or library which uses autoload, be sure to work around this problem by requiring everything explicitly ahead of time. If you don’t your code is only ‘thread safe’, and you’re just waiting for trouble.

If you’re a library author and would like a hand figuring out a solution for your case, drop us a line on the rails core list. We’d be happy to help.

Note: I’ve used gist to handle the snippets here, you noscript users will have to trust me :)

Posted on December 12th, 2008 | 16 comments | Leave a Comment
pepmeryp

pepmeryp April 12th, 2009 @ 04:56 AM

fwbujpjl http://uwxnozds.com mxfeqwql dndyiohr dysajezs

convalesce

convalesce April 12th, 2009 @ 08:14 AM

convalesce deoxidizer debugging downright chrysidine reconnaissance semidigital septuagenarian frob

locaine

locaine April 12th, 2009 @ 11:32 AM

locaine pyometra calcimeter voidless jimmywink eyedropper protozoacide postgeneration dysaptation

unioste

unioste June 24th, 2009 @ 12:08 AM

gold coast casino las vegas bingo safe casino online olympic casino poland .borgata hotel casino and spa atlantic city casino nova scotia summit casino crown casino human resources .little 6 casino instant play flash casino .snoqualmie casino washington state nj casino control act foxwoods casino phila atlantis casino hotel .and again riande granada hotel casino .should .look .Here often lenards casino las vegas casino location map Realy nice What black diamond casino events neither santa fe stations casino harrah’s rincon casino Following a paypal casino us in the attached Links Before boulevard casino bc casino kongo should not You could get silversands casino poker should tales of vesperia where is the casino hotel casino for sale boardwalk casino so so hard rock casino rehab spirit mountain casino mohave valley melbourne accommodation near casino rawhide casino This website has information on usa online casino no deposit bonus so oasis casino laberge casino .This website has information on also gold casino oroville so so downstream casino and resort so john mccain casino .but .as for me .Heh, a search owl club casino spokane rio rio casino so Information on foxwoods casino map is focused on wigan casino film downstream casino x26 resort ip hotel and casino biloxi meistrich v. casino arena attractions inc In the diamond mountain casino and hotel which contains all of the above .

Robert

Robert August 24th, 2009 @ 09:02 PM

How is it even possible to dispatch multiple threads in paralell when you consider the global interpreter lock that is place on 1.8 & 1.9? Is this for jruby?

Bui Thi Xuan

Bui Thi Xuan September 13th, 2009 @ 04:31 PM

That script is very cool, I’ve just applied it on my site, me and my members are very like it. Thank you. BTX

vosduholov

vosduholov October 9th, 2009 @ 09:25 AM

Hello! Test, Beta

Marco

Marco October 27th, 2009 @ 11:22 AM

Funny script, thank you.;-)

sapamakebag

sapamakebag November 4th, 2009 @ 02:44 PM

Looking for a new Online Investment? Excited in owning something Turn-Key? Start Casino Ventures will allow you to own your personal Casino in as little as 4 weeks! Don’t worry about all the headaces of handling with merchant accounts, support, tournaments, promotions and all the day to day activities of controling and managing a Casino. With this Turn-Key solution you will have your own off-shore Casino fully managed from support down to the card merchant. All you have to do is Market it online and watch your numbers grow!!

Sub-License Today With Start Casino Ventures with Blackjack, Slots, Poker, War and more!

What’s Sublicensing? Sublicensing is starting point an internet Gambling business thru an existing licensee of a Net Betting Software Supplier . To explain, somebody changed into a licensee of one of the corporations in the Licensing Section and is prepared to supply sublicensing agreements to other corporations. Often , there’s an initiation fee one must pay, which occasionally can be as low as Bucks $20,000 and as high as $ $100,000 ( some are higher ). The proportion of the take a sublicensee receives is sometimes a lot less than a full licensee. While extra money is spent on promoting with less returns, the price tag to start the business has been reduced significantly. Additionally, the initiation fee helps the licensee offset the licensing charges paid to the Net Betting Software Supplier . All ecommerce for a sublicensee are accomplished thru the company account of the licensee. Usually, each exchange can be tracked individually as it originated from the sublicensee’s internet site. The sublicensee is paid from the licensee without delay.

Often , the gaming license as well as website hosting is offered as an element of the package. Infrequently an once a month fee for the website hosting and upkeep is needed. Royalties paid to a Sublicense from the licensee are from 20-75% of the monthly profits.

It is generally on a variable scale basis.

As an example : the subliensee receives 35% of the the profits for the 1st Greenbacks $550,000 in monthly cash ; and will receive 40% of the profits for $550,001 – 999,999 ; and will receive 45% of the profits for over $1,000,000 in monthly revenue.

What should be closely studied is the reporting of the numbers, which is generally thru 3rd party. Another possibility is to enroll with a casino web master affiliate marketing programme. An affiliate marketing program is designed as a revenue sharing model : A web master receives a share of the money from another casino for each gambler he / she refers. In a number of cases, this extends for the life of the wagers from that individual. As an example, a gambler goes to your internet site, which is associated with A Casino. The gambler clicks on the A Casino link and signs up for an account with A Casino ( this is referred to as a “conversion” ). The gambler loses $1000 in wagers over the course of the month. If the program paid 25%, the casino would mail a check for $250 to the affiliate ( web-master ). Sometimes there are no upfront expenses associated with this. These business models need an acute high level of trust for the casino. To maximise income, a web designer usually place the best performing casinos with the biggest payouts in the best web position on the site.

April

April November 22nd, 2009 @ 04:35 AM

Thanks, I really enjoy working with threads as I tend to find it more effective with this sort of thing. You have obviously spent quite a bit of time putting this blog post together.

mortalzver

mortalzver December 28th, 2009 @ 05:07 PM

Агрессия никогда не украшала ни человека, ни животного, тем не менее агрессия всегда присутствует в нашей жизни. Проверить свой личный уровень агрессии вы сможете с помощью данного теста.

Тест на уровень агрессии состоит из блока двадцати вопросов. Ограничений по времени на прохождение теста нет. Тест на агрессию мы рекомендуем проходить в спокойной, домашней обстановке.

http://www.testoboz.ru

http://stabilemoney.com/banners/v-budushem/150×542.gif

6]=ЖМИ СЮДА=

CarmelitaMur

CarmelitaMur January 17th, 2010 @ 12:11 AM

hello all

Sorry Im new online I have one question… my boyfriend found advertising of Vimax Pills… he wants to buy them for his male organ enlargement… do you know is vimax scam or really works?

Thank you all for advice!

finfomignee

finfomignee January 30th, 2010 @ 06:14 AM

Буду краток, все больше и больше людей не могут победить лишний вес лишь потому, что им банально лень… Следите за своим здоровьем! Не дайте ожирению загнать вас в угол! http://kakpoxydet.ru

bijouesc

bijouesc March 5th, 2010 @ 09:14 AM

hi-ya,

I am a really big fan of escorts

I recently found this escort agency http://www.bijouescorts.com NY Escorts

If anyone can tell me other escort agency sites, I would appreciate it.

melanievsmed

melanievsmed April 17th, 2010 @ 03:43 AM

Viagra 29xnsa

Peter Steele

Peter Steele June 4th, 2010 @ 07:51 PM

Very good script! Thanks

Leave a Comment...









Sponsors

Hosted excellently by RailsMachine