zendframework / zend-expressive-session-ext

PHP ext-session persistence adapter for zend-expressive-session.
BSD 3-Clause "New" or "Revised" License
12 stars 9 forks source link

failed to start session while using swoole #27

Closed azjezz closed 6 years ago

azjezz commented 6 years ago

the library works as it supposed to using Apache, nginx or PHP built-in web-server, but that's not the case with swoole ( zendframework/zend-expressive-swoole ), i don't know how am i supposed to start the session or if this library is not compatible with swoole maybe?

composer requirements :

        "php": "^7.2",
        "ext-fileinfo": "*",
        "ext-hash": "*",
        "ext-json": "*",
        "ext-mbstring": "*",
        "ext-pdo": "*",
        "ext-swoole": "*",
        "dasprid/container-interop-doctrine": "dev-master",
        "league/commonmark": "^0.17.5",
        "league/commonmark-extras": "^0.1.4",
        "monolog/monolog": "^1.23",
        "paragonie/csp-builder": "^2.3",
        "psr/cache": "^1.0",
        "psr/container": "^1.0",
        "psr/http-factory": "^1.0",
        "psr/http-message": "^1.0",
        "psr/http-server-handler": "^1.0",
        "psr/http-server-middleware": "^1.0",
        "psr/log": "^1.0",
        "psr/simple-cache": "^1.0",
        "symfony/cache": "^4.1",
        "symfony/dotenv": "^4.1",
        "symfony/polyfill": "^1.9",
        "symfony/validator": "^4.1",
        "validus/translation": "^1.0.1",
        "zendframework/zend-component-installer": "^2.1.1",
        "zendframework/zend-config-aggregator": "^1.0",
        "zendframework/zend-diactoros": "^1.7.1",
        "zendframework/zend-expressive": "^3.0.1",
        "zendframework/zend-expressive-csrf": "^1.0",
        "zendframework/zend-expressive-flash": "^1.0",
        "zendframework/zend-expressive-helpers": "^5.0",
        "zendframework/zend-expressive-session": "^1.1",
        "zendframework/zend-expressive-session-ext": "^1.2",
        "zendframework/zend-expressive-swoole": "^1.0",
        "zendframework/zend-expressive-tooling": "^1.0",
        "zendframework/zend-expressive-twigrenderer": "^2.0",
        "zendframework/zend-expressive-zendrouter": "^3.0",
        "zendframework/zend-mail": "^2.10",
        "zendframework/zend-servicemanager": "^3.3",
        "zendframework/zend-stdlib": "^3.1"

This is the logged exception :

[2018-10-05 00:28:48] Polar.CRITICAL: Unexpected Exception : session_start(): Headers already sent. You cannot change the session module's ini settings at this time {"message":"session_start(): Headers already sent. You cannot change the session module's ini settings at this time","code":0,"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session-ext/src/PhpSessionPersistence.php","line":154,"trace":[{"function":"Zend\\Stratigility\\Middleware\\{closure}","class":"Zend\\Stratigility\\Middleware\\ErrorHandler","type":"->","args":[2,"session_start(): Headers already sent. You cannot change the session module's ini settings at this time","/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session-ext/src/PhpSessionPersistence.php",154,{"id":"24c029f3fa5fed0a53582aaaf1eb6535","options":[]}]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session-ext/src/PhpSessionPersistence.php","line":154,"function":"session_start","args":[{"use_cookies":false,"use_only_cookies":true,"cache_limiter":""}]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session-ext/src/PhpSessionPersistence.php","line":93,"function":"startSession","class":"Zend\\Expressive\\Session\\Ext\\PhpSessionPersistence","type":"->","args":["24c029f3fa5fed0a53582aaaf1eb6535"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session/src/LazySession.php","line":122,"function":"initializeSessionFromRequest","class":"Zend\\Expressive\\Session\\Ext\\PhpSessionPersistence","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session/src/LazySession.php","line":70,"function":"getProxiedSession","class":"Zend\\Expressive\\Session\\LazySession","type":"->","args":[]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-flash/src/FlashMessages.php","line":166,"function":"has","class":"Zend\\Expressive\\Session\\LazySession","type":"->","args":["Zend\\Expressive\\Flash\\FlashMessagesInterface::FLASH_NEXT"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-flash/src/FlashMessages.php","line":56,"function":"prepareMessages","class":"Zend\\Expressive\\Flash\\FlashMessages","type":"->","args":["[object] (Zend\\Expressive\\Session\\LazySession: {})","Zend\\Expressive\\Flash\\FlashMessagesInterface::FLASH_NEXT"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-flash/src/FlashMessages.php","line":66,"function":"__construct","class":"Zend\\Expressive\\Flash\\FlashMessages","type":"->","args":["[object] (Zend\\Expressive\\Session\\LazySession: {})","Zend\\Expressive\\Flash\\FlashMessagesInterface::FLASH_NEXT"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-flash/src/FlashMessageMiddleware.php","line":65,"function":"createFromSession","class":"Zend\\Expressive\\Flash\\FlashMessages","type":"::","args":["[object] (Zend\\Expressive\\Session\\LazySession: {})","Zend\\Expressive\\Flash\\FlashMessagesInterface::FLASH_NEXT"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php","line":46,"function":"process","class":"Zend\\Expressive\\Flash\\FlashMessageMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-stratigility/src/MiddlewarePipe.php","line":78,"function":"process","class":"Zend\\Expressive\\Middleware\\LazyLoadingMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-session/src/SessionMiddleware.php","line":34,"function":"handle","class":"Zend\\Stratigility\\MiddlewarePipe","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php","line":46,"function":"process","class":"Zend\\Expressive\\Session\\SessionMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-stratigility/src/MiddlewarePipe.php","line":78,"function":"process","class":"Zend\\Expressive\\Middleware\\LazyLoadingMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-helpers/src/ServerUrlMiddleware.php","line":37,"function":"handle","class":"Zend\\Stratigility\\MiddlewarePipe","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php","line":46,"function":"process","class":"Zend\\Expressive\\Helper\\ServerUrlMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-stratigility/src/MiddlewarePipe.php","line":78,"function":"process","class":"Zend\\Expressive\\Middleware\\LazyLoadingMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/src/Polar/Middleware/ErrorLoggingMiddleware.php","line":40,"function":"handle","class":"Zend\\Stratigility\\MiddlewarePipe","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php","line":46,"function":"process","class":"Polar\\Middleware\\ErrorLoggingMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-stratigility/src/MiddlewarePipe.php","line":78,"function":"process","class":"Zend\\Expressive\\Middleware\\LazyLoadingMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-stratigility/src/Middleware/ErrorHandler.php","line":143,"function":"handle","class":"Zend\\Stratigility\\MiddlewarePipe","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php","line":46,"function":"process","class":"Zend\\Stratigility\\Middleware\\ErrorHandler","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-stratigility/src/MiddlewarePipe.php","line":78,"function":"process","class":"Zend\\Expressive\\Middleware\\LazyLoadingMiddleware","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})","[object] (Zend\\Stratigility\\MiddlewarePipe: {})"]},{"file":"/home/azjezz/Documents/projects/polar/vendor/zendframework/zend-expressive-swoole/src/SwooleRequestHandlerRunner.php","line":318,"function":"handle","class":"Zend\\Stratigility\\MiddlewarePipe","type":"->","args":["[object] (Zend\\Diactoros\\ServerRequest: {})"]}]} []

Expected results

Actual results

azjezz commented 6 years ago

ping @weierophinney

weierophinney commented 6 years ago

Plain and simple, the ext-session adapter does not work with Swoole. I have suspicions why, but cannot point to a specific reason at this time. Regardless, it doesn't work, and likely any solution using ext-session never will.

I've created a zend-expressive-session adapter that uses a psr-6 cache pool, and have had success using it under swoole; I'll try to publish it in the coming week.

On Thu, Oct 4, 2018, 5:54 PM azjezz notifications@github.com wrote:

ping @weierophinney https://github.com/weierophinney

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-expressive-session-ext/issues/27#issuecomment-427195116, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlVxO5O8iiwulCeUw4JKbJuQvWTNgTks5uhpGygaJpZM4XJLbd .

On Oct 4, 2018 5:54 PM, "azjezz" notifications@github.com wrote:

ping @weierophinney https://github.com/weierophinney

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-expressive-session-ext/issues/27#issuecomment-427195116, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlVxO5O8iiwulCeUw4JKbJuQvWTNgTks5uhpGygaJpZM4XJLbd .

azjezz commented 6 years ago

thanks for clarifying @weierophinney , i have just finished my validus/cache package, does your session persistence accept any psr6/psr16 implementation or does it include one that it uses ?

weierophinney commented 6 years ago

It accepts any psr-6 implementation; I used cache/predis-adapter with mine.

I specifically used psr-6 and not psr-16, as it covers TTL, which is a requirement for sessions as they need to expire.

On Fri, Oct 5, 2018, 8:17 PM azjezz notifications@github.com wrote:

thanks for clarifying @weierophinney https://github.com/weierophinney , i have just finished my validus/cache https://github.com/ValidusPHP/cache package, does your session persistence accept any psr6/psr16 implementation or does it include one that it uses ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-expressive-session-ext/issues/27#issuecomment-427535170, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlV3GAkETKVDId4BhJVNCG9vR1NJMgks5uiASfgaJpZM4XJLbd .

azjezz commented 6 years ago

@weierophinney i had nothing to do today, and i needed sessions to continue working on my application so i made something similar to what you are working on, its missing some features ( cookies options, session regeneration .. etc ), but i'll be using it until the PSR16 session persistence is ready.

https://gist.github.com/azjezz/8a32d87df39422a12e3078a35a8c988a

one thing i noticed while doing this ( took me almost 2 hours to track it down ) is that the request is missing 'Cookie' header, so i had to obtain the session cookie from the cookies parameters : $request->getCookieParams();, is that normal ?

weierophinney commented 6 years ago

Yes - swoole puts cookies into a special request property and removes the header. The normal place to look for cookies in psr-7 server-side requests is the cookie params, so that's where we place it when marshalling the request.

On Sun, Oct 7, 2018, 6:14 PM azjezz notifications@github.com wrote:

@weierophinney https://github.com/weierophinney i had nothing to do today, and i needed sessions to continue working on my application so i made something similar to what you are working on, its missing some features ( cookies options, cache .. etc ), but i'll be using it until the PSR16 session persistence is ready.

https://gist.github.com/azjezz/8a32d87df39422a12e3078a35a8c988a

one thing i noticed while doing this ( took me almost 2 hours to track it down ) is that the request is missing 'Cookie' header, so i had to obtain the session cookies from the cookies parameters : $request->getCookieParams();, is that normal ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-expressive-session-ext/issues/27#issuecomment-427695408, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlV6SeIW3kyvJWlupAzY5axF5qbV4Uks5uioq7gaJpZM4XJLbd .

azjezz commented 6 years ago

@weierophinney i guess that's 1 reason why this package is not compatible with swoole then.

froschdesign commented 6 years ago

@azjezz Can you explain why you think this package is not compatible with Swoole?

weierophinney commented 6 years ago

@azjezz and @froschdesign — The reason this package isn't compatible with swoole is because Swoole runs in the CLI, and the CLI SAPI doesn't work with ext-session. It has nothing to do with where cookie information is found in the request instance.

Part of the reason we introduced zend-expressive-session in the first place is because we already knew that ext-session would not be a viable way to handle sessions when you run Expressive in non-traditional environments. At the time, it was due to experiments with ReactPHP, but that experience could be extrapolated to Icicle, Swoole, etc. This was the reason we ensured that zend-expressive-session could work directly with a PSR-7 request only. Now we just need to introduce adapters that do not use ext-session to demonstrate this feature. As I've noted previously in the thread, I've created one for myself that uses PSR-6, and I'll try and get it posted in the next couple days.

azjezz commented 6 years ago

this issue should be closed as this package is not compatible with swoole.

a new session persistence has been release which support swoole that makes use of PSR-6 cache. zendframework/zend-expressive-session-cache i have made a package that provides container factories for symfony/cache which is compatible with PSR-6 and zend component installer that you can use. validus/cache