asyncio SSL loses received data when `get_buffer()` returns a `bytearray`
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
Over TLS, a BufferedProtocol returning a bytearray from get_buffer() gets NULs instead of data - with the byte count still reported as correct.
Repro (run from a CPython directory root):
import asyncio
import ssl
D = "Lib/test/certdata/"
class Buffered(asyncio.BufferedProtocol):
buf = bytearray(100)
def get_buffer(self, hint): return self.buf
def buffer_updated(self, n): print("BufferedProtocol:", bytes(self.buf[:n]))
class Plain(asyncio.Protocol):
def data_received(self, data): print("Protocol :", data)
async def h(r, w):
w.write(b"A"*30); await w.drain(); w.write(b"B"*30)
async def main():
srv_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); srv_ctx.load_cert_chain(D + "keycert3.pem")
cli_ctx = ssl.create_default_context(cafile=D + "pycacert.pem")
srv = await asyncio.start_server(h, "localhost", 0, ssl=srv_ctx)
port = srv.sockets[0].getsockname()[1]
loop = asyncio.get_running_loop()
for proto in (Buffered, Plain):
await loop.create_connection(proto, "localhost", port, ssl=cli_ctx)
await asyncio.sleep(0.3)
asyncio.run(main())
The output would be, Protocol is here to have an example of proper behavior
BufferedProtocol: b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Protocol : b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'
Doc says (asyncio-protocol.rst - 620)
get_buffer() must return an object implementing the buffer protocol.
Proposed fix is to take memoryview(buf) once before the loop and read into view[offset:]
I have a fix ready for that
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-156276
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先查看 asyncio 協定文件中 asyncio-protocol.rst 約第 620 行的內容,並重現 issue 中的 TLS BufferedProtocol 範例。接著檢查連結的 PR gh-156276 以及相關的 asyncio SSL 傳輸測試;當 bytearray 緩衝區能保留接收到的 TLS 資料,且該回歸問題有測試涵蓋時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- networking
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100