Puzzled by error message in Mojo::IOLoop::Subprocess::run

Skip to first unread message

Edward Baudrez

Aug 11, 2020, 10:14:02 AM8/11/20
to Mojolicious

I'm a bit puzzled by the result I get from this test case:

use Mojo::Base -strict;
use Test::More;        
use Mojo::IOLoop;
my $err;
my $sub = Mojo::IOLoop->subprocess(
    sub { exit 55 },
    sub { my $self = shift; $err = shift; Mojo::IOLoop->stop }
cmp_ok $sub->exit_code, '==', 55, 'exit code';
is $err, '', 'no exception';

The second test (testing the contents of $err) fails with the error message "malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)")". It gives me a line number as follows: "~/opt/perl-5.30.2/lib/site_perl/5.30.2/Mojo/JSON.pm line 31". The context in that file around line 31 is:

# Replace pure-Perl fallbacks if Cpanel::JSON::XS is available
if (JSON_XS) {
  my $BINARY = Cpanel::JSON::XS->new->utf8;
  my $TEXT   = Cpanel::JSON::XS->new;
    for $BINARY, $TEXT;
  monkey_patch __PACKAGE__, 'encode_json', sub { $BINARY->encode($_[0]) };
  monkey_patch __PACKAGE__, 'decode_json', sub { $BINARY->decode($_[0]) };
  monkey_patch __PACKAGE__, 'to_json',     sub { $TEXT->encode($_[0]) };
  monkey_patch __PACKAGE__, 'from_json',   sub { $TEXT->decode($_[0]) };

Now I fail to understand why I'm getting this particular error, considering I'm not using any JSON functionality in this script. Looking at the file where the error originates doesn't give me any clues. I'm a bit lost here. Could anyone shed some light on this?

I'm using the latest Mojolicious:
  Perl        (v5.30.2, linux)
  Mojolicious (8.57, Supervillain)

  Cpanel::JSON::XS 4.09+   (4.19)
  EV 4.32+                 (4.33)
  IO::Socket::Socks 0.64+  (0.74)
  IO::Socket::SSL 2.009+   (2.068)
  Net::DNS::Native 0.15+   (0.22)
  Role::Tiny 2.000001+     (2.001004)
  Future::AsyncAwait 0.36+ (0.40)

This version is up to date, have fun!

Kind regards

Sebastian Riedel

Aug 11, 2020, 11:08:30 AM8/11/20
to Mojolicious
Please open an issue on GitHub, that looks like a bug in Mojo::IOLoop::Subprocess.


Edward Baudrez

Aug 31, 2020, 4:05:17 PM8/31/20
to Mojolicious
In case anyone is curious: this was discussed on GitHub (https://github.com/mojolicious/mojo/issues/1555) and is not a bug. It turns out it's not trivial to trap calls to exit in Perl. For my particular problem, I was able to find a workaround by overriding exit (i.e., setting CORE::GLOBAL::exit to a subroutine reference) and using the module Scope::Upper.

Kind regards
Reply all
Reply to author
0 new messages