Fix: Automatically switch to stream handler in coroutine context to resolve HTTPS connection failures #20
+801
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When executing stream responses in Hyperf's coroutine environment (e.g., Command with
$coroutine = true
), users encountered "Connection refused" errors for HTTPS connections, even though the same code worked perfectly in non-coroutine contexts:Root Cause
As identified in issue #XX, this is related to Swoole being compiled with older OpenSSL versions. The cURL handler may not properly handle HTTPS connections within coroutine contexts, leading to connection failures despite valid credentials and network connectivity.
Solution
This PR implements automatic coroutine detection and intelligently switches to PHP's stream handler in coroutine contexts. The stream handler is a pure PHP implementation that doesn't depend on Swoole's SSL implementation, making it stable across different OpenSSL versions.
Key Changes
1. Coroutine Detection (
HttpHandlerFactory::isInCoroutineContext()
)Added robust detection for both Swoole and Hyperf Engine coroutines:
2. Automatic Handler Switching
Modified
create()
method to automatically use stream handler in coroutine contexts:3. Smart Recommendations
Updated
getRecommendedHandler()
to recommend stream handler in coroutine contexts while maintaining cURL preference in non-coroutine environments for optimal performance.Usage
Zero Configuration (Recommended)
The fix works automatically with no code changes required:
Explicit Configuration (Optional)
Users can still override the handler if needed:
Benefits
Testing
HttpHandlerFactoryTest
)Documentation
COROUTINE_FIX.md
)FLOW_DIAGRAM.md
)Compatibility
Closes #XX
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/3ba905c11371512af9d9bdd27d99b782216b6945
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/a8d15584bafb0f0d9d938827840060fd4a3ebc99
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/PHP-DI/PhpDocReader/zipball/66daff34cbd2627740ffec9469ffbac9f8c8185c
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/Seldaek/monolog/zipball/10d85740180ecba7896c87e06a166e0c95a0e3b6
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/adbario/php-dot-notation/zipball/a94ce4493d19ea430baa8d7d210a2c9bd7129fc2
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/aws/aws-sdk-php/zipball/09f1e434cbeed5b916f572369b9f09389b13a5c0
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/awslabs/aws-crt-php/zipball/d71d9906c7bb63a28295447ba12e74723bd3730e
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/clue/reactphp-ndjson/zipball/392dc165fce93b5bb5c637b67e59619223c931b0
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/composer/pcre/zipball/b2bed4734f0cc156ee1fe9c0da2550420d99a21e
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/composer/semver/zipball/198166618906cb2de69b95d7d47e5fa8aa1b2b95
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/composer/xdebug-handler/zipball/6c1925561632e83d60a44492e0b344cf48ab85ef
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/doctrine/inflector/zipball/6d6c96277ea252fc1304627204c3d5e6e15faa3b
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/doctrine/instantiator/zipball/0a0fa9780f5d4e507415a065172d26a98d02047b
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/dtyq/php-mcp/zipball/e10e761086d2116a54bc62103d6fd6a701b04684
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/guzzle/guzzle/zipball/b51ac707cfa420b7bfd4e4d5e510ba8008e822b4
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/guzzle/promises/zipball/481557b130ef3790cf82b713667b43030dc9c957
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/guzzle/psr7/zipball/21dc724a0583619cd1652f673303492272778051
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hamcrest/hamcrest-php/zipball/f8b1c0173b22fa6ec77a81fe63e5b01eba7e6487
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/cache/zipball/40c786b5d1c85f41dd8692d7c3d9e2d187bce728
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/code-parser/zipball/340fb9902465bfc6d26d8b5a6d8d369590ea6e57
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/codec/zipball/95cef5a6b4ecdb4d87b912493839b2a081723cbb
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/collection/zipball/e49707e7f5e33f89250e9a4887065227e2b48947
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/conditionable/zipball/dec9dec9dbde14e20f3d7ba000c3302381019de1
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/config/zipball/fb0b51ba1cb9fb1e688bbe18ac36d0a7e4d7901b
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/context/zipball/ac666862d644db7d813342c880826a1fda599bdf
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/contract/zipball/6ef2c7f98917c52ccda3a37ae65b190848dde6c4
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/coordinator/zipball/a0497d2a260f166ab53fed2eca6bb4e48b49ef56
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/coroutine/zipball/5b474c4bb46be015f1340939d92931b96a0b0cad
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/di/zipball/edc8ab67b4a12664329f89b0f145309a332b60d0
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/engine-contract/zipball/26a18ec0375147546bf9702b0fd737fdd2cec1d6
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/engine/zipball/6595d2659ce7ebb940b8740a00ecd199128398f0
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/logger/zipball/f01c5885fbc22b27962b1ec147a824b3d1d740b2
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/macroable/zipball/14b0a00d3ab6c768aaa5c34222b84000bb75eb29
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/pipeline/zipball/cc588b9f780ee2cab94e0ef80578bf075e2b0f67
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/qdrant-client/zipball/70162fbe8e69f46b6258dcc2c26b91676f61626c
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/retry/zipball/1ab2b3f884d0688c9fa854a06f0d634c0b6dc9bb
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/serializer/zipball/03c8a4840e0a7be83670c2fb0f850a2204fad076
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/stdlib/zipball/13393734a4cc6c9878390b1f6b0fc7e5202c6b59
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/stringable/zipball/3e3501501e2a0e2c5fffff515da2f31f49f655f7
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/support/zipball/6d6c19bb2aa0f6516bd95427725ccb1df1fc65ae
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/tappable/zipball/f5c5d343c95238dcb3fe500876ceadc175e221f8
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/hyperf/utils/zipball/4b13a567a61d08a3c4d058499e28a5b26fc18f1c
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/igorw/evenement/zipball/0a16b0d71ab13284339abb99d9d2bd813640efbc
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/jmespath/jmespath.php/zipball/a2a865e05d5f420b50cc2f85bb78d565db12a6bc
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/jsonrainbow/json-schema/zipball/ac0d369c09653cf7af561f6d91a705bc617a87b8
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/marc-mabe/php-enum/zipball/bb426fcdd65c60fb3638ef741e8782508fda7eef
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/mockery/mockery/zipball/1f4efdd7d3beafe9807b08156dfcb176d18f1699
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/nikic/PHP-Parser/zipball/715f4d25e225bc47b293a8b997fe6ce99bf987d2
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/php-fig/http-client/zipball/bb5906edc1c324c9a05aa0873d40117941e5fa90
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/php-fig/http-factory/zipball/2b4765fddfe3b508ac62f829e852b1501d3f6e8a
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/php-fig/http-message/zipball/402d35bcb92c70c026d1a6a9883f06b2ead23d71
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/php-fig/simple-cache/zipball/764e0b3939f5ca87cb904f570ef9be2d78a07865
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/phpstan/phpstan/zipball/2770dcdf5078d0b0d53f94317e06affe88419aa8
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/cache/zipball/d47c472b64aa5608225f47965a484b75c7817d5b
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/child-process/zipball/1721e2b93d89b745664353b9cfc8f155ba8a6159
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/dns/zipball/eb8ae001b5a455665c89c1df97f6fb682f8fb0f5
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/event-loop/zipball/bbe0bd8c51ffc05ee43f1729087ed3bdf7d53354
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/promise/zipball/23444f53a813a3296c1368bb104793ce8d88f04a
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/socket/zipball/23e4ff33ea3e160d2d1f59a0e6050e4b0fb0eac1
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/reactphp/stream/zipball/1e5b0acb8fe55143b5b426817155190eb6f5b18d
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/schmittjoh/php-option/zipball/638a154f8d4ee6a5cfa96d6a34dfbe0cffa9566d
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/sebastianbergmann/diff/zipball/c41e007b4b62af48218231d6c2275e4c9b975b2e
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/202d0e344a580d7f7d04b3fafce6933e59dae906
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/24ed13d98130f0e7122df55d06c5c4942a577957
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/47e34210757a2f37a97dcd207d032e1b01e64c7a
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/sebastianbergmann/type/zipball/462699a16464c3944eefc02ebdd77882bd3925bf
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/sebastianbergmann/version/zipball/c51fa83a5d8f43f1402e3f32a005e6262244ef17
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/console/zipball/2b9c5fafbac0399a20a2e82429e2bd735dcfb7db
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/event-dispatcher-contracts/zipball/59eb412e93815df44f05f342958efa9f46b1e586
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/event-dispatcher/zipball/b7dc69e71de420ac04bc9ab830cf3ffebba48191
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/filesystem/zipball/edcbb768a186b5c3f25d0643159a787d3e63b7fd
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/finder/zipball/2a6614966ba1074fa93dae0bc804227422df4dfe
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/options-resolver/zipball/0ff2f5c3df08a395232bbc3c2eb7e84912df911d
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/380872130d3a5dd3ace2f4010d95125fde5d5c70
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/3833d7255cc303546435cb650316bff708a1c75c
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-php80/zipball/0cc9dd0f17f61d8131e7df6b84bd344899fe2608
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-php81/zipball/4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/polyfill-php84/zipball/d8ced4d875142b6a7426000426b8abc631d6b191
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/process/zipball/f24f8f316367b30810810d4eb30c543d7003ff3b
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/service-contracts/zipball/f021b05a130d35510bd6b25fe9053c2a8a15d5d4
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/stopwatch/zipball/5a49289e2b308214c8b9c2fda4ea454d8b8ad7cd
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/symfony/string/zipball/f96476035142921000338bad71e5247fbc138872
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/theofidry/cpu-core-counter/zipball/db9508f7b1474469d9d3c53b86f817e344732678
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)https://api.github.com/repos/vlucas/phpdotenv/zipball/24ac4c74f91ee2c193fa1aaa5c249cb0822809af
/usr/bin/php8.3 -n -c /tmp/sfpj34 /usr/bin/composer install --no-interaction
(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>协程内执行流式响应异常</issue_title>
<issue_description>## 单文件模式(正常)
Hyperf Command 模式(报错)
报错如下: