Giter Club home page Giter Club logo

Comments (4)

calid avatar calid commented on July 22, 2024

Thanks for the report. The original behavior was to support sockets outlasting context scope, but this was changed in 1.09. See 76f2da5 and 0f491b1.

Unfortunately things get hairy during global destruction where destruct order isn't guaranteed. To handle out-of-order object cleanup during GD it was necessary to change the cleanup logic such that sockets outlasting context scope is no longer supported. The thinking is this probably isn't a sane scenario anyways.

That said, obviously segfaulting is bad and this scenario should be detected and handled more cleanly. There's actually a note at the end of 76f2da5 to do this, just never got around to it :)

I think the fix will be pretty straightforward... essentially just need to check if a socket has already been closed before each socket operation, and if so die with an error like Operation on closed socket.

As far as the snippet you provided goes, alternatives that will work are:

sub f {
    my $zmq = ZMQ::FFI->new();
    my $socket = $zmq->socket(ZMQ_PUB);
    $socket->connect('ipc:///tmp/x');
    ($zmq, $socket);
}

my ($zmq, $socket) = f();
$socket->send('foo');

or:

sub f {
    my ($zmq) = @_;
    my $socket = $zmq->socket(ZMQ_PUB);
    $socket->connect('ipc:///tmp/x');
    $socket;
}

my $zmq = ZMQ::FFI->new();
my $socket = f($zmq);
$socket->send('foo');

from perlzmq.

calid avatar calid commented on July 22, 2024

Unfortunately adding a closed guard around the socket methods results in a significant slowdown:

# bench.pl
use strict;
use warnings;

use ZMQ::FFI;
use ZMQ::FFI::Constants qw(ZMQ_PUB);

use Benchmark;

my $c = ZMQ::FFI->new();
my $s = $c->socket(ZMQ_PUB);

$s->connect('ipc://tmp/foo');

timethis 1_000_000 => sub {
    $s->send('ohhai');
}
# with closed guard implementation
$ perl /tmp/bench.pl
timethis 1000000:  1 wallclock secs ( 1.17 usr +  0.00 sys =  1.17 CPU) @ 854700.85/s (n=1000000)

# and without
$ perl /tmp/foo.pl
timethis 1000000:  1 wallclock secs ( 0.55 usr +  0.00 sys =  0.55 CPU) @ 1818181.82/s (n=1000000)

so the vanilla implementation is more than twice as fast.

I tried to be clever and only add the guard after a socket has been closed, but unfortunately Moo(se) method modifiers and the meta api both change the definition for all instances of a class, it doesn't seem possible to dynamically modify the methods for a single instance.

I'm inclined to just document that operations on closed sockets are unsafe, and leave it to the user to avoid these situations. I can add an is_closed method that makes this easy for the user to check.

from perlzmq.

calid avatar calid commented on July 22, 2024

ok, if I inline the check the performance hit isn't nearly as noticeable.

obviously I don't want to manually add and maintain so much boiler, but as the socket classes are already being generated I can look into generating these checks as well.

from perlzmq.

calid avatar calid commented on July 22, 2024

Operations on closed sockets are ignored now and will trigger warnings:

use strict;
use warnings;

use ZMQ::FFI;
use ZMQ::FFI::Constants qw(ZMQ_PUB);

sub f {
    my $zmq = ZMQ::FFI->new();
    my $socket = $zmq->socket(ZMQ_PUB);
    $socket->connect('ipc:///tmp/x');
    $socket;
}

my $socket = f();
$socket->send('foo');
$ perl /tmp/closed_socket.pl
Operation on closed socket at /tmp/closed_socket.pl line 15.

from perlzmq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.