Skip to content

fix: check the pthread return values in builds that ship - #369

Merged
deemar merged 1 commit into
HardySimpson:masterfrom
yegorich:fix/consumer-pthread-checks
Oct 8, 2026
Merged

deemar merged 1 commit into
HardySimpson:masterfrom
yegorich:fix/consumer-pthread-checks

Conversation

@yegorich

Copy link
Copy Markdown
Contributor

consumer.c checked them with assert(), and NDEBUG is defined in Release, RelWithDebInfo and MinSizeRel -- the builds people actually install. So the return values were examined in development and nowhere else, which is the part of #357 that holds up.

ZC_PTHREAD_CHECK() reports through zc_error() instead, in every build. It covers the ten assert sites and eight calls that were not checked at all: the lock, wait and unlock around the consumer's wakeup condition, the pair around the flush wait, and both ends of log_consumer_queue_reserve().

Reporting is the honest maximum. On a correctly initialised normal mutex these fail with EINVAL or EDEADLK, which means corrupted memory or a programming error; there is no state a logging library can restore, so it says what happened and leaves the application running rather than pretending to recover.

The cost is a branch on a path already paying for a mutex, and the strings prove the checks are there:

$ strings consumer.c.o | grep 'pthread.*fail'
pthread_mutex_lock(&logc->event.siglock) fail, rc[%d]
...

from the -DNDEBUG build. 38/38 tests pass in Debug, 36/36 in Release, and the consumer and multithread tests are clean under ThreadSanitizer.

consumer.c checked them with assert(), and NDEBUG is defined in Release,
RelWithDebInfo and MinSizeRel -- the builds people actually install. So
the return values were examined in development and nowhere else, which is
the part of HardySimpson#357 that holds up.

ZC_PTHREAD_CHECK() reports through zc_error() instead, in every build. It
covers the ten assert sites and eight calls that were not checked at all:
the lock, wait and unlock around the consumer's wakeup condition, the pair
around the flush wait, and both ends of log_consumer_queue_reserve().

Reporting is the honest maximum. On a correctly initialised normal mutex
these fail with EINVAL or EDEADLK, which means corrupted memory or a
programming error; there is no state a logging library can restore, so it
says what happened and leaves the application running rather than
pretending to recover.

The cost is a branch on a path already paying for a mutex, and the strings
prove the checks are there:

    $ strings consumer.c.o | grep 'pthread.*fail'
    pthread_mutex_lock(&logc->event.siglock) fail, rc[%d]
    ...

from the -DNDEBUG build. 38/38 tests pass in Debug, 36/36 in Release, and
the consumer and multithread tests are clean under ThreadSanitizer.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@deemar
deemar merged commit 1c74813 into HardySimpson:master Oct 8, 2026
32 checks passed
@yegorich
yegorich deleted the fix/consumer-pthread-checks branch October 8, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants