When a request is cancelled, the promise seems to not be cancelled.

未关闭
#534 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
php
领域
api, backend

调研方向

Start with src/Middleware/RequestBodyBufferMiddleware.php and reproduce the delayed handler using the provided PHP server and browser AbortController example. Compare the 3.x behavior with the linked comparison, then verify that cancelling the request no longer allows the delayed handler to continue.

由索引模型根据 Issue 内容生成。

描述

bug

When a request is cancelled, the promise seems to not be cancelled.

<?php


require __DIR__ . '/../vendor/autoload.php';

use function React\Async\async;
use function React\Async\delay;
use function React\Promise\resolve;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

$http = new React\Http\HttpServer(
    function (ServerRequestInterface $request, callable $next) {
        $withHeaders = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => '*',
            'Access-Control-Allow-Headers' => '*',
            'Access-Control-Expose-Headers' => '*',
        ];

        if ($request->getMethod() == 'OPTIONS') {
            return new Response(200, $withHeaders, 'OK');
        }
        return resolve($next($request))->then(
            function ($response) use ($withHeaders) {
                foreach ($withHeaders as $key => $value) {
                    if (!$response->hasHeader($key)) {
                        $response = $response->withHeader($key, $value);
                    }
                }
                return $response;
            }
        );
    },
    function (Psr\Http\Message\ServerRequestInterface $request) {
        return async(function () {
            echo "hello world 1\n";
            delay(3);
            echo "hello world 2\n";
            return React\Http\Message\Response::plaintext(
                "Hello World!\n"
            )->withHeader('Access-Control-Allow-Origin', '*')
                ->withHeader('Access-Control-Allow-Methods', '*')
                ->withHeader('Access-Control-Allow-Headers', '*')
                ->withHeader('Access-Control-Expose-Headers', '*');
        })();
    }
);
$socket = new React\Socket\SocketServer(getenv('LISTEN') ?: '0.0.0.0:8070');
$http->listen($socket);

echo "Server running at http://127.0.0.1:8070" . PHP_EOL;

front Cancel request after 1 seconds.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       const controller = new AbortController();
        const signal = controller.signal;

        setTimeout(() => {
            controller.abort();
        }, 1000);

        fetch('http://192.168.1.9:8070', { 
            signal, 
            method: 'post',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
				'hello':'world'
            })
        })
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(error => {
            if (error.name === 'AbortError') {
            console.log('请求已取消');
            } else {
            console.error('请求失败:', error);
            }
        });
    </script>
</body>

</html>

After the cancellation of the 1 second, the backend also displayed "hello world 2".

when modify the https://github.com/reactphp/http/blob/3.x/src/Middleware/RequestBodyBufferMiddleware.php ,it work for me

link

主要语言
PHP
星标
781
派生
166
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

reactphp/http 的其他 Issue

查看 reactphp/http 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。