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

Output after the loop is closed #32

Closed
jvdlaar opened this issue Nov 15, 2018 · 5 comments
Closed

Output after the loop is closed #32

jvdlaar opened this issue Nov 15, 2018 · 5 comments

Comments

@jvdlaar
Copy link

jvdlaar commented Nov 15, 2018

Below is my code that creates several persistent subscriptions. When I have verbose logging enabled and use the console logger on my event store client connection then this gives me this error:

Fatal error: Uncaught Amp\ByteStream\ClosedException: The stream is not writable in /app/vendor/amphp/byte-stream/lib/ResourceOutputStream.php:164
Stack trace:
#0 /app/vendor/amphp/byte-stream/lib/ResourceOutputStream.php(144): Amp\ByteStream\ResourceOutputStream->send('[2018-11-15 13:...', false)
#1 /app/vendor/amphp/log/src/StreamHandler.php(56): Amp\ByteStream\ResourceOutputStream->write('[2018-11-15 13:...')
#2 /app/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(39): Amp\Log\StreamHandler->write(Array)
#3 /app/vendor/monolog/monolog/src/Monolog/Logger.php(344): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#4 /app/vendor/monolog/monolog/src/Monolog/Logger.php(623): Monolog\Logger->addRecord(100, 'EventStoreNodeC...', Array)
#5 /app/vendor/prooph/event-store-client/src/Internal/EventStoreConnectionLogicHandler.php(926): Monolog\Logger->debug('EventStoreNodeC...')
#6 /app/vendor/prooph/event-store-client/src/Internal/EventStoreConnectionLogicHandler.php(184): Prooph\EventStoreClient\ in /app/vendor/amphp/byte-stream/lib/ResourceOutputStream.php on line 164

Which is caused by the __destruct function on the EventStoreAsyncNodeConnection class which closes the connection and then wants to output some debug output. Manually closing the connection inside the loop doesn't help because the __destruct doesn't check if it's already closed.

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('Create subscriptions');

        Loop::run(function () use ($output): Generator {
            yield $this->connection->connectAsync();

            foreach ($this->subscriptions as $subscription) {
                $name = (string) $subscription;

                try {
                    $result = yield $this->connection->createPersistentSubscriptionAsync(
                        $subscription->getStream(),
                        $subscription->getGroupName(),
                        $subscription->getSettings()
                    );

                    $output->writeln(
                        sprintf('=> <comment>%s</comment>: <comment>%s</comment>', $name, $result->status()->name())
                    );
                } catch (Throwable $error) {
                    $output->writeln(
                        sprintf('=> <comment>%s</comment>: <error>%s</error>', $name, $error->getMessage())
                    );
                }
            }

            Loop::stop();
        });

        $output->writeln('Subscriptions created');

        return 0;
    }
@prolic
Copy link
Member

prolic commented Nov 15, 2018 via email

@jvdlaar
Copy link
Author

jvdlaar commented Nov 15, 2018

Hmm building a small reproducable script proves to be quite hard because everything is caused by the order in which objects are destructed. So if the ResourceOutputStream part of the console logger is destructed before the connection then all bad things happen.

I solved it for myself by not using the useConsoleLogger function but by using the useCustomLogger which changed the desctruction process in a way that works.

Best solution would be that the __destruct function in EventStoreAsyncNodeConnection would check if the connection is still alive and then you can close the connection manually.

@prolic
Copy link
Member

prolic commented Nov 17, 2018

The closing of the event-store-connection will not close the output (which is \STDOUT). The exception says that \STDOUT is not writeable. I'm not sure yet under which conditions \STDOUT can be not writeable.

By the way:
You can call close() as often as you want on the connection. See also this test-case: https://github.com/prooph/event-store-client/blob/master/tests/event_store_connection_should.php#L46-L51

I created a ticket here for now: amphp/log#1

Let's see what the amp-guys can say.

@jvdlaar
Copy link
Author

jvdlaar commented Nov 19, 2018

Thank you very much!

@prolic
Copy link
Member

prolic commented Nov 24, 2018

@jvdlaar Amp team confirms this is a problem on their side, so I'm closing this here. You can track the related issue and check once it is resolved or further discuss there. I cannot do something about it at this point.

@prolic prolic closed this as completed Nov 24, 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