develooper Front page | perl.perl5.porters | Postings from September 2013

Re: [perl #119897] glob in threads is broken in perl5.18

Thread Previous | Thread Next
From:
Brian Fraser
Date:
September 20, 2013 23:30
Subject:
Re: [perl #119897] glob in threads is broken in perl5.18
Message ID:
CA+nL+nbUh_Qipj3TCmRSfs6XNX9ikTLhyaoH37Lh1A_+3L6doQ@mail.gmail.com
On Fri, Sep 20, 2013 at 12:39 PM, Father Chrysostomos via RT <
perlbug-followup@perl.org> wrote:

> On Fri Sep 20 04:09:25 2013, niels@thykier.net wrote:
> >
> > This is a bug report for perl from niels@thykier.net,
> > generated with the help of perlbug 1.39 running under perl 5.18.1.
> >
> >
> > -----------------------------------------------------------------
> > [Please describe your issue here]
> >
> > Dear perl porters,
> >
> > When Debian migrated to Perl5.18.1, I started to experience random
> > seg. faults in one of my perl scripts.  hugmeir from #p5p devised
> > the following mimimal test case:
> >
> > """
> > use threads;
> > scalar glob("*");
> > threads->create(sub { glob("*") })->join();
> > """
> >
> > The test case itself does not crash, but it does spew out warnings
> > like:
> >
> > """
> > Unbalanced string table refcount: (1) for "I" during global
> >    destruction.
> > Attempt to free nonexistent shared string 'I', [...]
> > Attempt to free unreferenced scalar: SV 0x8c6a1fc, [...]
> > """
> >
> > Based on that, I replaced my calls to "glob" in the threads and my
> >    script
> > stopped crashing.
> >
> >
> > hugmeir also suggested that he believed this bug to also affect blead
> > (I have not tested that assertion).  TonyC from #p5p suggested that it
> > might be because the "glob state isn't being cloned".
>
> Interesting.  Could this be the same as #117823?
>

Yes. The problem for both is that x_GLOB_ENTRIES should be thread-local,
but is unintentionally being shared between threads.

This patch solves the issue:

diff --git a/ext/File-Glob/Glob.xs b/ext/File-Glob/Glob.xs
index b3705b3..181dbbc 100644
--- a/ext/File-Glob/Glob.xs
+++ b/ext/File-Glob/Glob.xs
@@ -396,6 +396,13 @@ PPCODE:
     iterate(aTHX_ doglob_iter_wrapper);
     SPAGAIN;

+void
+CLONE(...)
+CODE:
+    PERL_UNUSED_ARG(items);
+    MY_CXT_CLONE;
+    MY_CXT.x_GLOB_ENTRIES = NULL;
+
 BOOT:
 {
 #ifndef PERL_EXTERNAL_GLOB


But it means that this:

./perl -Ilib -Mthreads -E 'sub foo {scalar glob("*")} foo(); say
threads->create(\&foo)->join() for 1..3'

Will return "Artistic" four times, rather than "Artristic" followed by the
second file thrice. If the latter is the desired behavior, then we need to
hv_dup(x_GLOB_ENTRIES) in CLONE() as well.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About