micropython / micropython/micropython

TCP socket RESET's the connection when not reading the entire read buffer

Open
#17,072 8 comments 0 reactions 1 assignee View on GitHub

@projectgus is already working on this.

Since Jul 31, 2025.

bug
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

Port, board and/or hardware

W5500-EVB-Pico, Raspberry Pi Pico W

MicroPython version

MicroPython v1.24.1 on 2024-11-29; W5500-EVB-Pico with RP2040

Reproduction

When not reading the entire TCP message, micropython will RESET the connection, instead of doing a proper TCP connection teardown.

Example:

import network
from machine import SPI, Pin
import time
import asyncio

def w5x00_init():
    # https://github.com/Wiznet/RP2040-HAT-MicroPython/blob/main/Ethernet%20Example%20Getting%20Started%20%5BMicropython%5D.md
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20))
    nic.active(True)
    nic.ifconfig('dhcp')
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())
    print(nic.ifconfig())

async def handle_client_read_1000(r, w):
    await r.read(1000)
    w.write(b'HTTP/1.0 200 OK\r\nContent-Length: 2\r\nContent-Type: text/plain\r\n\r\nhi')
    w.close()
    await w.wait_closed()

async def server_async(handle_client):
    await asyncio.start_server(handle_client, '0.0.0.0', 80)
    while True:
        await asyncio.sleep(1)
        
w5x00_init()
asyncio.run(server_async(handle_client_read_1000))
Expected behaviour

Reading parts of the TCP stream/reading the entire stream/not reading the stream should not change the behavior of writing into the stream.

When running the same code (except the network setup) in CPython it does a proper TCP connection teardown and does not send RESET.

Observed behaviour

When doing

curl 192.168.2.127

we get a TCP connection teardown after the payload has been send (FIN-ACK):

Image

But when sending a message longer then the buffer (here 1000B) via

curl 192.168.2.127/hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii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iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

we get

Image

I.e. it sends RESET instead of doing a proper connection teardown.

This causes various strange behaviors as seen in the original discussion.

Additional Information

See the original discussion

Code of Conduct

Yes, I agree

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.