534e90
From d4499d6a6f8007df03fe5292aab4ba0367499dd0 Mon Sep 17 00:00:00 2001
534e90
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
534e90
Date: Tue, 10 Jun 2014 14:28:09 +0200
534e90
Subject: [PATCH] Destroy DB_File objects only from original thread context
534e90
MIME-Version: 1.0
534e90
Content-Type: text/plain; charset=UTF-8
534e90
Content-Transfer-Encoding: 8bit
534e90
534e90
This patch fixes a crash when destroing a hash tied to a DB_File
534e90
database after spawning a thread:
534e90
534e90
use Fcntl;
534e90
use DB_File;
534e90
use threads;
534e90
tie(my %dbtest, 'DB_File', "test.db", O_RDWR|O_CREAT, 0666);
534e90
threads->new(sub {})->join;
534e90
534e90
This crashed or paniced depending on how perl was configured.
534e90
534e90
Closes RT#61912.
534e90
534e90
Signed-off-by: Petr Písař <ppisar@redhat.com>
534e90
---
534e90
 DB_File.xs     | 50 +++++++++++++++++++++++++++++++-------------------
534e90
 MANIFEST       |  1 +
534e90
 t/db-threads.t | 46 ++++++++++++++++++++++++++++++++++++++++++++++
534e90
 3 files changed, 78 insertions(+), 19 deletions(-)
534e90
 create mode 100644 t/db-threads.t
534e90
534e90
diff --git a/DB_File.xs b/DB_File.xs
534e90
index 679c416..762f4ed 100755
534e90
--- a/DB_File.xs
534e90
+++ b/DB_File.xs
534e90
@@ -397,6 +397,7 @@ typedef union INFO {
534e90
 
534e90
 typedef struct {
534e90
 	DBTYPE	type ;
534e90
+	tTHX    owner ;
534e90
 	DB * 	dbp ;
534e90
 	SV *	compare ;
534e90
 	bool	in_compare ;
534e90
@@ -983,6 +984,7 @@ SV *   sv ;
534e90
 		    name, flags, mode, sv == NULL) ;  
534e90
 #endif
534e90
     Zero(RETVAL, 1, DB_File_type) ;
534e90
+    RETVAL->owner = aTHX;
534e90
 
534e90
     /* Default to HASH */
534e90
     RETVAL->filtering = 0 ;
534e90
@@ -1255,6 +1257,7 @@ SV *   sv ;
534e90
 
534e90
 /* printf("In ParseOpenInfo name=[%s] flags=[%d] mode = [%d]\n", name, flags, mode) ;  */
534e90
     Zero(RETVAL, 1, DB_File_type) ;
534e90
+    RETVAL->owner = aTHX;
534e90
 
534e90
     /* Default to HASH */
534e90
     RETVAL->filtering = 0 ;
534e90
@@ -1571,27 +1574,36 @@ db_DESTROY(db)
534e90
 	INIT:
534e90
 	  CurrentDB = db ;
534e90
 	  Trace(("DESTROY %p\n", db));
534e90
-	CLEANUP:
534e90
-	  Trace(("DESTROY %p done\n", db));
534e90
-	  if (db->hash)
534e90
-	    SvREFCNT_dec(db->hash) ;
534e90
-	  if (db->compare)
534e90
-	    SvREFCNT_dec(db->compare) ;
534e90
-	  if (db->prefix)
534e90
-	    SvREFCNT_dec(db->prefix) ;
534e90
-	  if (db->filter_fetch_key)
534e90
-	    SvREFCNT_dec(db->filter_fetch_key) ;
534e90
-	  if (db->filter_store_key)
534e90
-	    SvREFCNT_dec(db->filter_store_key) ;
534e90
-	  if (db->filter_fetch_value)
534e90
-	    SvREFCNT_dec(db->filter_fetch_value) ;
534e90
-	  if (db->filter_store_value)
534e90
-	    SvREFCNT_dec(db->filter_store_value) ;
534e90
-	  safefree(db) ;
534e90
+	CODE:
534e90
+	  RETVAL = 0;
534e90
+	  if (db && db->owner == aTHX) {
534e90
+	    RETVAL = db_DESTROY(db);
534e90
 #ifdef DB_VERSION_MAJOR
534e90
-	  if (RETVAL > 0)
534e90
-	    RETVAL = -1 ;
534e90
+	    if (RETVAL > 0)
534e90
+		RETVAL = -1 ;
534e90
 #endif
534e90
+	  }
534e90
+	OUTPUT:
534e90
+	  RETVAL
534e90
+	CLEANUP:
534e90
+	  Trace(("DESTROY %p done\n", db));
534e90
+	  if (db && db->owner == aTHX) {
534e90
+	    if (db->hash)
534e90
+		SvREFCNT_dec(db->hash) ;
534e90
+	    if (db->compare)
534e90
+		SvREFCNT_dec(db->compare) ;
534e90
+	    if (db->prefix)
534e90
+		SvREFCNT_dec(db->prefix) ;
534e90
+	    if (db->filter_fetch_key)
534e90
+		SvREFCNT_dec(db->filter_fetch_key) ;
534e90
+	    if (db->filter_store_key)
534e90
+		SvREFCNT_dec(db->filter_store_key) ;
534e90
+	    if (db->filter_fetch_value)
534e90
+		SvREFCNT_dec(db->filter_fetch_value) ;
534e90
+	    if (db->filter_store_value)
534e90
+		SvREFCNT_dec(db->filter_store_value) ;
534e90
+	    safefree(db) ;
534e90
+	  }
534e90
 
534e90
 
534e90
 int
534e90
diff --git a/MANIFEST b/MANIFEST
534e90
index e460e81..47f43f7 100644
534e90
--- a/MANIFEST
534e90
+++ b/MANIFEST
534e90
@@ -27,6 +27,7 @@ t/db-btree.t
534e90
 t/db-hash.t
534e90
 t/db-recno.t
534e90
 t/pod.t
534e90
+t/db-threads.t
534e90
 typemap
534e90
 version.c
534e90
 META.yml                                 Module meta-data (added by MakeMaker)
534e90
diff --git a/t/db-threads.t b/t/db-threads.t
534e90
new file mode 100644
534e90
index 0000000..b9f69b6
534e90
--- /dev/null
534e90
+++ b/t/db-threads.t
534e90
@@ -0,0 +1,46 @@
534e90
+#!./perl
534e90
+
534e90
+use warnings;
534e90
+use strict;
534e90
+use Config;
534e90
+use Fcntl;
534e90
+use Test::More;
534e90
+use DB_File;
534e90
+
534e90
+if (-d "lib" && -f "TEST") {
534e90
+    if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
534e90
+        plan skip_all => 'DB_File was not built';
534e90
+    }
534e90
+}
534e90
+plan skip_all => 'Threads are disabled'
534e90
+    unless $Config{usethreads};
534e90
+
534e90
+plan tests => 7;
534e90
+
534e90
+# Check DBM back-ends do not destroy objects from then-spawned threads.
534e90
+# RT#61912.
534e90
+use_ok('threads');
534e90
+
534e90
+my %h;
534e90
+unlink <threads*>;
534e90
+
534e90
+my $db = tie %h, 'DB_File', 'threads', O_RDWR|O_CREAT, 0640;
534e90
+isa_ok($db, 'DB_File');
534e90
+
534e90
+for (1 .. 2) {
534e90
+    ok(threads->create(
534e90
+        sub {
534e90
+            $SIG{'__WARN__'} = sub { fail(shift) }; # debugging perl panics
534e90
+                # report it by spurious TAP line
534e90
+            1;
534e90
+        }), "Thread $_ created");
534e90
+}
534e90
+for (threads->list) {
534e90
+    is($_->join, 1, "A thread exited successfully");
534e90
+}
534e90
+
534e90
+pass("Tied object survived exiting threads");
534e90
+
534e90
+undef $db;
534e90
+untie %h;
534e90
+unlink <threads*>;
534e90
-- 
534e90
1.9.3
534e90