Skip to content

Solving excessive read task CPU usage and question on TCP connection state handling #1010

@42dot-bongjun-hur

Description

@42dot-bongjun-hur

Describe the release item

In Zenoh-Pico's unicast transport, the read thread consumes excessive CPU resources when there is no data to receive. This occurs because the thread is busy-waiting on a non-blocking socket, immediately retrying the read operation in a tight loop. To fix this, I propose adding a short sleep (z_sleep_ms(1)) after an unsuccessful read to allow the CPU to yield to other tasks. This simple change will significantly reduce CPU usage, which is critical for resource-constrained embedded systems.

zenoh-pico\src\transport\unicast\read.c line 358:

            size_t to_read = 0;
            // Retrieve data
            if (!_z_unicast_client_read(ztu, curr_peer, &to_read)) {
                // [Proposed Change] Add a small delay to prevent busy-waiting
                z_sleep_s(1);
                continue;
            }

Additionally, I would like to know how a Zenoh-pico client can detect when its TCP connection to a broker has been terminated. What is the proper way to handle this state in the client application?

Metadata

Metadata

Assignees

No one assigned

    Labels

    releasePart of the next release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions