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

core/thread_flags: add thread_flags_wait_any_or_mbox() #18977

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

maribu
Copy link
Member

@maribu maribu commented Nov 25, 2022

Contribution description

This adds thread_flags_wait_any_or_mbox() that blocks until any of a given thread flags is set, or a message is retrieved from an mbox - whatever happens first.

This then was used to implement ztimer_mbox_get_timeout() (which relates to mbox_get() the same way ztimer_mutex_lock_timeout() relates to mutex_lock()).

Finally, ztimer_mbox_get_timeout() is used in GNRC to fix a nasty race condition.

Testing procedure

A test application is provided.

Issues/PRs references

Alternative to #18949 and #19965

@github-actions github-actions bot added Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: tests Area: tests and testing framework labels Nov 25, 2022
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this saves us from hacks like #19965 I'm all for it!
Can you provide an example how this solves the issue in GNRC?
I have a test setup that provokes the issue quite reliably.

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 11, 2023
@riot-ci
Copy link

riot-ci commented Oct 11, 2023

Murdock results

FAILED

963d3b3 sys/net/gnrc_sock: use ztimer_mbox_get_timeout()

Success Failures Total Runtime
157 0 7989 40s

Artifacts

@MrKevinWeiss
Copy link
Contributor

Since we have an ACK and it is agreed that this is proper fix I would suggest we merge before the hard feature freeze today? Any objections?

@maribu
Copy link
Member Author

maribu commented Oct 13, 2023

I'm sadly afk, otherwise I would suggest that I add a big warning that the function is super mega experimental. That would give some leverage to revert, if needed be.

@maribu maribu force-pushed the core/mbox/thread_flags branch from e371762 to 04681db Compare October 16, 2023 07:32
@maribu
Copy link
Member Author

maribu commented Oct 16, 2023

⬆️ First a rebase without any changes

@maribu maribu changed the title RFC: core/thread_flags: add thread_flags_wait_any_or_mbox() core/thread_flags: add thread_flags_wait_any_or_mbox() Oct 16, 2023
@maribu maribu force-pushed the core/mbox/thread_flags branch from 4b100e1 to 8c7f4b4 Compare October 16, 2023 10:45
@github-actions github-actions bot added Area: network Area: Networking Area: doc Area: Documentation Area: timers Area: timer subsystems Area: boards Area: Board ports Area: sys Area: System labels Oct 16, 2023
@maribu
Copy link
Member Author

maribu commented Oct 16, 2023

Can you provide an example how this solves the issue in GNRC?

Done. Care to test?

@maribu maribu force-pushed the core/mbox/thread_flags branch from 8c7f4b4 to debc55d Compare October 16, 2023 10:47
@maribu
Copy link
Member Author

maribu commented Oct 16, 2023

A commit of a different PR sneaked in, sorry

@github-actions github-actions bot removed Area: doc Area: Documentation Area: boards Area: Board ports labels Oct 16, 2023
@benpicco
Copy link
Contributor

benpicco commented Oct 16, 2023

Hm I applied 17e12fc, c8853f1 and 8a2bc1f but I still see

>>> timeout happened 9847 µs early <<<
>>> timeout happened 1166 µs early <<<
>>> timeout happened 9824 µs early <<<
>>> timeout happened 9818 µs early <<<
>>> timeout happened 9794 µs early <<<
>>> timeout happened 9825 µs early <<<
>>> timeout happened 9400 µs early <<<
>>> timeout happened 9730 µs early <<<
>>> timeout happened 1043 µs early <<<
>>> timeout happened 9667 µs early <<<

with

    unsigned now = ztimer_now(ZTIMER_USEC);
    while (1) {
        res = gnrc_sock_recv((gnrc_sock_reg_t *)sock, &pkt, timeout, &tmp, &_aux);
        /* HACK: gnrc_sock_recv() sometimes returnes -ETIMEDOUT too early */
        now = ztimer_now(ZTIMER_USEC) - now;
        if (res == -ETIMEDOUT && now < (timeout - timeout/10))  {
            timeout -= now;
            printf(">>> timeout happened %u µs early <<<\n", timeout);
            continue;
        }
        break;
    }

(from #19965)

@maribu
Copy link
Member Author

maribu commented Oct 16, 2023

Can you provide more details on how to reproduce this? Maybe I can even turn this into a unit test.

@benpicco
Copy link
Contributor

It's unfortunately a bit involved for a unit test, but if you check out the nanocoap_non_block-gnrc_bug branch and run ./test.sh -n 100 init and ./test.sh attach in tests/nanocoap_cli you can cycle through the different native instances in tmux, you'll quickly find some that show the early timeout.

@MrKevinWeiss MrKevinWeiss added the Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch label Oct 23, 2023
@MrKevinWeiss
Copy link
Contributor

It would be good to have either this or #19965 backported to the release...

bors bot added a commit that referenced this pull request Oct 23, 2023
19990: sys/psa_crypto: allow repeated initialization r=benpicco a=mguetschow

### Contribution description

- simple unit test which calls `psa_crypto_init()` twice
- fix to no re-initialize key slots (which left them in a broken state)

### Testing procedure

- `make -C tests/sys/psa_crypto all test` succeeds
- `git checkout HEAD~1 && make -C tests/sys/psa_crypto all test` fails



20011: tests/unittests: add a unit test for ztimer r=benpicco a=maribu

### Contribution description

This adds test coverage for removing ztimers with focus on ensuring that offsets are correctly updated on subsequent timers (e.g. not having timers fire too early).

### Testing procedure

Run the unit tests (will be done by the CI as well). Maybe also introduce a random bug in `ztimer_remove()` and check if this is indeed caught by the unit tests.

### Issues/PRs references

Prompted by #18977 (comment)

Co-authored-by: Mikolai Gütschow <[email protected]>
Co-authored-by: Marian Buschsieweke <[email protected]>
bors bot added a commit that referenced this pull request Oct 23, 2023
20011: tests/unittests: add a unit test for ztimer r=benpicco a=maribu

### Contribution description

This adds test coverage for removing ztimers with focus on ensuring that offsets are correctly updated on subsequent timers (e.g. not having timers fire too early).

### Testing procedure

Run the unit tests (will be done by the CI as well). Maybe also introduce a random bug in `ztimer_remove()` and check if this is indeed caught by the unit tests.

### Issues/PRs references

Prompted by #18977 (comment)

Co-authored-by: Marian Buschsieweke <[email protected]>
bors bot added a commit that referenced this pull request Nov 3, 2023
20009: cpu/native: fix bug in periph_timer r=MrKevinWeiss a=maribu

### Contribution description

While debugging #18977 (comment) it became obvious that the `periph_timer` in `native` is broken and issues early IRQs. This replaces the use of `setitimer` that cannot use a monotonic clock source with `timer_settime()`.

### Testing procedure

I have some non-publishable code that tests if the time an ISR fires in terms of `timer_read()` is no earlier than the time expected. This occasionally triggered with `master`, but I didn't see any of these issues anymore with this PR. I guess I should revive my PR to spice up the periph timer tests and add a polished version of this and let this run for an hour or two.

The tests ins `tests/periph/timer*` should still succeed on `native`. (They do for me in a container running `riot/riotbuild`).

### Issues/PRs references

Found while debugging #18977 (comment)

20042: dist/tools/uf2: add target to also copy families.json file r=MrKevinWeiss a=MichelRottleuthner

### Contribution description

The updated UF2 pkg (#20035) stores the family ID in an external .json file. I overlooked that and flashing fails if this file is not present. This PR fixes it by also copying the json into the tool folder.

### Testing procedure
Check if the `feather-nrf52840-sense` can be flashed when the new UF2 pkg is cloned freshly.


### Issues/PRs references
 Fixes a regression introduced with #20035 


Co-authored-by: Marian Buschsieweke <[email protected]>
Co-authored-by: Michel Rottleuthner <[email protected]>
@MrKevinWeiss
Copy link
Contributor

ping, I think we would like this in the release

@maribu maribu force-pushed the core/mbox/thread_flags branch from 602b464 to 963d3b3 Compare November 13, 2023 12:50
@maribu
Copy link
Member Author

maribu commented Nov 13, 2023

I addressed the whitepsace and spelling issues uncovered by static-tests (sqaushed them right in) and rebased on top of the changes for github merge queues.

I'm almost certain that this does fix a race condition. But the issue at hand in @benpicco's test is then not caused by this. (Nor was it caused by the issue in the periph_timer implementation on native.) However, until that test is indeed passing, we cannot be sure if this fix is just broken, or whether there is indeed a third bug still not found that needs to be addressed as well.

My gut tells me that since the race condition has been there for ages, it might be best to solve this puzzle fully before applying a fix that may not work - even if this means the fix will not get in with this release.

@MrKevinWeiss
Copy link
Contributor

Then I will remove it from the release backport and we can just have a known bug... We are already way over the time.

@MrKevinWeiss MrKevinWeiss removed the Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch label Nov 14, 2023
@benpicco
Copy link
Contributor

#19965 provides a workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: network Area: Networking Area: sys Area: System Area: tests Area: tests and testing framework Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants