emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
307 stars 50 forks source link

Fix a race condition in test_multi_threaded_dates #171

Closed sepeth closed 1 week ago

sepeth commented 3 weeks ago

Hi there,

This fixes a race condition in test_multi_threaded_dates.

If a worker thread takes less than 500 ms, this test fails due to missing MUST_BE_PRINTED lines. This is because the main thread sleeps 500 ms before setting a new spec for trace!.

XXXXX [2024-09-02 09:55:33.010986 +00:00] T["test_multi_threaded_dates"] INFO [tests/test_multi_threaded_dates.rs:41] create many log lines with a considerable number of threads, verify the log
XXXXX [2024-09-02 09:55:33.514113 +00:00] T["test_multi_threaded_dates"] INFO [tests/test_utils.rs:81] Task executed in 503 ms.
test test_multi_threaded_dates ... FAILED

failures:

---- test_multi_threaded_dates stdout ----
thread 'test_multi_threaded_dates' panicked at tests/test_multi_threaded_dates.rs:139:5:
assertion `left == right` failed
  left: 100003
 right: 100008

There is a race condition that can potentially happen but it is a lot less likely: if all of the worker threads finishes before the main thread configures the trace!. This is a lot less likely because the condvar will notify the main thread before the workers start doing IO loop, and main thread no longer sleeps once all worker threads initialized.