Forum: Ruby on Rails Using SCGI Wrong Causes Bad Breath

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
zedshaw (Guest)
on 2005-12-04 22:57
(Received via mailing list)
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
http://www.zedshaw.com/
ryan (Guest)
on 2005-12-05 06:16
(Received via mailing list)
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
ezra (Guest)
on 2005-12-05 10:38
(Received via mailing list)
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.
>

<snip>

LOL!

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
This topic is locked and can not be replied to.