{"record":{"id":"22da04737ebded7e","repo":"vectordotdev/vector","slug":"double-chunk-size-events-initialization","errorCode":null,"errorMessage":"double chunk_size_events initialization","messagePattern":"double chunk_size_events initialization","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/source_sender/mod.rs","lineNumber":43,"sourceCode":"\nstatic CHUNK_SIZE_EVENTS: AtomicUsize = AtomicUsize::new(0);\n\n/// Returns the configured source sender chunk size in events, or [`DEFAULT_CHUNK_SIZE_EVENTS`] if\n/// unset.\n#[must_use]\npub fn chunk_size_events() -> usize {\n    match CHUNK_SIZE_EVENTS.load(Ordering::Relaxed) {\n        0 => DEFAULT_CHUNK_SIZE_EVENTS,\n        size => size,\n    }\n}\n\n/// Sets the process-wide source sender chunk size in events. Must be called at most once, before\n/// the topology is built. Panics if called more than once.\npub fn set_chunk_size_events(size: usize) {\n    CHUNK_SIZE_EVENTS\n        .compare_exchange(0, size, Ordering::Acquire, Ordering::Relaxed)\n        .unwrap_or_else(|_| panic!(\"double chunk_size_events initialization\"));\n}\n\n#[cfg(any(test, feature = \"test\"))]\nconst TEST_BUFFER_SIZE: usize = 100;\n\nuse vector_common::internal_event::HistogramName;\n\nconst LAG_TIME_NAME: HistogramName = HistogramName::SourceLagTimeSeconds;\nconst SEND_LATENCY_NAME: HistogramName = HistogramName::SourceSendLatencySeconds;\nconst SEND_BATCH_LATENCY_NAME: HistogramName = HistogramName::SourceSendBatchLatencySeconds;\n\n/// A post-processing step applied to every event that flows through a [`SourceSender`].\n///\n/// Implement this trait to mutate events just before they are placed on the output channel.\n/// Because each method receives a typed reference, it is impossible at the type level to\n/// accidentally change an event's variant.\n///\n/// It is applied *globally* — to all outputs (default and named ports) produced by the same","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/vectordotdev/vector/blob/3708c39b12a93212ed8b8d7510b4cc7769cb5864/lib/vector-core/src/source_sender/mod.rs#L25-L61","documentation":"The source-sender chunk size is a process-wide setting stored in a static AtomicUsize. set_chunk_size_events() installs it with compare_exchange(0, size): the first call flips the global from 0 to the configured value; any later call finds a non-zero current value and panics with 'double chunk_size_events initialization'. The setting must be applied exactly once, before the topology is built, because SourceSender buffers are sized from it (threads * chunk_size_events).","triggerScenarios":"Calling set_chunk_size_events twice in one process - e.g. multiple test binaries/cases sharing a process, or embedding code building two topologies with different chunk sizes. In the Vector CLI it is set once from the --chunk-size-events flag in app.rs, so end users normally cannot trigger it.","commonSituations":"Integration test suites where each test calls set_chunk_size_events in setup; library consumers that build/teardown pipelines in a loop; passing --chunk-size-events while a wrapper already initialized it.","solutions":["Call set_chunk_size_events exactly once at process start, before any topology is built","In test suites, set it in a shared once-per-process hook (or not at all, relying on DEFAULT_CHUNK_SIZE_EVENTS) instead of per-test","Use a std::sync::Once / OnceLock guard around the call so accidental double invocation is a no-op rather than a panic"],"exampleFix":"// before\nset_chunk_size_events(size); // second call panics\n\n// after\nstatic INIT: std::sync::Once = std::sync::Once::new();\nINIT.call_once(|| set_chunk_size_events(size));","handlingStrategy":"validation","validationCode":"static CHUNK_INIT: std::sync::Once = std::sync::Once::new();\nCHUNK_INIT.call_once(|| set_chunk_size_events(size)); // idempotent across calls","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set the chunk size exactly once, at process start, before building any topology","Keep the call in one shared init function; do not sprinkle it into per-test setup","Run isolated cases in separate processes when different chunk sizes are required"],"tags":["rust","vector","global-state","initialization","panic"],"backgroundTag":"duplicate-global-initialization","analyzedSha":"3708c39b12a93212ed8b8d7510b4cc7769cb5864","analyzedAt":"2026-08-20T07:02:18.786Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}