0e6766
From faa03ffb8ccbf754d38d041570fcf2ce8816f36b Mon Sep 17 00:00:00 2001
0e6766
From: =?UTF-8?q?Petr=20=C5=A0abata?= <contyk@redhat.com>
0e6766
Date: Wed, 2 Sep 2015 16:24:58 +0200
0e6766
Subject: [PATCH] File::Glob: Dup glob state in CLONE()
0e6766
MIME-Version: 1.0
0e6766
Content-Type: text/plain; charset=UTF-8
0e6766
Content-Transfer-Encoding: 8bit
0e6766
0e6766
File::Glob: Dup glob state in CLONE()
0e6766
0e6766
This solves [perl #119897] and [perl #117823], and restores the
0e6766
behavior of glob() in conjunction with threads of 5.14 and older.
0e6766
0e6766
Since 5.16, code that used glob() inside a thread had been
0e6766
unintentionally sharing state between threads, which lead to things
0e6766
like this crashing and failing assertions:
0e6766
0e6766
./perl -Ilib -Mthreads -e 'scalar glob("*"); threads->create(sub { glob("*") })->join();'
0e6766
0e6766
Signed-off-by: Petr Ĺ abata <contyk@redhat.com>
0e6766
---
0e6766
 MANIFEST                  |  1 +
0e6766
 ext/File-Glob/Glob.xs     | 33 ++++++++++++++++++++++
0e6766
 ext/File-Glob/t/threads.t | 71 +++++++++++++++++++++++++++++++++++++++++++++++
0e6766
 3 files changed, 105 insertions(+)
0e6766
 create mode 100644 ext/File-Glob/t/threads.t
0e6766
0e6766
diff --git a/MANIFEST b/MANIFEST
0e6766
index 181bb3f..9771022 100644
0e6766
--- a/MANIFEST
0e6766
+++ b/MANIFEST
0e6766
@@ -3683,6 +3683,7 @@ ext/File-Glob/t/global.t	See if File::Glob works
0e6766
 ext/File-Glob/TODO		File::Glob extension todo list
0e6766
 ext/File-Glob/t/rt114984.t	See if File::Glob works
0e6766
 ext/File-Glob/t/taint.t		See if File::Glob works
0e6766
+ext/File-Glob/t/threads.t	See if File::Glob + threads works
0e6766
 ext/GDBM_File/GDBM_File.pm	GDBM extension Perl module
0e6766
 ext/GDBM_File/GDBM_File.xs	GDBM extension external subroutines
0e6766
 ext/GDBM_File/hints/sco.pl	Hint for GDBM_File for named architecture
0e6766
diff --git a/ext/File-Glob/Glob.xs b/ext/File-Glob/Glob.xs
0e6766
index d74e7a4..6c69aa6 100644
0e6766
--- a/ext/File-Glob/Glob.xs
0e6766
+++ b/ext/File-Glob/Glob.xs
0e6766
@@ -9,6 +9,9 @@
0e6766
 #define MY_CXT_KEY "File::Glob::_guts" XS_VERSION
0e6766
 
0e6766
 typedef struct {
0e6766
+#ifdef USE_ITHREADS
0e6766
+    tTHX interp;
0e6766
+#endif
0e6766
     int		x_GLOB_ERROR;
0e6766
     HV *	x_GLOB_ENTRIES;
0e6766
 } my_cxt_t;
0e6766
@@ -380,6 +383,33 @@ PPCODE:
0e6766
     iterate(aTHX_ doglob_iter_wrapper);
0e6766
     SPAGAIN;
0e6766
 
0e6766
+#ifdef USE_ITHREADS
0e6766
+
0e6766
+void
0e6766
+CLONE(...)
0e6766
+INIT:
0e6766
+    HV *glob_entries_clone = NULL;
0e6766
+CODE:
0e6766
+    PERL_UNUSED_ARG(items);
0e6766
+    {
0e6766
+        dMY_CXT;
0e6766
+        if ( MY_CXT.x_GLOB_ENTRIES ) {
0e6766
+            CLONE_PARAMS param;
0e6766
+            param.stashes    = NULL;
0e6766
+            param.flags      = 0;
0e6766
+            param.proto_perl = MY_CXT.interp;
0e6766
+            
0e6766
+            glob_entries_clone = MUTABLE_HV(sv_dup_inc((SV*)MY_CXT.x_GLOB_ENTRIES, &param));
0e6766
+        }
0e6766
+    }
0e6766
+    {
0e6766
+        MY_CXT_CLONE;
0e6766
+        MY_CXT.x_GLOB_ENTRIES = glob_entries_clone;
0e6766
+        MY_CXT.interp = aTHX;
0e6766
+    }
0e6766
+
0e6766
+#endif
0e6766
+
0e6766
 BOOT:
0e6766
 {
0e6766
 #ifndef PERL_EXTERNAL_GLOB
0e6766
@@ -394,6 +424,9 @@ BOOT:
0e6766
     {
0e6766
 	dMY_CXT;
0e6766
 	MY_CXT.x_GLOB_ENTRIES = NULL;
0e6766
+#ifdef USE_ITHREADS
0e6766
+	MY_CXT.interp = aTHX;
0e6766
+#endif
0e6766
     }  
0e6766
 }
0e6766
 
0e6766
diff --git a/ext/File-Glob/t/threads.t b/ext/File-Glob/t/threads.t
0e6766
new file mode 100644
0e6766
index 0000000..141450a
0e6766
--- /dev/null
0e6766
+++ b/ext/File-Glob/t/threads.t
0e6766
@@ -0,0 +1,71 @@
0e6766
+#!./perl
0e6766
+
0e6766
+BEGIN {
0e6766
+    chdir 't' if -d 't';
0e6766
+    @INC = '../lib';
0e6766
+    require Config; import Config;
0e6766
+    if ($Config{'extensions'} !~ /\bFile\/Glob\b/i) {
0e6766
+        print "1..0\n";
0e6766
+        exit 0;
0e6766
+    }
0e6766
+}
0e6766
+use strict;
0e6766
+use warnings;
0e6766
+# Test::More needs threads pre-loaded
0e6766
+use if $Config{useithreads}, 'threads';
0e6766
+use Test::More;
0e6766
+
0e6766
+BEGIN {
0e6766
+    if (! $Config{'useithreads'}) {
0e6766
+        plan skip_all => "Perl not compiled with 'useithreads'";
0e6766
+    }
0e6766
+}
0e6766
+
0e6766
+use File::Temp qw(tempdir);
0e6766
+use File::Spec qw();
0e6766
+use File::Glob qw(csh_glob);
0e6766
+
0e6766
+my($dir) = tempdir(CLEANUP => 1)
0e6766
+    or die "Could not create temporary directory";
0e6766
+
0e6766
+my @temp_files = qw(1_file 2_file 3_file);
0e6766
+for my $file (@temp_files) {
0e6766
+    open my $fh, ">", File::Spec->catfile($dir, $file)
0e6766
+        or die "Could not create file $dir/$file: $!";
0e6766
+    close $fh;
0e6766
+}
0e6766
+my $cwd = Cwd::cwd();
0e6766
+chdir $dir
0e6766
+    or die "Could not chdir to $dir: $!";
0e6766
+
0e6766
+sub do_glob { scalar csh_glob("*") }
0e6766
+# Stablish some glob state
0e6766
+my $first_file = do_glob();
0e6766
+is($first_file, $temp_files[0]);
0e6766
+
0e6766
+my @files;
0e6766
+push @files, threads->create(\&do_glob)->join() for 1..5;
0e6766
+is_deeply(
0e6766
+    \@files,
0e6766
+    [($temp_files[1]) x 5],
0e6766
+    "glob() state is cloned for new threads"
0e6766
+);
0e6766
+
0e6766
+@files = threads->create({'context' => 'list'},
0e6766
+    sub {
0e6766
+        return do_glob(), threads->create(\&do_glob)->join()
0e6766
+    })->join();
0e6766
+
0e6766
+is_deeply(
0e6766
+    \@files,
0e6766
+    [@temp_files[1,2]],
0e6766
+    "..and for new threads inside threads"
0e6766
+);
0e6766
+
0e6766
+my $second_file = do_glob();
0e6766
+is($second_file, $temp_files[1], "state doesn't leak from threads");
0e6766
+
0e6766
+chdir $cwd
0e6766
+    or die "Could not chdir back to $cwd: $!";
0e6766
+
0e6766
+done_testing;
0e6766
-- 
0e6766
2.4.3
0e6766