Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

RE - How to fix error handling? #78

Closed
domgraziano opened this issue Nov 9, 2019 · 6 comments · Fixed by #79
Closed

RE - How to fix error handling? #78

domgraziano opened this issue Nov 9, 2019 · 6 comments · Fixed by #79

Comments

@domgraziano
Copy link

Describe the bug
Linked to #20
From any symfony controller if an error or exception is thrown, the symfony request response cycle does not terminate and the browser (or any client eg postman) is hanging waiting for the response which never comes back.

Steps To Reproduce
I'm adding a repository with a symfony installation wrapped in a docker container to reproduce you would have 3 endpoint

  • localhost:9501/success ok
  • localhost:9501/error throwing error
  • localhost:9501/exception throwing exception

here the repo:
trySwoole.zip

Expected behavior
Response finishes with 500 code / server error

Environment (please complete the following information):
Dockerised varsion started from https://www.swoole.co.uk/article/docker.html but substituted with php php:7.3-cli as the bundle requires 7.3 and swoole-4.4.7

Logs


Fatal error: Uncaught Swoole\ExitException: swoole exit in /app/project/vendor/symfony/debug/ErrorHandler.php:668
Stack trace:
#0 [internal function]: Symfony\Component\Debug\ErrorHandler::Symfony\Component\Debug\{closure}()
#1 {main}
  thrown in /app/project/vendor/symfony/debug/ErrorHandler.php on line 668

@Rastusik
Copy link
Contributor

Rastusik commented Nov 9, 2019

@k911 I'm experiencing this too btw

@k911
Copy link
Owner

k911 commented Nov 10, 2019

@domgraziano @Rastusik To confirm, you didn't install twig and configure symfony's exception handler (as described here https://symfony.com/doc/current/controller/error_pages.html) or don't want to, correct?

@Rastusik
Copy link
Contributor

@k911 I have twig bundle installed. Exceptions are handled fine, if they are caught by Symfony, but the described problem occurs to me on php errors

@k911
Copy link
Owner

k911 commented Nov 10, 2019

@Rastusik

if they are caught by Symfony

So your case is probably different than @domgraziano , because I see that @domgraziano don't have twig pack installed. If you could provide reproducer repository I would be grateful.

@domgraziano

Installing twig should fix your problems, just run:

composer require twig

Anyway, I think this issue could stay open because server could at least terminate HTTP connection with 500 error code in such situations.

@domgraziano
Copy link
Author

domgraziano commented Nov 10, 2019

@k911 thanks for looking into it.

I can confirm that installing twig would terminate the request but only for thrown Exception, not for error (in my example, with twig, running in prod mode, the localhost:9501/error would still hang and response never come back, confirming what @Rastusik said).

My view is that I shouldn't be forced to install twig though. As per after symfony 4 I could have only a few components to being able to speed up a minimal API for which I might or might not need twig at all, which was my original case.

I do agree that the server itself should be able to terminate the request, and the bundle should probably provide such a mechanism. If you have any idea on where to look, I can have a look myself. I tried to catch a \Swoole\ExitException in the K911\Swoole\Bridge\Symfony\HttpKernel\HttpKernelRequestHandler without success

@domgraziano
Copy link
Author

@k911 actually I've just tried:

    public function handle(SwooleRequest $request, SwooleResponse $response): void
    {
	try {
	     $httpFoundationRequest = $this->requestFactory->make($request);
	     $httpFoundationResponse = $this->kernel->handle($httpFoundationRequest);	
	     $this->responseProcessor->process($httpFoundationResponse, $response);		
	} catch(\Error $e) {
	        return;
	} catch(\Exception $e) {
	        return;
	}
		
        if ($this->kernel instanceof TerminableInterface) {
            $this->kernel->terminate($httpFoundationRequest, $httpFoundationResponse);
        }
    }

in K911\Swoole\Bridge\Symfony\HttpKernel\HttpKernelRequestHandler

and this would terminate with a 500, without Twig, downside is that nothing will be logged as we are returning, so it might not be that immediate as a fix

@k911 k911 added kind/bug/confirmed Something isn't working and removed kind/bug/possible Something probably isn't working labels Nov 12, 2019
@k911 k911 closed this as completed in #79 Nov 13, 2019
k911 added a commit that referenced this issue Nov 13, 2019
…imeouts (#79)

When any exception/error is not caught by Symfony's exception handler Swoole's worker will exit and response will not terminate properly.

The purpose of this pull request is to prevent the situation when the request will timeout due to lack of response because Swoole's worker has exited due to an uncaught exception. If you're looking for a custom exception handing in Symfony, please use this guide: https://symfony.com/doc/current/event_dispatcher.html

Fixes #78
k911 added a commit that referenced this issue Nov 13, 2019
* fix(http-client): Make HttpClient serializable ([0ee8918](0ee8918))
* fix(http-server): Add top-level exception handler to prevent server timeouts (#79) ([08c76c4](08c76c4)), closes [#79](#79) [#78](#78)
* chore(commitlint): Update configuration options to less restrictive ([bfb6d4c](bfb6d4c))
* chore(composer): Update dependencies ([9c3d7b2](9c3d7b2))
* chore(swoole): Update to v4.4.10 ([089e91d](089e91d))
* chore(swoole): Update to v4.4.8 ([a22e9ff](a22e9ff))
* chore(swoole): Upgrade to v4.4.12 ([30567b4](30567b4))
* ci(circle): Add default release notes when empty (#72) ([57e3c4e](57e3c4e)), closes [#72](#72)
* ci(circle): Add shellcheck job (#77) ([97fd9ef](97fd9ef)), closes [#77](#77)
* ci(circle): Fix release notes script generator ([490a8bc](490a8bc))
* ci(circle): Fix release version script ([68fd0e9](68fd0e9))
* feat(session): Add in-memory syfmony session storage (#73) ([4ccdca0](4ccdca0)), closes [#73](#73)
* refactor(phpstan): Enable "inferPrivatePropertyTypeFromConstructor" parameter ([3436fd9](3436fd9))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants