hardburn has asked for the wisdom of the Perl Monks concerning the following question:

I believe I've found a bug in Apache::Session, but I'm asking here in case I'm doing something stupid. From the Apache::Session docs:

As you put data into the session hash, Session squirrels it away for later use. When you untie() the session hash, or it passes out of scope, Session checks to see if anything has changed. If so, Session gains an exclusive lock and writes the session to the data store. It then releases any locks it has acquired.

However, it appears that it does not correctly check for differences in an array-of-hashes. I've done a little digging into Apache::Session's tests, and they do not appear to cover more complex data structures.

The series of (long) one-liners below reproduce the bug and show a work-around. I've added comments to make each snippit more clear. Sorry about the long lines.

# Create empty session # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", undef, {FileName=>"/tmp/sessions/test/"}; print D +umper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d' }; # Open that session for preliminary testing # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"}; print Dumper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d' }; # Add simple scalar data to session # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"}; $s{t} = 1; print Dumper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 't' => 1 }; # Re-open session to check for added data # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"};print Dumper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 't' => 1 }; # Start on our AoH # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"}; $s{m} = [{one => 1}]; print Dumper(\%s); untie +%s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 'm' => [ { 'one' => 1 } ], 't' => 1 }; # Add another entry to our AoH. Notice that Dumper # says it's there before we untie(). # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"}; push @{$s{m}}, {two=>2}; print Dumper(\%s); unt +ie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 'm' => [ { 'one' => 1 }, { 'two' => 2 } ], 't' => 1 }; # Get back the session. Notice that the second entry # in the AoH is missing. # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"};print Dumper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 'm' => [ { 'one' => 1 } ], 't' => 1 }; # Work-around--create a dummy parameter which forces # Apache::Session to realize that the data has changed. # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"}; push@{$s{m}}, {two => 2}; $s{dummy} = 1; print +Dumper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 'dummy' => 1, 'm' => [ { 'one' => 1 }, { 'two' => 2 } ], 't' => 1 }; # Re-open session to check for new data. Notice that # the AoH was saved properly due to the dummy param. # $ perl -MApache::Session::File -MData::Dumper=Dumper -e 'tie %s, "Apac +he::Session::File", "41b4260f41720aed6b24e802a41ad57d", {FileName=>"/ +tmp/sessions/test/"}; print Dumper(\%s); untie %s;' $VAR1 = { '_session_id' => '41b4260f41720aed6b24e802a41ad57d', 'm' => [ { 'one' => 1 }, { 'two' => 2 } ], 'dummy' => 1, 't' => 1 };

----
send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Replies are listed 'Best First'.
Re: AoH Bug in Apache::Session?
by waswas-fng (Curate) on Jul 26, 2004 at 15:16 UTC
    From the A::S pod:

    Note that Apache::Session does only a shallow check to see if anything has changed. If nothing changes in the top level tied hash, the data will not be updated in the backing store. You are encouraged to timestamp the session hash so that it is sure to be updated.


    -Waswas

      Thanks. Figured it'd be something I missed.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.