fix(ddtrace/tracer): resolve race conditions in write pipeline#3821
Merged
dd-mergequeue[bot] merged 7 commits intoAug 27, 2025
Merged
Conversation
darccio
reviewed
Jul 31, 2025
kakkoyun
marked this pull request as draft
July 31, 2025 10:50
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
from
July 31, 2025 16:45
ed249b2 to
7c27333
Compare
kakkoyun
marked this pull request as ready for review
July 31, 2025 16:50
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
from
July 31, 2025 17:11
7c27333 to
f021440
Compare
darccio
reviewed
Aug 1, 2025
kakkoyun
marked this pull request as draft
August 5, 2025 09:12
kakkoyun
marked this pull request as ready for review
August 5, 2025 14:47
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
from
August 5, 2025 15:21
727f1f3 to
1cd4101
Compare
darccio
requested changes
Aug 7, 2025
kakkoyun
marked this pull request as draft
August 26, 2025 16:47
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
2 times, most recently
from
August 26, 2025 17:03
9bf4c8c to
aa94938
Compare
darccio
reviewed
Aug 27, 2025
Add TestAgentWriterRaceCondition and TestAgentWriterTraceCountAccuracy to reproduce and validate race conditions between add() and flush() operations. These tests demonstrate that concurrent access to payload buffers can cause: - Data races in bytes.Buffer operations - Buffer corruption leading to slice bounds panics - Incorrect trace counting due to counter races The tests consistently fail with -race detector on unfixed code, providing a reliable way to validate the race condition and verify fixes. Signed-off-by: Kemal Akkoyun <[email protected]>
Fix race condition between add() and flush() operations by implementing mutex-based synchronization for payload access. **Problem**: Multiple goroutines could access the same payload buffer concurrently, causing: - Data races in bytes.Buffer operations - Buffer corruption and panics (slice bounds out of range) - Lost or corrupted traces **Solution**: - Add mutex (mu) to agentTraceWriter struct - Protect add() operations with mutex to prevent concurrent payload access - Protect payload swap in flush() to ensure atomic replacement **Verification**: - Race condition tests now pass consistently with no race detector warnings - No panics or data corruption - All existing functionality preserved Signed-off-by: Kemal Akkoyun <[email protected]>
…fe interface - Add private payload interfaces (payloadWriter, payloadReader, payload) - Implement safePayload wrapper with RWMutex for thread-safe operations - Rename payload to unsafePayload, replace with safe interface - Optimize itemCount() using direct atomic access - Rename incrementCount() to recordItem() for clarity - Add comprehensive concurrent access tests - All payload operations now race-condition free Signed-off-by: Kemal Akkoyun <[email protected]>
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
from
August 27, 2025 11:39
aa94938 to
ccc2a6b
Compare
kakkoyun
marked this pull request as ready for review
August 27, 2025 14:31
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
from
August 27, 2025 14:35
2db4b4f to
d2ba67f
Compare
Signed-off-by: Kemal Akkoyun <[email protected]>
Signed-off-by: Kemal Akkoyun <[email protected]>
…tion optimizations Adds missing benchmarks for payload, writer, and transport operations to support data-driven performance optimization. Benchmark results (Darwin 24.6.0 arm64, go1.25.0): Performance comparison (main vs optimized branch): - stats() calls: 15x slower per call (0.25ns → 3.9ns) - Lock frequency: 40-60% reduction (5+ → 2-3 per flush) - Payload throughput: No regression (~365ns vs ~370ns) - Net effect: Lower lock contention under high concurrency ✅ Memory optimization experiments (all reverted after benchmarking): - Header pools: 8% performance regression, no allocation benefit - Buffer pre-allocation: 62% slower, 3x memory usage - Smart buffer growth: 43% slower, 2x memory usage - Reader pools: Overhead exceeds benefit
Signed-off-by: Kemal Akkoyun <[email protected]>
kakkoyun
force-pushed
the
kakkoyun/fix_race_writer_payload
branch
from
August 27, 2025 14:43
d2ba67f to
fd699fe
Compare
darccio
approved these changes
Aug 27, 2025
Member
Author
|
/merge |
|
View all feedbacks in Devflow UI.
This merge request is not mergeable yet, because of pending checks/missing approvals. It will be added to the queue as soon as checks pass and/or get approvals.
The expected merge time in
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Eliminates race conditions in the tracer's write pipeline by implementing comprehensive thread-safe interfaces and fixing a critical deadlock bug that was introduced during the initial race condition fix.
Motivation
During analysis of the tracer's writer implementation, multiple race conditions were discovered that could cause serious issues under high concurrency:
Original Race Conditions:
agentTraceWriterbetweenadd()andflush()operationsbytes.Bufferoperations detected by Go's race detectorslice bounds out of rangeSolution
Phase 1: Fix Writer Race Conditions
mu sync.MutextoagentTraceWriterstructadd()operations with mutex to prevent concurrent payload accessflush()to ensure atomic replacementPhase 2: Comprehensive Payload Interface Redesign
payloadWriter,payloadReader,payload)safePayloadwrapper withRWMutexfor thread-safe operationspayloadtounsafePayload, replaced with safe interface throughout codebaseitemCount()using direct atomic access for performanceincrementCount()torecordItem()for clarityKey Fixes
agentTraceWriter.add()Files Changed
Core Writer Components:
ddtrace/tracer/writer.go- Fixed deadlock, added mutex synchronizationddtrace/tracer/payload.go- Thread-safe interface redesignddtrace/tracer/transport.go- Updated to use safe payload interfaceCI Visibility Components:
ddtrace/tracer/civisibility_*.go- Updated for thread-safe payload usageTesting & Configuration:
ddtrace/tracer/*_test.go- Added comprehensive race condition testsddtrace/tracer/option.go- Updated payload initializationTesting
** Race Condition Tests:**
TestAgentWriterRaceCondition: Spawns multiple goroutines doing concurrent add/flush operationsTestAgentWriterTraceCountAccuracy: Validates trace counting accuracy under concurrencyTestWorker: Now passes (was timing out due to deadlock)Verification:
Backward Compatibility
Reviewer's Checklist
./scripts/lint.shlocally.