Mod_ruby post-request cleanup

This is very likely just a bug in my code, but I figured I’d ask, just to save
me tearing my hair out needlessly if it’s expected behaviour.

I’m trying to keep a pool of database connections persistent across requests.

I’m doing this via a class variable containing an hash of datasources that are
known about. Each hash entry contains an array of connections (which are
intended to be reused across requests). This actually all sits inside
another class, that handles the determination of what datasources are loaded
up, based on an XML configuration file, in case that’s important.

Somehow, the hash seems to be losing the keys from session to session, meaning
I’m re-connecting to the database all the time. As I say, this feels like
some kind of silly bug, but …

Just after sending the page back, via CGI.out(), I’m printing out what keys
are in the hash, and I can see the one I expect. However, at the start of
the next request, while the class variable still exists (and has the same
address), the keys seem to be gone.

As I say, it’s probably just a bug, but I figured I should check whether
mod_ruby does some kind of cleanup of non-class variables at the end of each
request, that I might be falling foul of.

I’m going to do some tests with something simpler, but I’d prefer to save
myself some messing about if there’s something mod_ruby is doing that I don’t
know about.

Cheers,

H.

[moving to modruby@modruby.net, remove ruby-talk from the address on reply]

I'm trying to keep a pool of database connections persistent across
requests.

I'm doing this via a class variable containing an hash of
datasources that are known about. Each hash entry contains an array
of connections (which are intended to be reused across requests).
This actually all sits inside another class, that handles the
determination of what datasources are loaded up, based on an XML
configuration file, in case that's important.

Nope, shouldn't be.

Somehow, the hash seems to be losing the keys from session to
session, meaning I'm re-connecting to the database all the time. As
I say, this feels like some kind of silly bug, but ...

Try using a global variable instead of a class variable. Are you
using apache/reload.rb? If you reload the class, the old class's
class variables should get nuked.

Just after sending the page back, via CGI.out(), I'm printing out
what keys are in the hash, and I can see the one I expect. However,
at the start of the next request, while the class variable still
exists (and has the same address), the keys seem to be gone.

This smells like it could be bugish... I wonder if the hash is being
marked with rb_gc_mark() instead of rb_gc_mark_children(). I wouldn't
call this a bug quite yet, but it could be.

As I say, it's probably just a bug, but I figured I should check
whether mod_ruby does some kind of cleanup of non-class variables at
the end of each request, that I might be falling foul of.

Nothing specifically for non-class variables that are outside of
normal scope rules.

I'm going to do some tests with something simpler, but I'd prefer to
save myself some messing about if there's something mod_ruby is
doing that I don't know about.

It's possible. -sc

···

--
Sean Chittenden

[ Sorry. I tried to send this to the mod_ruby ML, as Sean suggested, but even
though I receive messages sent to that list (which I would have thought means
I’m subscribed), for some reason it won’t let me post to it. ]

Try using a global variable instead of a class variable. Are you
using apache/reload.rb? If you reload the class, the old class’s
class variables should get nuked.

I’ve tried using a global variable and I see the same results (which I half
expected, since class variables are effectively global variables held inside
a different namespace … so to speak).

I checked and there’s no reference to apache/reload.rb in my httpd.conf, which
was based on the example that’s supplied with mod_ruby. All I’ve done is add
a RubyPath command and a RubyRequire.

What I’ll do is try to throw out all of my code and reproduce the problem with
a simple class and script.

This smells like it could be bugish… I wonder if the hash is being
marked with rb_gc_mark() instead of rb_gc_mark_children(). I wouldn’t
call this a bug quite yet, but it could be.

I wrote a simple class and it’s not just a hash that seems to not survive
across requests. Basically, every change that’s made to the object seems to
be lost.

It’s very weird, since all of the setup that’s done in the initialize() seems
to be maintained, even though the object isn’t getting reconstructed (I’ve
added a $stderr.puts into initialize() and it’s definitely only called once).

It’s as though mod_ruby is storing a clone of the object when it’s first
constructed and re-creating the variable from the clone in each session.

Anyway, I’ll come back when I have an example that’s simple enough to post on
the list. Who knows; maybe trying to create that example will highlight
what’s happening … I live in hope :-).

H.

···

On Wed, 9 Oct 2002 11:23, Sean Chittenden wrote: