svn commit: r1932225 - spamassassin/trunk/lib/Mail/SpamAssassin/Plugin

[email protected]
Newsgroups gmane.mail.spam.spamassassin.cvs
Message-ID <177304257865.1012631.10517584066868521363@svn02-us-east.apache.org>
Author: gbechis
Date: Mon Mar  9 07:49:38 2026
New Revision: 1932225

Log:
improve locking

Modified:
   spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/NeuralNetwork.pm

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/NeuralNetwork.pm
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/NeuralNetwork.pm	Mon Mar  9 07:05:10 2026	(r1932224)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/NeuralNetwork.pm	Mon Mar  9 07:49:38 2026	(r1932225)
@@ -615,35 +615,38 @@ sub learn_message {
   my @email_texts = map { $_->{text} } @training_data;
   my @labels = map { $_->{label} } @training_data;
 
+  my $lock_path = $dataset_path . '.lock';
+  $lock_path = Mail::SpamAssassin::Util::untaint_file_path($lock_path);
+  open(my $lock_fh, '>', $lock_path) or do {
+    info("Cannot open lock file '$lock_path': $!");
+    return;
+  };
+  flock($lock_fh, LOCK_EX) or do {
+    info("Cannot acquire lock on '$lock_path': $!");
+    close($lock_fh);
+    return;
+  };
+
   # Update the vocabulary
   my $update_vocab = 1;
 
   # Convert email text to numerical feature vectors
   my ($feature_vectors, $vocab_size, $vocab_keys_ref) = _text_to_features($self, $self->{main}->{conf}, $nn_data_dir, $update_vocab, $isspam, undef, @email_texts);
 
-  return unless $feature_vectors && @$feature_vectors;
+  unless ($feature_vectors && @$feature_vectors) {
+    close($lock_fh);
+    return;
+  }
 
   my $num_input = scalar(@{$feature_vectors->[0]{vec}});
   if ($num_input == 0) {
     dbg("No valid features found in message, skipping learning");
+    close($lock_fh);
     return;
   }
   my $num_hidden_neurons = int(sqrt($num_input)) || 1;
   my $num_output_neurons = 1;
 
-  # Reload model from disk if cache has expired
-  my $lock_path = $dataset_path . '.lock';
-  $lock_path = Mail::SpamAssassin::Util::untaint_file_path($lock_path);
-  open(my $lock_fh, '>', $lock_path) or do {
-    info("Cannot open lock file '$lock_path': $!");
-    return;
-  };
-  flock($lock_fh, LOCK_EX) or do {
-    info("Cannot acquire lock on '$lock_path': $!");
-    close($lock_fh);
-    return;
-  };
-
   my $network;
   if(defined $self->{neural_model} && $self->{neural_model}->num_inputs() == $num_input) {
     $network = $self->{neural_model};
@@ -1280,7 +1283,7 @@ sub _save_vocabulary_to_sql {
     my $count = 0;
 
     $self->{dbh}->begin_work();
-    foreach my $keyword (keys %{$terms}) {
+    foreach my $keyword (sort keys %{$terms}) {
       my $term_data = $terms->{$keyword};
       $sth_upsert->execute(
         lc($username),
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.