Comments (4)
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.
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.
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.
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)
- Segfault when using EV version 3 HOT 1
- user linger value is clobbered HOT 1
- segfault during global destruction HOT 1
- test hangs with some versions of Socket HOT 2
- zmq_poll function missing ? HOT 11
- support exporting zmq constants directly from ZMQ::FFI
- Add missing socket api methods to SocketRole requires HOT 1
- DEALER-REP hangs forever HOT 9
- Faster get/set on sockets HOT 1
- using proxy results in high cpu usage HOT 4
- Context object still keeps around socket objects after they go out of scope HOT 3
- Unable to build a release with SELinux enabled on system HOT 1
- Unable to build a release with a perl compiled with clang HOT 1
- zmq-ffi: t/proxy.t occasionally hangs HOT 26
- zmq_bind: Protocol not supported HOT 2
- Fails to install on CentOS HOT 4
- FFI::Platypus should be specified as configure_requires HOT 1
- Tests will fail in perl-5.38.0
- dealer with AE::io don't get any event when watch_write is 0 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from perlzmq.