-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
drivers: serial: Fix race condition in nRF5 UART TX #7431
drivers: serial: Fix race condition in nRF5 UART TX #7431
Conversation
A little tricky to reproduce, and I don't have an easy test case on hand. I was reproducing this on our own project. At first I tried to lock the scheduler in a printk-heavy area of the code, and that fixed the issue. I investigated further and pinpointed it to the I also found that RIOT-OS had the same issue in the past, with the exact same fix: RIOT-OS/RIOT#5954 |
I also removed the documentation part about checking if the transmitter is empty. This doesn't seem to be done at the moment or before. Maybe writing to Thoughts? |
Codecov Report
@@ Coverage Diff @@
## master #7431 +/- ##
=======================================
Coverage 55.25% 55.25%
=======================================
Files 468 468
Lines 51673 51673
Branches 9893 9893
=======================================
Hits 28554 28554
Misses 19206 19206
Partials 3913 3913 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No objections from me, but will let @anangl take a look at it before I approve.
The patch is okay. Regarding the removed documentation part - indeed, the current implementation does not do exactly what UART API states it should do. Actually, it should wait until the transmitter becomes empty before writing a new character there, i.e. do it like this:
But then, we would fall into a similar issue. When the thread is preempted between clearing the Therefore, I think the patch is the best what we can do to provide "thread safety" here without introducing some locking. |
132d0ac
to
af6de18
Compare
Fix a somewhat rare race condition when the thread gets preempted in the middle of sending a byte through UART. If the other thread also sends another byte through UART and "consumes" the EVENTS_TXDRDY value, the first thread will get stuck in the while loop forever. By moving the reset to the function start, we guarantee that the baseline state of EVENTS_TXRDY is 1. Therefore, the first thread will continue normally when it executes again. Signed-off-by: Thiago Silveira <thiago@exati.com.br>
I chatted with @anangl over IRC and we agreed on the comment in the commit. @carlescufi, I think this is ready now! @anangl, thanks for the explanation and help. |
Hi,
Before this commit shell is responding to the commands. |
@leodesigner I can reproduce, let me open the issue |
Fix a somewhat rare race condition when the thread gets
preempted in the middle of sending a byte through UART.
If the other thread also sends another byte through UART
and "consumes" the EVENTS_TXDRDY value, the first thread
will get stuck in the while loop forever.
By moving the reset to the function start, we guarantee that
the baseline state of EVENTS_TXRDY is 1. Therefore, the first
thread will continue normally when it executes again.
Signed-off-by: Thiago Silveira thiago@exati.com.br