diff -ru Mail-SpamAssassin-3.2.3.orig/lib/Mail/SpamAssassin/BayesStore/DBM.pm Mail-SpamAssassin-3.2.3/lib/Mail/SpamAssassin/BayesStore/DBM.pm
--- Mail-SpamAssassin-3.2.3.orig/lib/Mail/SpamAssassin/BayesStore/DBM.pm	2007-08-08 08:19:09.000000000 -0500
+++ Mail-SpamAssassin-3.2.3/lib/Mail/SpamAssassin/BayesStore/DBM.pm	2007-08-31 16:05:56.000000000 -0500
@@ -279,7 +279,11 @@
     my $name = $path.'_'.$dbname;
     my $db_var = 'db_'.$dbname;
     dbg("bayes: tie-ing to DB file R/W $name");
-    tie %{$self->{$db_var}},$self->DBM_MODULE,$name, O_RDWR|O_CREAT,
+
+    ($self->DBM_MODULE eq 'DB_File') and
+         Mail::SpamAssassin::Util::avoid_db_file_locking_bug ($name);
+
+    tie %{$self->{$db_var}},$self->DBM_MODULE, $name, O_RDWR|O_CREAT,
 		 (oct($main->{conf}->{bayes_file_mode}) & 0666)
        or goto failed_to_tie;
   }
diff -ru Mail-SpamAssassin-3.2.3.orig/lib/Mail/SpamAssassin/DBBasedAddrList.pm Mail-SpamAssassin-3.2.3/lib/Mail/SpamAssassin/DBBasedAddrList.pm
--- Mail-SpamAssassin-3.2.3.orig/lib/Mail/SpamAssassin/DBBasedAddrList.pm	2007-08-08 08:19:15.000000000 -0500
+++ Mail-SpamAssassin-3.2.3/lib/Mail/SpamAssassin/DBBasedAddrList.pm	2007-08-31 16:05:56.000000000 -0500
@@ -82,15 +82,19 @@
 
     dbg("auto-whitelist: tie-ing to DB file of type $dbm_module $mod1 in $path");
 
+    ($self->{is_locked} && $dbm_module eq 'DB_File') and 
+            Mail::SpamAssassin::Util::avoid_db_file_locking_bug ($path);
+
     if (! tie %{ $self->{accum} }, $dbm_module, $path, $mod2,
-      oct($main->{conf}->{auto_whitelist_file_mode}) ) {
-        my $err = $!;   # might get overwritten later
-        if ($self->{is_locked}) {
-          $self->{main}->{locker}->safe_unlock($self->{locked_file});
-          $self->{is_locked} = 0;
-        }
-        die "auto-whitelist: cannot open auto_whitelist_path $path: $err\n";
+            oct($main->{conf}->{auto_whitelist_file_mode}) )
+    {
+      my $err = $!;   # might get overwritten later
+      if ($self->{is_locked}) {
+        $self->{main}->{locker}->safe_unlock($self->{locked_file});
+        $self->{is_locked} = 0;
       }
+      die "auto-whitelist: cannot open auto_whitelist_path $path: $err\n";
+    }
   }
 
   bless ($self, $class);
diff -ru Mail-SpamAssassin-3.2.3.orig/lib/Mail/SpamAssassin/Util.pm Mail-SpamAssassin-3.2.3/lib/Mail/SpamAssassin/Util.pm
--- Mail-SpamAssassin-3.2.3.orig/lib/Mail/SpamAssassin/Util.pm	2007-08-08 08:19:15.000000000 -0500
+++ Mail-SpamAssassin-3.2.3/lib/Mail/SpamAssassin/Util.pm	2007-08-31 16:39:24.000000000 -0500
@@ -57,6 +57,7 @@
 
 use Config;
 use File::Spec;
+use File::Basename;
 use Time::Local;
 use Sys::Hostname (); # don't import hostname() into this namespace!
 use Fcntl;
@@ -1551,6 +1552,43 @@
 
 ###########################################################################
 
+# bug 5612: work around for bugs in Berkeley db 4.2
+#
+# on 4.2 having the __db.[DBNAME] file will cause an loop that will never finish
+# on 4.3+ the loop will timeout after 301 open attempts, but we will still
+# be unable to open the database.  This workaround solves both problems. 
+#
+sub avoid_db_file_locking_bug {
+  my $path = shift;
+  
+  my $db_tmp_file = '__db.'.basename($path);
+  my $db_tmp_file_match = $db_tmp_file . '.';
+
+  my $parentdir = dirname($path);
+
+  # The db bug is actually more pervasive then orignally thought we can also have __db.[DBNAME].* as well.
+
+  opendir(my $sa_db_dir_fh, $parentdir);
+  while(my $test_filename = readdir($sa_db_dir_fh)) {  
+	# If the file is __db.[DBNAME] or the file is __db.[DBNAME].* (glob) then we need to remove it
+	# we use substr here as its slightly fater then using a regex 
+
+	my $newtest = substr($test_filename,0,length($db_tmp_file_match));
+
+	if ( substr($test_filename,0,length($db_tmp_file_match)) eq $db_tmp_file_match || $test_filename eq $db_tmp_file ) {
+		my $db_tmpfile = untaint_file_path( File::Spec->catfile($parentdir,$test_filename) );
+		
+		dbg("Berkeley DB bug work-around: cleaning tmp file $db_tmpfile");
+		if (!unlink($db_tmpfile)) {
+			die "cannot remove Berkeley DB tmp file $db_tmpfile: $!\n";
+		}
+	}
+  }
+  closedir($sa_db_dir_fh);
+}
+
+###########################################################################
+
 1;
 
 =back
