Skip to content

logging: thread starvation for lower-priority producers #101401

@lromeraj

Description

@lromeraj

Describe the bug

Due to the current implementation, if the logging thread is configured with a higher priority than other threads that produce log messages at a lower priority, there is a possibility that the logging thread can starve those lower-priority threads indefinitely.

In some scenarios, users may have expensive tasks that take a considerable amount of time to execute. In such cases, it is reasonable to increase the priority of the logging thread in order to observe log messages while those tasks are running.

It is important to note that this issue can only occur when there are also higher-priority threads producing log messages. The following scenario illustrates the problem.

  • P(A) < P(L) < P(B)
  • L is the logging thread

  • Thread A
    Calls z_log_msg_enqueue() and reaches msg_alloc(), but is interrupted by Thread B, which has higher priority. The message has not yet been committed.

  • Thread B
    Preempts Thread A, calls z_log_msg_enqueue(), and reaches msg_commit(), causing z_log_msg_post_finalize() to be called.

  • Thread L
    Calls z_impl_log_process() and attempts to claim Thread A’s message. Since the message has not yet been committed, z_log_msg_claim() returns NULL.
    Next, z_log_msg_pending() is called and returns true because the message is still present in the buffer but not committed. At this point, an endless loop begins.

  • Thread L (repeats)
    Calls z_impl_log_process() again, attempts to claim Thread A’s message, receives NULL from z_log_msg_claim(), and z_log_msg_pending() continues to return true.

  • And so on…


From this point onward, the logging thread becomes stuck in a tight loop and prevents lower-priority threads from ever being scheduled, resulting in indefinite starvation.

Regression

  • This is a regression.

Steps to reproduce

Create a sample application with two log-producing threads: one running at a higher priority than the logging thread and one running at a lower priority. During execution, the logging thread eventually becomes stuck in an infinite loop.

Relevant log output

Impact

Intermittent – Occurs occasionally; hard to reproduce.

Environment

N/A

Additional Context

This problem was already reported in the following discussion #68289

Metadata

Metadata

Assignees

Labels

area: LoggingbugThe issue is a bug, or the PR is fixing a bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions