[issue35928] socket makefile read-write discards received data

Palle Ravn report at bugs.python.org
Thu Feb 7 07:30:33 EST 2019


New submission from Palle Ravn <p.ravn at samsung.com>:

Using socket.makefile in read-write mode had a bug introduced between version 3.6.6 and 3.6.7. The same bug is present in version 3.7.x.

The below code example will behave very differently between 3.6.6 and 3.6.7. It's based on the echo-server example from the docs.

import socket

HOST = '127.0.0.1'
PORT = 0

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, PORT))

    print(f'Waiting for connection on port {s.getsockname()[1]}')
    s.listen(1)

    conn, addr = s.accept()
    print(f'Connected by {addr}')

    with conn:
        f = conn.makefile(mode='rw')

        while True:
            m = f.readline()
            print(f'msg: {m!r}')

            if not m:
                exit(0)

            f.write(m)
            f.flush()


Python 3.6.7:
Sending the string "Hello\nYou\n" will only print "Hello\n" and also only return "Hello\n" to the client.
Removing the lines with f.write(m) and f.flush() and both "Hello\n" and "You\n" will be returned to the client.
It's like the call to f.write() somehow empties the read buffer.

Python 3.6.6:
Sending "Hello\nYou\n" will return "Hello\n" and "You\n" to the client without any modifications to the above code.

----------
components: IO
messages: 335017
nosy: pravn
priority: normal
severity: normal
status: open
title: socket makefile read-write discards received data
type: behavior
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35928>
_______________________________________


More information about the Python-bugs-list mailing list