Skip to content

Consumer thread in DatabaseLogger has no shutdown path #472

@tylerkron

Description

@tylerkron

Problem

DatabaseLogger.Consumer() runs a while (true) loop with no cancellation mechanism. When Dispose() is called, it disposes the BlockingCollection<DataSample> _buffer, which causes the consumer thread to throw ObjectDisposedException. The exception is caught and logged, but the while(true) loop continues — now repeatedly calling .Count on a disposed collection, logging errors in an infinite loop until the process exits.

Impact

  • Resource leak: consumer thread never terminates
  • Log spam: infinite error logging after disposal
  • Clean shutdown impossible

Suggested fix

  1. Add a CancellationTokenSource _consumerCts field
  2. Change while (true) to while (!_consumerCts.IsCancellationRequested)
  3. Use _buffer.GetConsumingEnumerable(_consumerCts.Token) or check the token in the loop body
  4. In Dispose(), cancel _consumerCts before disposing _buffer
  5. Optionally call _buffer.CompleteAdding() to signal the consumer that no more items will arrive

Files involved

  • Daqifi.Desktop/Loggers/DatabaseLogger.csConsumer() method and Dispose()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions