MagicStack / MagicStack/httptools

`on_url` not called intermittently?

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

还没有人认领这个 Issue。

主要语言
Python
星标
1.3k
派生
107
PR 合并指标
30 天内没有已合并 PR

描述

From time to time, my server output an error that let me think that Request.on_url is not called for the current stream.

Here is a small script to reproduce the behaviour:

import asyncio

from httptools import HttpRequestParser


class Request:

    def __init__(self):
        self.EOF = False

    def on_url(self, url: bytes):
        self.on_url_called = True

    def on_message_complete(self):
        self.EOF = True


async def serve(reader, writer):
    chunks = 2 ** 16
    req = Request()
    parser = HttpRequestParser(req)
    while True:
        data = await reader.read(chunks)
        parser.feed_data(data)
        if not data or req.EOF:
            break
    assert req.on_url_called
    writer.write(b'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK')
    writer.write_eof()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    coro = loop.create_task(asyncio.start_server(serve, '127.0.0.1', 8080))
    server = loop.run_until_complete(coro)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        print('Bye.')
    finally:
        server.close()
        loop.run_until_complete(server.wait_closed())
        loop.close()

To be run with python file.py

And then benchmark it with something like ab -n 10000 -c 8 'http://127.0.0.1:8080/'

Once every, say, 10000 requests, I get this error:

Task exception was never retrieved
future: <Task finished coro=<serve() done, defined at test_httptools.py:18> exception=AttributeError("'Request' object has no attribute 'on_url_called'",)>
Traceback (most recent call last):
  File "test_httptools.py", line 27, in serve
    assert req.on_url_called
AttributeError: 'Request' object has no attribute 'on_url_called'

Any hint? :)

Thanks!

贡献指南

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

从这里开始

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

调研方向

使用 test_httptools.py 中的脚本以及报告中描述的 ab 基准测试,重现间歇性故障。从 HttpRequestParser 和 parser.feed_data 入手,然后追踪请求数据如何到达 Request.on_url。当确定有效请求为何可以在未设置 on_url_called 的情况下完成,并为该行为添加或更新覆盖测试后,即视为完成。

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

评估

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

把新 issue 发到你的邮箱

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