Commit 7ace53e
committed
fix a race in communication between Task and Motion
Fixes #2386
Before this commit, communication of the emcmot_command_t from Task to
Motion had a race condition. The race could cause Motion to process a
command buffer consisting of some old fields from the previous command
and some new fields from the current command. The race is demonstrated
by the following sequence of events:
1. Motion starts `emcmotCommandHandler()`. It gets to command.c line 421
and reads the (old) `head` and `tail`, sees that they are the same,
and believes that the command buffer is in a consistent state (i.e.,
not in the process of being updated).
2. Task starts `usrmotWriteEmcmotCommand()`. It gets to usrmotintf.cc
line 96 and starts writing a new command to shared memory. It writes
`head` and `commandNum`, but nothing after that.
3. Motion continues, gets to command.c line 425, sees the new
`commandNum`, and believes that it has a new command. Motion starts
processing the command buffer, even though most of the fields haven't
been updated by Task yet.
4. Task finishes copying the command into the shared memory buffer,
but it's too late; Motion has processed an inconsistent command.
This commit fixes the race by doing away with the broken `head`/`tail`
synchronization mechanism and replacing it with a mutex in the emcmot
struct, which protects the command struct.
Task locks the mutex before starting to copy the new command to shared
memory. Motion trylocks the mutex before accessing the command shared
memory. If the trylock fails, Motion gives up for now and tries again
on the next invocation.
I'm not sure why this bug doesn't cause us more problems - i don't think
i've ever noticed Motion executing a command incorrectly. The window
for the race is very small, maybe we've just been getting lucky?
Seems fishy...
Also, the head/tail synchronization mechanism probably worked fine back
when it was written, before multi-processor systems were common.1 parent b8996e2 commit 7ace53e
6 files changed
Lines changed: 31 additions & 17 deletions
File tree
- src/emc
- motion-logger
- motion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
| 315 | + | |
| 316 | + | |
319 | 317 | | |
320 | 318 | | |
| 319 | + | |
321 | 320 | | |
322 | 321 | | |
323 | 322 | | |
| |||
706 | 705 | | |
707 | 706 | | |
708 | 707 | | |
| 708 | + | |
| 709 | + | |
709 | 710 | | |
710 | 711 | | |
711 | 712 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
401 | 401 | | |
402 | 402 | | |
403 | 403 | | |
| 404 | + | |
404 | 405 | | |
405 | 406 | | |
406 | 407 | | |
| 408 | + | |
| 409 | + | |
407 | 410 | | |
408 | | - | |
| 411 | + | |
409 | 412 | | |
410 | 413 | | |
411 | 414 | | |
| |||
417 | 420 | | |
418 | 421 | | |
419 | 422 | | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | 423 | | |
426 | 424 | | |
427 | 425 | | |
| |||
2021 | 2019 | | |
2022 | 2020 | | |
2023 | 2021 | | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
| 2030 | + | |
| 2031 | + | |
| 2032 | + | |
| 2033 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
692 | 692 | | |
693 | 693 | | |
694 | 694 | | |
695 | | - | |
696 | 695 | | |
697 | 696 | | |
698 | | - | |
699 | 697 | | |
700 | 698 | | |
701 | 699 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
218 | 217 | | |
219 | 218 | | |
220 | 219 | | |
| |||
265 | 264 | | |
266 | 265 | | |
267 | 266 | | |
268 | | - | |
269 | 267 | | |
270 | 268 | | |
271 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
| 20 | + | |
17 | 21 | | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
84 | 83 | | |
85 | 84 | | |
86 | | - | |
87 | | - | |
| 85 | + | |
88 | 86 | | |
89 | 87 | | |
90 | 88 | | |
91 | 89 | | |
92 | 90 | | |
93 | 91 | | |
94 | 92 | | |
| 93 | + | |
95 | 94 | | |
| 95 | + | |
96 | 96 | | |
| 97 | + | |
| 98 | + | |
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
| |||
0 commit comments