Sending a signal to a perl script while it is closing a filehandle

This question already has an answer here:

  • What happens to a SIGINT (^C) when sent to a perl script containing children? 3 answers

  • I cannot reproduce the behavior that you are observing. When I press CTRL-C in the terminal, both the child and the parent immediately receives SIGINT :

    use diagnostics;
    use feature qw(say);
    use strict;
    use warnings;
    
    $SIG{INT} = sub {  say "This is expected to print"; die };
    my $pid = open ( my $pipe, "|-", "script.pl" );
    say "PID = $pid";
    eval {
        say "Closing..";
        my $close_ok = close $pipe; # Note "close" here waits for child to exit
        if ( ! $close_ok ) {
            say "Error closing: $!";
        }
        else {
            say "Close done.";
        }
    };
    if ( $@ ) {
        say "Parent caught SIGINT.";
    }
    

    where script.pl is:

    #! /usr/bin/env perl
    
    use feature qw(say);
    use strict;
    use warnings;
    
    $SIG{INT} = sub { die };
    eval {
        say "Sleeping..";
        for (1..5) {
            sleep 1;
            say $_;
        }
    };
    if ( $@ ) {
        say "Child caught SIGINT.";
        exit;
    }
    

    the output of running the first program in the terminal ( gnome-terminal on Ubuntu 16.04) is:

    PID = 1746
    Closing..
    Sleeping..
    1
    2
    ^CThis is expected to print
    Child caught SIGINT.
    Parent caught SIGINT.
    Uncaught exception from user code:
        refcnt: fd -1 < 0
    

    Note that there is an uncaught exception refcnt: fd -1 < 0 . I have no idea what that is. Maybe because close did not succeed?

    链接地址: http://www.djcxy.com/p/33706.html

    上一篇: 什么是不调用libusb的后果

    下一篇: 在关闭文件句柄时向perl脚本发送信号