RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

sys/newlib_syscalls_default: fix race condition in __sinit()

Open maribu opened this issue 1 year ago • 2 comments

Contribution description

This eagerly calls __sinit() instead of lazy initialization upon the first call to stdio (e.g. puts(), printf()). The issue is that without locking (as is currently the case for all RIOT platforms but ESP) two concurrent "first calls" may result in concurrent initialization of the same structure and data corruption.

Testing procedure

Have two threads concurrently use stdio and the boot message disabled. This may result in data corruptions and crashes.

See https://github.com/RIOT-OS/RIOT/issues/20067 for details on a reproducible crashing app.

Issues/PRs references

Fixes https://github.com/RIOT-OS/RIOT/issues/20067

maribu avatar Feb 16 '24 05:02 maribu

@mchesser: Could you give this a try?

I'm aware that it still is a pain in the ass that RIOT doesn't support (at least optional) reentrancy for platforms other than ESP. That is in issue that stills needs to be addressed.

However, even people desperate for saving RAM don't like crashes, and this hopefully makes the concurrent use of stdio crash-free.

maribu avatar Feb 16 '24 06:02 maribu

Murdock results

:heavy_check_mark: PASSED

375aed13e6d90c1b7e0fc7263f04ff12a84dc29e sys/newlib_syscalls_default: fix race condition in __sinit()

Success Failures Total Runtime
142848 0 142848 02h:14m:59s

Artifacts

riot-ci avatar Feb 16 '24 06:02 riot-ci

OK, full CI run with tests passed. I guess this is OK to merge.

Note: The CI run failed twice before due to:

  1. No USB connection to a samr21-xpro. (I'd say that is safe to ignore, either the USB cable was disconnected, flaky, or the issue is with the eDBG and not with the firmware on the MCU.)
  2. A test on native failed because some wake up was too slow. This could be due to load, or due to the general flakiness of native.

maribu avatar Feb 20 '24 08:02 maribu

Thx :)

maribu avatar Feb 20 '24 16:02 maribu