🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.nntp
5 messages
5 total messages Started by Dennis Davis Tue, 08 Nov 2011 10:40
The mailpost perl script from inn-2.5.3rc1.
#84484
Author: Dennis Davis
Date: Tue, 08 Nov 2011 10:40
92 lines
3553 bytes
I've been looking at the mailpost perl script from inn-2.5.3rc1.  As
previously discussed here, the locking has been switched to use a
perl wrapper for INN's shlock program.  So locking the Message-ID
database now looks like:

INN::Utils::Shlock::lock($lockfile, 5) or mailArtAndDie ("cannot create lockfile $lockfile");

...do stuff with the database locked...

INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile ");

Code similar to the above occurs in a couple of places.

There's a slight problem with the above code.  While doing stuff
with the database locked, error conditions can cause the script to
terminate early.  Typically this is done by calling mailArtAndDie.
This will leave a lock in place, which is untidy.  This isn't a big
problem as the shlock program will detect and remove stale locks.
The simple patches appended below will try to prevent stale locks
being left behind.


--- mailpost.orig	Wed Oct 26 09:48:52 2011
+++ mailpost	Mon Nov  7 10:17:41 2011
@@ -22,15 +22,11 @@
 use strict;
 
 my $debugging = 0 ;
+my $locked = 0 ;
 my $tmpfile ;
 my $tmpfile2 ;
 my $msg ;
 
-END {
-    unlink ($tmpfile) if $tmpfile ;		# in case we die()
-    unlink ($tmpfile2) if $tmpfile2 ;		# in case we die()
-}
-
 my $usage = $0 ;
 $usage =~ s!.*/!! ;
 my $prog = $usage ;
@@ -379,6 +375,7 @@
 
 # Acquire a lock.
 INN::Utils::Shlock::lock($lockfile, 5) or mailArtAndDie ("cannot create lockfile $lockfile");
+$locked = 1;
 
 my %DATABASE ;
 dbmopen(%DATABASE, $Database, 0666) || mailArtAndDie ("can't dbmopen $Database: $!");
@@ -405,6 +402,7 @@
 
 # Unlock.
 INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile");
+$locked = 0;
 
 ## For crosspost.
 
@@ -431,6 +429,7 @@
 
   # Acquire a lock.
   INN::Utils::Shlock::lock($lockfile, 5) or mailArtAndDie ("cannot create lockfile $lockfile");
+  $locked = 1;
 
   my $umask_bak = umask();
   umask(000);
@@ -445,6 +444,7 @@
 
   # Unlock.
   INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile");
+  $locked = 0;
 
   if (defined($dup)) {
     syslog("err", "mismatch $newsgroups") if $debugging && $use_syslog;
@@ -597,6 +597,15 @@
   }
   return join ",", @orig ;
 
+}
+
+END {
+    unlink ($tmpfile) if $tmpfile ;		# in case we die()
+    unlink ($tmpfile2) if $tmpfile2 ;		# in case we die()
+    #
+    # in case we bale out, typically by calling mailArtAndDie, while
+    # holding a lock.
+    INN::Utils::Shlock::unlock($lockfile) if $locked ;
 }
 
 =head1 NAME
-- 
Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK
D.H.Davis@bath.ac.uk
Re: The mailpost perl script from inn-2.5.3rc1.
#84488
Author: Dennis Davis
Date: Fri, 11 Nov 2011 16:36
16 lines
1595 bytes
In article <LuC8By.M8s.B.shoe@bath.ac.uk>,
Dennis Davis  <D.H.Davis@bath.ac.uk> wrote:
>I've been looking at the mailpost perl script from inn-2.5.3rc1.

...

Small progress report:

I'm currently running an INN-2.5.2 system on Solaris.  I've manually
installed the INN::Utils::Shlock from inn-2.5.3rc1 on this system.
So now I'm using my modified mailpost perl script from inn-2.5.3rc1.
It's being used as a gateway function to convert a mailing list into
a moderated Usenet Newsgroup.  Seems to work a treat.
-- 
Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK
D.H.Davis@bath.ac.uk
Re: The mailpost perl script from inn-2.5.3rc1.
#84506
Author: Julien ÉLIE
Date: Wed, 16 Nov 2011 21:58
48 lines
2903 bytes
Hi Dennis,

> While doing stuff
> with the database locked, error conditions can cause the script to
> terminate early.  Typically this is done by calling mailArtAndDie.
> This will leave a lock in place, which is untidy.  This isn't a big
> problem as the shlock program will detect and remove stale locks.
> The simple patches appended below will try to prevent stale locks
> being left behind.

The same problem affects:
- contrib/thdexpire.in
- control/controlchan.in + newgroup.pl and rmgroup.pl modules
- frontends/pullnews.in

(backends/send-uucp.in already deals with the "untidy exit".)



>   # Acquire a lock.
>   INN::Utils::Shlock::lock($lockfile, 5) or mailArtAndDie ("cannot create lockfile $lockfile");
> + $locked = 1;

[...]

>   # Unlock.
>   INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile");
> + $locked = 0;

[...]

> +END {
> +    INN::Utils::Shlock::unlock($lockfile) if $locked;
>   }

Instead of using a $locked variable, wouldn't it be better to add a 
INN::Utils::Shlock::releaselocks() function?

INN::Utils::Shlock would then keep track of still used lock files during 
the run of a Perl script (through an array because several lock files 
can exist at the same time for a script).
Then, it would be enough to add INN::Utils::Shlock::releaselocks() in an 
END section in all the Perl scripts using this module.

-- 
Julien ÉLIE

« Videt non te diu. »
Re: The mailpost perl script from inn-2.5.3rc1.
#84507
Author: Dennis Davis
Date: Thu, 17 Nov 2011 16:36
70 lines
3011 bytes
In article <ja1blc$13g$1@news.trigofacile.com>,
Julien ÉLIE  <iulius@nom-de-mon-site.com.invalid> wrote:

...

>Instead of using a $locked variable, wouldn't it be better to add a 
>INN::Utils::Shlock::releaselocks() function?
>
>INN::Utils::Shlock would then keep track of still used lock files during 
>the run of a Perl script (through an array because several lock files 
>can exist at the same time for a script).
>Then, it would be enough to add INN::Utils::Shlock::releaselocks() in an 
>END section in all the Perl scripts using this module.

That's a much better idea.  Why didn't I think of it :-(

The patches below are for INN::Utils::Shlock and are a quick
and dirty attempt to implement this functionality.  I haven't
added anything to the manul page, although this does need a few
descriptive lines.


*** Shlock.pm.orig	Wed Oct 26 09:48:33 2011
--- Shlock.pm	Thu Nov 17 16:25:33 2011
***************
*** 15,21 ****
--- 15,23 ----
  our @ISA = qw(Exporter);
  our $VERSION = "$INN::Config::VERSION";
  
+ my %lockfiles;
  
+ 
  ##
  ##  Returns true if the file is properly locked.
  ##
***************
*** 30,35 ****
--- 32,38 ----
  
      while ($locktry < $lockretrymax) {
          if (system("$INN::Config::newsbin/shlock", '-p', $$, '-f', $lockfile) == 0) {
+             $lockfiles{$lockfile} = 1;
              return 1;
          }
          $locktry++;
***************
*** 49,54 ****
--- 52,69 ----
      return unlink $lockfile;
  }
  
+ ##
+ ## Attempt to unlock any leftover locks.
+ ##
+ sub releaselocks {
+     my $key;
+     foreach $key (keys(%lockfiles)) {
+         unlock($key);
+     }
+     undef(%lockfiles);
+     return 1;
+ }
+ 
  
  ##  This array will contain what it is possible to export.
  our @EXPORT_OK = qw(lock unlock);
-- 
Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK
D.H.Davis@bath.ac.uk
Re: The mailpost perl script from inn-2.5.3rc1.
#84901
Author: Julien ÉLIE
Date: Mon, 28 May 2012 18:38
112 lines
4278 bytes
Hi Dennis,

Sorry for my very late reply.
I have just managed to have a look at it.


>> INN::Utils::Shlock would then keep track of still used lock files during
>> the run of a Perl script (through an array because several lock files
>> can exist at the same time for a script).
>> Then, it would be enough to add INN::Utils::Shlock::releaselocks() in an
>> END section in all the Perl scripts using this module.
>
> That's a much better idea.  Why didn't I think of it :-(
>
> The patches below are for INN::Utils::Shlock and are a quick
> and dirty attempt to implement this functionality.  I haven't
> added anything to the manul page, although this does need a few
> descriptive lines.

OK, many thanks for your suggestion.

I also changed the unlock() function to delete successfully released 
lock files from the %lockfiles hash.
Otherwise, we might get into concurrent accesses (when instance #1 of a 
script exits, it calls releaselocks() and could erase a possible valid 
lock file of instance #2 of the same script running at the same time).

Suggested change is:

  sub unlock {
      my $lockfile = shift;
-    return unlink $lockfile;
+    if (unlink $lockfile) {
+        delete $lockfiles{$lockfile};
+        return 1;
+    } else {
+        return 0;
+    }
  }



> + ##
> + ## Attempt to unlock any leftover locks.
> + ##
> + sub releaselocks {
> +     my $key;
> +     foreach $key (keys(%lockfiles)) {
> +         unlock($key);
> +     }
> +     undef(%lockfiles);
> +     return 1;
> + }

I modified the return value so as to obtain additional information.

+##
+##  Attempts to unlock any leftover locks.
+##  Returns the number of removed locks.
+##
+sub releaselocks {
+    my $key;
+    my $count = 0;
+
+    foreach $key (keys(%lockfiles)) {
+        $count += unlock($key);
+    }
+
+    undef(%lockfiles);
+    return $count;
+}


And also:

-our @EXPORT_OK = qw(lock unlock);
+our @EXPORT_OK = qw(lock unlock releaselocks);


Documentation:

Instead of calling unlock(I<lockfile>), the releaselocks()
function can be called.  It removes any leftover locks, which is useful
when several different locks are used.  Another possible use is to call
it in an END code block:

     END {
         # In case we bail out, while holding a lock.
         INN::Utils::Shlock::releaselocks();
     }


=item releaselocks()

Removes all the lock files previously created by calling the
lock(I<lockfile>) function.

This function returns the number of removed lock files.






I hope that's fine for you.  It will be shipped with INN 2.5.3.
Thanks again for your useful feedback about the 2.5.3rc1 version 
(release candidate).

-- 
Julien ÉLIE

« Etna : lave dévalante. »
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads