Using SCGI Wrong Causes Bad Breath


#1

Hi Folks,

I’m currently working up the TODO list for the next round of SCGI
development. What I’d like to do right now though is warn people about
a gotcha regarding Dir.chdir.

PROBLEM

If you are using Dir.chdir in your Rails application then you’ll run
into a very weird indirect bug where Rails has problems loading random
lib/ or other .rb files like controllers and such. What happens is
this moves the Ruby process to your chosen directory and then
Rails can’t dynamically load any of it’s dependency files like
controllers and libs.

SOLUTION

This guy walks into the doctor's office and he says, "Doc, my

arm hurts when I do this." He then rams his arm up his ass,
grabbing his tonsils and scrubbing his toungue.
The doctor says, “Yikes! What the hell are you doing that
for?!”
The guy responds, “It’s easier than brushing my teeth.”

The point of the joke is that you’re running a long running Ruby
process that handles tons of requests in a stateless manner. If you
write your code under the assumption that you’re running a regular CGI
(which runs once) then you’ll have lots of problems. Changing
directories, not closing files, putting stuff in thread local,
using class variables (beware the @@), and
preventing the GC from releasing things will cause you lots of trouble.

Any reason you give for doing this inside a RoR app is just as retarded
as the guy in the joke. All you get is bad breath. Just don’t do it
as there’s easier ways which work just as well.

For example, did you know that you can pass a block to Dir.chdir and
it’ll work like a pushd? So, Dir.chdir(“someplace”) { do_stuff() }
will change to “someplace”, run do_stuff(), and come back. Even then I
would avoid this practice since you don’t know how it will impact other
parts of Rails and it’s too easy to forget.

I’m planning to include a few safety features to support this kind of
bad coding, but I’m also going to try putting in some whiny log
messages in order to encourage people to quit. It’s also a good case
for doing your development in as close a configuration to production as
you can. I’ve switched to SCGI full time for both production and
development work and it quickly catches errors like this.

As a side note, please e-mail me your list of requests for SCGI.
Thanks.

Zed A. Shaw


#2

Thanks for this post… I’ve never laughed so much reading a dev
mailing
list…

–Ryan

----- Original Message -----
From: “Zed A. Shaw” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Sunday, December 04, 2005 3:56 PM
Subject: [Rails] Using SCGI Wrong Causes Bad Breath


#3

On Dec 4, 2005, at 12:56 PM, Zed A. Shaw wrote:

Hi Folks,

I’m currently working up the TODO list for the next round of SCGI
development. What I’d like to do right now though is warn people
about
a gotcha regarding Dir.chdir.

LOL!

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732