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

UdpSocket created and used inside async function doesn't work properly #2193

Closed
yanshay opened this issue Nov 16, 2023 · 8 comments
Closed

Comments

@yanshay
Copy link

yanshay commented Nov 16, 2023

I wrote a small program (ESP32/no_std, using embassy/embassy-net) that sends UDP packets and it work just fine.

When I moved the networking related code into an async function, it stopped working, UDP Packets wouldn't be sent. Everything seemed to progress properly w/o any errors, but the packets weren't sent.

I pinpointed it to the move of the UdpSocket::new into the async function. Once it moved into the async function the packets weren't sent. For the program to work, the UdpSocket has to be created in the main() and all the rest can run inside the async function.

I thought it might have to do with #2175 but the buffers are created in the main function which never ends (so exist before and after the networking code, so no memory issues).
I tried letting the executor run after calling the new (using Time::after) , in case something needs to be processed before continuing in the net_task of .

Any ideas what's the reason? Is this maybe by design?

@yanshay yanshay changed the title UdpSocket created and used inside aync function doesn't function properly UdpSocket created and used inside async function doesn't function properly Nov 16, 2023
@yanshay yanshay changed the title UdpSocket created and used inside async function doesn't function properly UdpSocket created and used inside async function doesn't work properly Nov 16, 2023
@Dirbaio
Copy link
Member

Dirbaio commented Nov 16, 2023

could it be that you're creating the socket, sending some packet, then immediately dropping the socket?

"sending" the packets only enqueues them in the socket buffers, the socket needs to "stick around" for them to actually be sent.

@yanshay
Copy link
Author

yanshay commented Nov 16, 2023

could it be that you're creating the socket, sending some packet, then immediately dropping the socket?

"sending" the packets only enqueues them in the socket buffers, the socket needs to "stick around" for them to actually be sent.

The buffers are in the main function, so never dropped.
I await the send_to and then return from the function, so the socket is dropped then.
Does the send_to.await return before the UDP packet is actually sent? If so, then why does it need to be awaited? And how can I wait until it gets sent?

@Dirbaio
Copy link
Member

Dirbaio commented Nov 16, 2023

The buffers are in the main function, so never dropped. I await the send_to and then return from the function, so the socket is dropped then.

The buffers don't matter, it's the socket what must not get dropped.

Does the send_to.await return before the UDP packet is actually get sent? If

"send_to" only enqueues them in the socket buffers, the socket needs to "stick around" for them to actually be sent.

If so, then why does it need to be awaited?

if the socket buffers are full, it waits until some packets are sent so there's free space.

And how can I wait until it gets sent?

Currently, the only way is to not drop the socket. We could add a async fn flush(&mut self) to wait for all queued packets to be sent, like TCP has. PRs are welcome if you'd be interested in adding it.

@ProfFan
Copy link
Contributor

ProfFan commented Oct 18, 2024

Encountered the same problem today.

I am interested in implementing a flush function for this, @Dirbaio any directions?

@ProfFan
Copy link
Contributor

ProfFan commented Oct 19, 2024

Ok this is blocked by smoltcp-rs/smoltcp#838

@ProfFan
Copy link
Contributor

ProfFan commented Oct 19, 2024

Pushed PR on upstream for exporting the necessary symbols

@ProfFan
Copy link
Contributor

ProfFan commented Oct 23, 2024

This issue can be closed?

@ProfFan
Copy link
Contributor

ProfFan commented Oct 23, 2024

@Dirbaio

@Dirbaio Dirbaio closed this as completed Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants