Skip to content

nmt: Fix wait_for_bootup race with a following heartbeat - #681

Open
SahilRajputdotexe wants to merge 1 commit into
canopen-python:masterfrom
SahilRajputdotexe:fix/wait-for-bootup-race
Open

SahilRajputdotexe wants to merge 1 commit into
canopen-python:masterfrom
SahilRajputdotexe:fix/wait-for-bootup-race

Conversation

@SahilRajputdotexe

Copy link
Copy Markdown

Fixes #560.

wait_for_bootup() checks _state_received after it has been notified. If a heartbeat is handled before the waiting thread gets to run, _state_received has already been overwritten with the new state, so the boot-up is missed and the call times out. That matches the log in the issue, where the boot-up is followed by a heartbeat within a millisecond. Moving the check inside the lock doesn't help, because the overwrite happens before the waiter gets the lock back.

This keeps the boot-up in a separate flag that later heartbeats don't touch, and waits on it with Condition.wait_for().

The new test delivers a boot-up and a heartbeat while holding the condition lock, so the waiter can only run after both. It times out on master and passes with the change.

One small difference: the timeout is now exactly the value passed in. The old loop could wait about 0.1 s longer.

wait_for_bootup() looked at _state_received after being notified, but a
heartbeat handled before the waiting thread got to run had already
overwritten the boot-up value, so the boot-up was missed and the call
timed out.

Remember the boot-up in its own flag and wait on that with wait_for().

Fixes canopen-python#560
@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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 this pull request may close these issues.

node.nmt.wait_for_bootup misses boot message if a bootmessage is immidiately followed by another heartbeat message

1 participant