| 1 | use strictscip-perl cpan ZMQ-FFI 1.19 strict/; |
| 2 | use warningsscip-perl cpan ZMQ-FFI 1.19 warnings/; |
| 3 | |
| 4 | use ZMQ::FFIscip-perl cpan ZMQ-FFI 1.19 ZMQ/FFI/ qw(ZMQ_REQ); |
| 5 | |
| 6 | my $context = ZMQ::FFI->newscip-perl cpan ZMQ-FFI 1.19 ZMQ/FFI/new().(); |
| 7 | my $socket = $context->socketscip-perl cpan ZMQ-FFI 1.19 ZMQ/FFI/socket().(ZMQ_REQ); |
| 8 | |
| 9 | sub closurescip-perl cpan ZMQ-FFI 1.19 main/closure(). { $socket } |
| 10 | |
| 11 | # Suprisingly, the above can cause this script to hang. Closing over $socket |
| 12 | # may result in $context getting cleaned up before $socket during global |
| 13 | # destruction. This is despite the fact that $socket has a reference to |
| 14 | # $context, and therefore would be expected to get cleaned up first (and |
| 15 | # always does during normal destruction). |
| 16 | # |
| 17 | # This triggers a hang as zmq contexts block during cleanup until close has |
| 18 | # been called on all sockets. So for single threaded applications you _must_ |
| 19 | # close all sockets before attempting to destroy the context. |
| 20 | # |
| 21 | # Remove the closure and global destruction cleanup happens in the expected |
| 22 | # order. However the lesson of course is to not assume _any_ particular |
| 23 | # cleanup order during GD. The ordering may change with different perl |
| 24 | # versions, different arrangements of the code, different directions of the |
| 25 | # wind, etc. |
| 26 | # |
| 27 | # The old adage "all bets are off during global destruction" is still true |
| 28 | # and code that assumes a particular cleanup order during GD will fail |
| 29 | # eventually. |