Skip to content

exception in child Perl sub causes execution to escape run (and isn't returned to parent) #122

Open
@djerius

Description

@djerius

When a Perl subroutine is run as the child, I expect that an exception thrown in the sub will get propagated up to the parent,

From the fine manual

start() pauses the parent until the child executes the command or CODE reference and propagates any exceptions thrown (including exec() failure) back to the parent. This has several pleasant effects: any exceptions thrown in the child, including exec() failure, come flying out of start() or run() as though they had occurred in the parent.

However, unless I'm totally misunderstanding things, this doesn't happen. Here's some sample code:

use IPC::Run 'run';

print STDERR "Parent $$\n";

sub child {
    print STDERR "Child $$\n";
    die("Child dies");
}

sub parent {
    run \&child;
}
eval { parent } ;

if ( $@ ) {
    print "$$ error: $@\n";
}
else {
    print "$$ no error\n";
}

Here's what I expect to happen.

  1. Parent runs child
  2. Child dies, returns immediately from die() to parent
  3. Exception is magically massaged so that run() in parent throws child's exception
  4. eval in parent catches child's exception.

Here's some output:

Parent 26043
Child 26086
26086 error: Child dies at ../../IPC-Run/child_dies2.pl line 9.

26043 no error

Notice that it is the child that catches the error and prints it, actually continuing (because run is not trapping the error) to execute after the end of the child subroutine when it should have returned to the parent. The parent does not see the exception at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions