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.


In reply to AoH Bug in Apache::Session? by hardburn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.