Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling SelectorSocketTransport.writelines with a very large payload doesn't get completely written to the socket #103462

Closed
alisaifee opened this issue Apr 12, 2023 · 1 comment · Fixed by #103463
Assignees
Labels
3.12 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@alisaifee
Copy link
Contributor

alisaifee commented Apr 12, 2023

Bug report

As a consequence of the optimization introduced in #91166, when SelectorSocketTransport.writelines is used with a very large buffer which can't be written in one shot either by using socket.send or socket.sendmsg the remaining data in the buffer is never written to the socket.

The following example can be used to reproduce the error:

import asyncio


async def send_message(chunk_size: int, chunks: int):
    await asyncio.sleep(1)
    reader, writer = await asyncio.open_connection("127.0.0.1", 9898)
    data = [bytes(chunk_size)] * chunks + [b"end"]
    writer.writelines(data)
    await writer.drain()
    writer.close()
    await writer.wait_closed()


async def handle_message(reader, writer):
    data = await reader.read()
    print(f"End marker: {data[-3:]!r}")
    writer.close()
    await writer.wait_closed()


async def run_server():
    server = await asyncio.start_server(handle_message, "127.0.0.1", 9898)
    async with server:
        await server.serve_forever()


async def main():
    await asyncio.gather(run_server(), send_message(6500, 100))


asyncio.run(main())

The example above prints End marker: b'end' with python 3.11 but not with python 3.12.0a6+

Your environment

  • CPython versions tested on: 3.12.0a7
  • Operating system and architecture: macOS 13.2

Linked PRs

@kumaraditya303
Copy link
Contributor

Good catch! I have been running this code for a while but the buffers aren't large enough to trigger this.

@kumaraditya303 kumaraditya303 added topic-asyncio 3.12 bugs and security fixes labels Apr 12, 2023
@kumaraditya303 kumaraditya303 self-assigned this Apr 12, 2023
kumaraditya303 pushed a commit that referenced this issue Apr 13, 2023
carljm added a commit to carljm/cpython that referenced this issue Apr 13, 2023
* main:
  pythongh-103479: [Enum] require __new__ to be considered a data type (pythonGH-103495)
  pythongh-103365: [Enum] STRICT boundary corrections (pythonGH-103494)
  pythonGH-103488: Use return-offset, not yield-offset. (pythonGH-103502)
  pythongh-103088: Fix test_venv error message to avoid bytes/str warning (pythonGH-103500)
  pythonGH-103082: Turn on branch events for FOR_ITER instructions. (python#103507)
  pythongh-102978: Fix mock.patch function signatures for class and staticmethod decorators (python#103228)
  pythongh-103462: Ensure SelectorSocketTransport.writelines registers a writer when data is still pending (python#103463)
  pythongh-95299: Rework test_cppext.py to not invoke setup.py directly (python#103316)
aisk pushed a commit to aisk/cpython that referenced this issue Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
2 participants