You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test: Fix test failures when session tracking is enabled (#1393)
* fix(logs): Fix test failures when session tracking is enabled
This commit fixes 3 test failures in test_logs.c that occur when
auto-session tracking is enabled (the default):
- basic_logging_functionality
- formatted_log_messages
- logs_disabled_by_default
Root causes and fixes:
1. validate_logs_envelope counted all envelopes but only validated logs
- Session envelopes from sentry_close() were triggering assertions
- Fixed by filtering: only count/validate log envelopes, skip others
2. formatted_log_messages didn't wait for batching thread to start
- Added sleep_ms(20) after sentry_init() to match other tests
3. batching_stop flag wasn't reset between sentry_init() calls
- Once set to 1 during shutdown, subsequent startups would fail
- Fixed by resetting to 0 in sentry__logs_startup()
These issues were discovered in console SDK testing where session
tracking is enabled by default and tests run sequentially in a single
process.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(logs): Eliminate thread startup race with enum state machine
This commit eliminates the thread startup race condition by:
1. Replacing batching_stop with enum-based thread_state
- SENTRY_LOGS_THREAD_STOPPED (0): Not running
- SENTRY_LOGS_THREAD_RUNNING (1): Thread active and processing logs
- Improves code clarity and makes thread lifecycle explicit
2. Thread signals RUNNING state after initialization
- Batching thread sets state to RUNNING after mutex setup
- Provides deterministic indication that thread is ready
3. Adding test-only helper: sentry__logs_wait_for_thread_startup()
- Polls thread_state until RUNNING (max 1 second)
- Zero production overhead (only compiled with SENTRY_UNITTEST)
- Tests explicitly wait for thread readiness instead of arbitrary sleeps
4. Updating shutdown to use atomic state transition
- Atomically transitions from RUNNING to STOPPED
- Detects double shutdown or never-started cases
- Returns early if thread wasn't running
Benefits:
- Eliminates race where logs could be enqueued before thread starts
- Tests are deterministic (no arbitrary timing assumptions)
- Code is clearer with explicit state names
- No production overhead (test helper is ifdef'd out)
The one remaining sleep in basic_logging_functionality test is intentional
- it tests batch timing behavior (wait for buffer flush).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(logs): Fix thread lifecycle race condition causing memory leaks
This commit fixes a race condition in the logs batching thread lifecycle
that caused valgrind to report 336 byte memory leaks in CI tests.
## Problem
When `sentry__logs_shutdown()` ran before the batching thread transitioned
from initial state to RUNNING, the two-state model couldn't distinguish
between "never started" and "starting but not yet running", causing
shutdown to skip joining the thread.
## Solution
1. Added three-state lifecycle enum:
- STOPPED (0): Thread never started or shut down
- STARTING (1): Thread spawned but not yet initialized
- RUNNING (2): Thread active and processing logs
2. Added `sentry__atomic_compare_swap()` primitive for atomic state
verification (cross-platform: Windows InterlockedCompareExchange,
POSIX __atomic_compare_exchange_n)
3. Startup sets state to STARTING before spawning thread
4. Thread uses CAS to verify STARTING → RUNNING transition
- If CAS fails (shutdown already set to STOPPED), exits cleanly
- Guarantees thread only runs if it successfully transitioned
5. Shutdown always joins thread if old state != STOPPED
## Benefits
- Eliminates race condition where shutdown could miss a spawned thread
- Thread explicitly verifies state transition with CAS
- No memory leaks in any shutdown scenario
- All 212 unit tests pass
- All log integration tests pass
Fixes test failures:
- test_before_send_log
- test_before_send_log_discard
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* docs(logs): Address code review comments with clarifying documentation
Addresses bot review feedback by adding documentation without changing behavior:
1. **CAS memory ordering**: Added comment explaining sequential consistency
usage for thread state transitions and why it's appropriate for
synchronization
2. **Condition variable cleanup**: Added note explaining that static storage
condition variables don't require explicit cleanup on POSIX and Windows
3. **CAS function documentation**: Enhanced docstring to document memory
ordering guarantees and note that ABA problem isn't a concern for simple
integer state machines
4. **Shutdown race handling**: Added comment explaining how the atomic CAS
in the thread prevents the race when shutdown occurs during STARTING state
All changes are documentation/comments only - no behavior changes.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Update src/sentry_logs.c
Co-authored-by: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com>
0 commit comments