--- ../high_perf/lib/Danga/Socket.pm	2005-05-09 13:29:33.000000000 +0000
+++ lib/Danga/Socket.pm	2005-05-10 21:07:47.000000000 +0000
@@ -72,9 +72,11 @@
      %OtherFds,                  # A hash of "other" (non-Danga::Socket) file
                                  # descriptors for the event loop to track.
      $PostLoopCallback,          # subref to call at the end of each loop, if defined
+     $LocalPostLoopCallback,     # true if there is a local post loop callback in effect
      );
 
 %OtherFds = ();
+$LocalPostLoopCallback = 0;
 
 #####################################################################
 ### C L A S S   M E T H O D S
@@ -307,17 +309,18 @@
     # now we can close sockets that wanted to close during our event processing.
     # (we didn't want to close them during the loop, as we didn't want fd numbers
     #  being reused and confused during the event loop)
-    while(my $j = shift @ToClose) {
-        $j->[1]->close();
-        $j->[0]->{closing} = 0;
+    foreach my $f (@ToClose) {
+        close($f);
     }
-
+    @ToClose = ();
 
     # now we're at the very end, call per-connection callbacks if defined
-    for my $fd (%DescriptorMap) {
-        my $pob = $DescriptorMap{$fd};
-        if( defined $pob->{post_loop_callback} ) {
-            return unless $pob->{post_loop_callback}->(\%DescriptorMap, \%OtherFds);
+    if ($LocalPostLoopCallback) {
+        for my $fd (%DescriptorMap) {
+            my $pob = $DescriptorMap{$fd};
+            if( defined $pob->{post_loop_callback} ) {
+                return unless $pob->{post_loop_callback}->(\%DescriptorMap, \%OtherFds);
+            }
         }
     }
 
@@ -485,7 +488,7 @@
 
     # defer closing the actual socket until the event loop is done
     # processing this round of events.  (otherwise we might reuse fds)
-    push @ToClose, [$self,$sock];
+    push @ToClose, $sock;
 
     return 0;
 }
@@ -801,10 +804,12 @@
     if(ref $class) {
         my Danga::Socket $self = $class;
         if( defined $ref && ref $ref eq 'CODE' ) {
+            $LocalPostLoopCallback++;
             $self->{post_loop_callback} = $ref;
         }
         else {
+            $LocalPostLoopCallback--;
             delete $self->{post_loop_callback};
         }
     }
     else {
