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

TcpStream cannot write more than 5760 bytes on ESP32 #6805

Open
victorbnl opened this issue Aug 29, 2024 · 3 comments
Open

TcpStream cannot write more than 5760 bytes on ESP32 #6805

victorbnl opened this issue Aug 29, 2024 · 3 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-net Module: tokio/net

Comments

@victorbnl
Copy link

victorbnl commented Aug 29, 2024

Version

tokio-tcplistener-issue v0.1.0 (/home/victor/dev/tokio-tcplistener-issue)
└── tokio v1.39.3

Platform

Linux Victor-Laptop 6.10.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Tue, 06 Aug 2024 07:21:19 +0000 x86_64 GNU/Linux

Code running on an Ai Thinker ESP32-CAM

Description

The code below prints "Wrote 5760 bytes" and blocks forever on the next call to write. I expect it to write all the 8000 bytes to the TCP stream.

Adding a call to flush right after writing does not solve the problem.

The issue happens too with write_all (it blocks forever). It doesn’t occur on a std::net::TcpListener.

Code

Required tokio features: io-util,net,rt

use std::thread;

use esp_idf_svc::{wifi, eventloop, nvs};
use esp_idf_svc::io::vfs;
use esp_idf_svc::hal::peripherals;

use tokio::net::TcpListener;
use tokio::io::AsyncWriteExt;

fn main() {
    esp_idf_svc::sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    let peripherals = peripherals::Peripherals::take().unwrap();
    let sys_loop = eventloop::EspSystemEventLoop::take().unwrap();
    let nvs = nvs::EspDefaultNvsPartition::take().unwrap();

    let mut wifi = wifi::BlockingWifi::wrap(
        wifi::EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs)).unwrap(),
        sys_loop,
    ).unwrap();

    let wifi_configuration = wifi::Configuration::AccessPoint(wifi::AccessPointConfiguration {
        ssid: "ESP32".try_into().unwrap(),
        password: "PASSWORD".try_into().unwrap(),
        ..Default::default()
    });

    wifi.set_configuration(&wifi_configuration).unwrap();
    wifi.start().unwrap();
    wifi.wait_netif_up().unwrap();

    vfs::initialize_eventfd(5).unwrap();

    thread::Builder::new()
        .stack_size(60_000)
        .spawn(|| {
            tokio::runtime::Builder::new_multi_thread()
                .enable_all()
                .build()
                .unwrap()
                .block_on(async {
                    let listener = TcpListener::bind("0.0.0.0:8080").await.unwrap();

                    loop {
                        let (mut stream, _) = listener.accept().await.unwrap();

                        let data = [1; 8000];

                        let mut written = 0;
                        while written < 8000 {
                            written += stream.write(&data[written..]).await.unwrap();
                            println!("Wrote {} bytes", written);
                        }
                    }
                });
        }).unwrap();

    core::mem::forget(wifi);
}
@victorbnl victorbnl added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Aug 29, 2024
@Darksonn Darksonn added the M-net Module: tokio/net label Aug 29, 2024
@Darksonn
Copy link
Contributor

What is it connecting to? Is the peer reading the data?

@victorbnl
Copy link
Author

What connects to this server is just a simple TCP client on my computer that reads exactly 8000 bytes of data and writes it to a file. I can confirm it reads it because with a std::net::TcpListener, the full data is written. With tokio, however, the client is stuck forever on read_exact (or, if using read, reads 5760 bytes and gets stuck on the next read).

@Darksonn
Copy link
Contributor

Darksonn commented Sep 3, 2024

Well, could you share the client?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-net Module: tokio/net
Projects
None yet
Development

No branches or pull requests

2 participants