zend-mail icon indicating copy to clipboard operation
zend-mail copied to clipboard

Uncaught exception of Protocol\Smtp if use it from console with crashed server

Open GeeH opened this issue 9 years ago • 1 comments

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html


Original Issue: https://api.github.com/repos/zendframework/zendframework/issues/6488 User: @filipp-k Created On: 2014-07-28T09:18:00Z Updated At: 2016-03-10T18:05:14Z Body Sorry my bad English =)

After fixing #3645 bug another one rising.

Now Protocol\Smtp has this _disconnect() method:

protected function _disconnect()
{
    // Make sure the session gets closed
    $this->quit();
    parent::_disconnect();
}

which calls in __desctuct() method of AbstractProtocol, which causes uncaught exception on exit($warningLevel) in ConsoleResponseSender:

public function __invoke(SendResponseEvent $event)
{
    $response = $event->getResponse();
    if (!$response instanceof Response) {
        return;
    }

    $this->sendContent($event);
    $errorLevel = (int) $response->getMetadata('errorLevel',0);
    $event->stopPropagation(true);
    exit($errorLevel); // GC calls __destruct(), then _disconnect(), then quit() which rises RuntimeException if server crashes
}

I have fixed this problem this way:

class Smtp extends \Zend\Mail\Protocol\Smtp
{
    ...
    public function __destruct()
    {
        try {
            parent::__destruct();
        } catch (\Exception $e) {
            // ignore
        }
    }    
}

I don't know how correctly it is, so correct my version and I will create pull request.


Comment

User: @coolmic Created On: 2014-11-28T12:14:39Z Updated At: 2014-11-28T12:14:39Z Body Not exactly the same issue, but very likely.

I am using https://github.com/juriansluiman/SlmQueue to send email. It open a never-ending php session. So the smtp connection is supposed to be persistent. But sometimes, the connection is closed (by postfix maybe?).

The SmtpTransport doesn't reconnect (and there is no way to reconnect). So I tried to create another Smtp transport, on Zend\Mail\Protocol\Exception\RuntimeException.

But I get random Zend\Mail\Protocol\Exception\RuntimeException thrown because of garbage collector. sometime when I do

$this->smtp = null;

or when gc_collect_cycles() is called from SlmQueue\Strategy\MaxMemoryStrategy . Because the __destruct method is called, but the connection is invalid.

I fixed it the same way as @filipp-qoma .


Comment

User: @coolmic Created On: 2016-03-10T15:32:42Z Updated At: 2016-03-10T15:32:42Z Body Why close? It's still not fixed.

We should add a try catch in __destruct , or add a public method stopSession to set $this->sess = false

Should I do a pull request?


Comment

User: @Ocramius Created On: 2016-03-10T15:36:39Z Updated At: 2016-03-10T15:36:39Z Body @coolmic the proposed solution doesn't seem to be the correct one, as it fixes the symptom, not the cause...


Comment

User: @filipp-k Created On: 2016-03-10T17:46:31Z Updated At: 2016-03-10T17:46:31Z Body So propose correct one but don't close issue. Code sends BYE twice, and second time happens when connection is already closed.


Comment

User: @filipp-k Created On: 2016-03-10T17:47:20Z Updated At: 2016-03-10T17:47:20Z Body I'm so sorry about misstap...


Comment

User: @adamlundrigan Created On: 2016-03-10T18:05:14Z Updated At: 2016-03-10T18:05:14Z Body @filipp-k @coolmic it was marked "To Be Closed" has been inactive for a very long time and this repository no longer contains the code being referenced. If you wish to provide a fix for this please open a new issue and/or PR over on the Zend\Mail repository (zendframework/zend-mail) and reference this existing issue so we know this one can be closed out. Thanks!


GeeH avatar Jun 28 '16 14:06 GeeH

This repository has been closed and moved to laminas/laminas-mail; a new issue has been opened at https://github.com/laminas/laminas-mail/issues/52.

michalbundyra avatar Jan 15 '20 19:01 michalbundyra