Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple example not working #22

Closed
shrimpwagon opened this issue Nov 30, 2018 · 6 comments
Closed

Simple example not working #22

shrimpwagon opened this issue Nov 30, 2018 · 6 comments

Comments

@shrimpwagon
Copy link

[2018-11-30 16:47:58] local.DEBUG: The serialized closure is signed. Make sure you use a security provider for both serialization and unserialization.

#0 [internal function]: Opis\Closure\SerializableClosure->unserialize('@{"closure":"a:...')
#1 /home/vagrant/code/vendor/opis/closure/functions.php(34): unserialize('C:32:"Opis\Clos...')
#2 /home/vagrant/code/vendor/spatie/async/src/Runtime/ParentRuntime.php(92): Opis\Closure\unserialize('C:32:"Opis\Clos...')
#3 /home/vagrant/code/vendor/spatie/async/src/Runtime/ChildRuntime.php(23): Spatie\Async\Runtime\ParentRuntime::decodeTask('QzozMjoiT3Bpc1x...')
#4 {main}

    $pool = \Spatie\Async\Pool::create();

    $json = '';



    $pool->add(function() {

        logger('rates test 1');

        return '{"id":"1","label":"test 1","cost":100}';

    })->then(function($out) use(&$json) {

        logger('rates test 2');

        if( $json ) $json .= ',';

        $json .= $out;

    })->catch(function(Throwable $e) {

        logger($e->getMessage());

    });


    $pool->add(function() {

        return '{"id":"2","label":"test 2","cost":200}';

    })->then(function($out) use(&$json) {

        if( $json ) $json .= ',';

        $json .= $out;

    })->catch(function(Throwable $e) {

        //

    });


    $pool->add(function() {

        return '{"id":"3","label":"test 3","cost":300}';

    })->then(function($out) use(&$json) {

        if( $json ) $json .= ',';

        $json .= $out;

    })->catch(function(Throwable $e) {

        //

    });


    $pool->wait();

    logger('rates test 3');

    echo '{"rates":[' . $json . ']}';

The $json variable is blank

@msarca
Copy link
Member

msarca commented Nov 30, 2018

When you call the add method, the closure is serialized and since you have set a security provider by using the setSecretKey method, the closure ends being cryptographically signed. Then, another process is created where the closure is unserialized and eventually executed. But, since the setSecretKey method was not called in that newly created process, an exception is thrown. These are just assumptions based on the error message that gets displayed. Does your code work if you omit to use the setSecretKey method? If it does, then my assumptions are correct and I will need to open an issue to the spatie/async project.

@shrimpwagon
Copy link
Author

shrimpwagon commented Nov 30, 2018

So I had this working in a non-Laravel environment with the exact same code and libraries. Maybe Laravel is setting some kind of PHP environment or something? Looking at the spatie/async code I do not see the secret key or secret provider being set. Would this get set any other way? Maybe by an environment variable of some kind?

I'm pretty sure it is an opis issue. This is the snippet from spatie:

public static function encodeTask($task): string
  {
      if ($task instanceof Closure) {
          $task = new SerializableClosure($task);
      }

    return base64_encode(serialize($task));
}

It's pretty simple. I don't see anywhere where a secret key or provider is being set ...

The serialize function is: use function Opis\Closure\serialize;

@shrimpwagon
Copy link
Author

shrimpwagon commented Nov 30, 2018

It definitly has something to do with Laravel. Laravel also uses this library and it is setting the secret key. I think things are getting mixed up. I'm thinking the securityProvider should not be static.

@shrimpwagon
Copy link
Author

Yes, it is an issue with spatie project.

In case anyone is curious:

spatie/async#52

@shrimpwagon
Copy link
Author

Technically this could all be fixed if securityProvider property was not static.

Not sure why it needs to be.

The Laravel and Spatie libraries are conflicting in this case.

The child process of Spatie lib doesn't know anything about the Laravel lib when it sets the secret key.

@msarca
Copy link
Member

msarca commented Nov 30, 2018

The solution to this issue was submitted to spatie/async.

@msarca msarca closed this as completed Nov 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants