Skip to content

add timeout configuration for sync serial reads#509

Open
onur-ozkan wants to merge 1 commit into
mavlink:masterfrom
Orkavian:timeout-config
Open

add timeout configuration for sync serial reads#509
onur-ozkan wants to merge 1 commit into
mavlink:masterfrom
Orkavian:timeout-config

Conversation

@onur-ozkan

@onur-ozkan onur-ozkan commented Jun 6, 2026

Copy link
Copy Markdown
Member

Adds timeout configuration for serial reads.

@onur-ozkan onur-ozkan added the scope:dialects Affects MAVLink dialect definitions or dialect-generated code. label Jun 6, 2026
@onur-ozkan

Copy link
Copy Markdown
Member Author

Pinging @akumaigorodski so they know their change is updated.

@patrickelectric

Copy link
Copy Markdown
Member

Just to be sure, what was broken ?

@onur-ozkan

onur-ozkan commented Jun 6, 2026

Copy link
Copy Markdown
Member Author

Just to be sure, what was broken ?

recv() can time out after 1 millisecond when there are no messages available. If someone wants non-blocking, one-shot read they can use try_read instead. A 1 millisecond timeout will be as instant as try_recv() therefore makes no sense IMO.

@akumaigorodski

Copy link
Copy Markdown
Contributor

Factually my original change fixed 100% CPU core issue while retaining all the functionality. My interpretation of that is without idle timeout it was retrying to read immediately on failure, thus taking all available compute resources.

And then staying idle for 1ms seemed like good enough tradeoff between possible added latency and compute use.

Just in case, 100% CPU was not manifesting itself when RPI was connected to SITL simulator via Ethernet/UDP, but started showing up with UART and actual flight controller.

I don't quite like the way it is implemented now, I'd rather remove factory method with hidden logic entirely and let users, for example, provide their own serialport::new (because someone might need different stop bits etc), adding utility methods for every parameter is not scalable.

@onur-ozkan

Copy link
Copy Markdown
Member Author

I didn't understand what you mean with hidden logic and scalibility to be honest. Can you say more? Scale what exactly? What is hidden ?

@akumaigorodski

Copy link
Copy Markdown
Contributor

Basically I'm proposing to remove a higher level mavlink::connect with different types of strings and its parsing logic, and let users assemble each invariant from their own parts. If this was in place then I would never have an issue configuring my own serialport::new.

@onur-ozkan

Copy link
Copy Markdown
Member Author

Just to be sure, what was broken ?

recv() can time out after 1 millisecond when there are no messages available. If someone wants non-blocking, one-shot read they can use try_read instead. A 1 millisecond timeout will be as instant as try_recv() therefore makes no sense IMO.

My claim about recv() should be wrong here. I forgot that we are doing a loop inside recv(). I thought the timeout was applied to whole recv().

So there shouldn't be anything broken really.

@onur-ozkan

Copy link
Copy Markdown
Member Author

Basically I'm proposing to remove a higher level mavlink::connect with different types of strings and its parsing logic, and let users assemble each invariant from their own parts. If this was in place then I would never have an issue configuring my own serialport::new.

You could use try_recv to avoid all this. Then you can have your own way to receive messages with any specific timeout.

@onur-ozkan

Copy link
Copy Markdown
Member Author

Changed the approach. The default timeout now remains at 1 ms. I think the 100% CPU usage was caused by having no timeout i.e the lopp was constantly spinning in recv(). With a 1 ms timeout each iteration is blocked by the serial port for up to 1 ms which prevents the busy loop. From that aspect the default 1ms timeout makes sense. But it's still worth keeping it configurable since different use cases may benefit from different timeout values.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:dialects Affects MAVLink dialect definitions or dialect-generated code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants