garu / Clone

recursively copy Perl datatypes
7 stars 10 forks source link

Clone::clone on shared data causes crash [rt.cpan.org #93821] #18

Open atoomic opened 5 years ago

atoomic commented 5 years ago

Migrated from rt.cpan.org#93821 (status was 'open')

Requestors:

From james_avera@yahoo.com on 2014-03-13 05:13:10:

Hi,

I'm trying to use Clone::clone to copy a hash which is shared among threads,
but when the result is accessed, Perl dies with
"Can't locate object method "FETCH" via package "threads::shared::tie" 
at ..."

Here's an example:
#!/usr/bin/perl
use strict; use warnings;
use threads;
use threads::shared;
use Clone;

my $a = shared_clone { foo => 100, bar => 200 };
my $b = Clone::clone($a);
print $b->{foo}, "\n"; #crashes here

-Jim

From garu@cpan.org on 2014-05-15 22:16:59:

Hi Jim,

I don't think Clone is thread-safe. Are you able to reproduce the issue if you use Storable's dclone() instead of Clone's clone()?

Thanks!

From jim.avera@gmail.com on 2014-05-15 23:31:38:

On 05/15/2014 03:16 PM, Breno G. de Oliveira via RT wrote:
| I don't think Clone is thread-safe.
| Are you able to reproduce the issue if you use Storable's dclone() 
instead of Clone's clone()?

I'm not familiar with Storable, but gave it a try.   The result of 
dclone() can not be
de-referenced either, but the error is different:
#!/usr/bin/perl
use strict; use warnings; use threads; use threads::shared;
use Storable qw(dclone);

my $a = shared_clone { foo => 100, bar => 200 };
my $b = dclone($a);
# The next statement crashes with:
# Can't locate object method "FETCH" via package "threads::shared::tie"
print $b->{foo}, "\n";

From ralf.neubauer@wido.bv.aok.de on 2015-11-06 16:20:09:

Unfortunately threads::shared implements the sharing with tie-s to threads::shared::tie, but it makes zero sense to have a clone take part in the sharing -- if you wanted that, you would use the original variable, not the clone... Clone and Storable will (dirtily) have to special case that an remove all tie-s to threads::shared::tie, to be able to cooperate with threads::shared. Note that threads::shared itself has the same problem, you can't send a structure over a Thread::Queue and return the sent object over this or another Threads::Queue, this will crash after one or more iterations (I don't remember).

Ralf

From ralf.neubauer@wido.bv.aok.de on 2015-11-06 16:32:17:

https://rt.perl.org/Public/Bug/Display.html?id=38443

Date:   Mon, 06 Feb 2006 16:43:36 +0100

:)
atoomic commented 5 years ago

I'm not familiar with threads would appreciate some external feedback on this one