Skip to content

Commit 7713549

Browse files
authored
fix: defensive reads for all thread locals (#161)
* fix: defensive reads for all thread locals Signed-off-by: Zhenchi <[email protected]> * fix: clippy Signed-off-by: Zhenchi <[email protected]> * address comment Signed-off-by: Zhenchi <[email protected]> * address comment Signed-off-by: Zhenchi <[email protected]> * address comment Signed-off-by: Zhenchi <[email protected]> --------- Signed-off-by: Zhenchi <[email protected]>
1 parent 88c6033 commit 7713549

File tree

9 files changed

+46
-29
lines changed

9 files changed

+46
-29
lines changed

minitrace/src/collector/global_collector.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ fn register_receiver(rx: Receiver<CollectCommand>) {
5050
}
5151

5252
fn send_command(cmd: CollectCommand) {
53-
COMMAND_SENDER.with(|sender| sender.send(cmd).ok());
53+
COMMAND_SENDER.try_with(|sender| sender.send(cmd).ok()).ok();
5454
}
5555

5656
fn force_send_command(cmd: CollectCommand) {
57-
COMMAND_SENDER.with(|sender| sender.force_send(cmd));
57+
COMMAND_SENDER
58+
.try_with(|sender| sender.force_send(cmd))
59+
.ok();
5860
}
5961

6062
/// Sets the reporter and its configuration for the current application.
@@ -432,7 +434,7 @@ fn amend_local_span(
432434
timestamp_unix_ns: begin_time_unix_ns,
433435
properties: span.properties.clone(),
434436
};
435-
events.entry(parent_id).or_insert(vec![]).push(event);
437+
events.entry(parent_id).or_default().push(event);
436438
continue;
437439
}
438440

@@ -470,7 +472,7 @@ fn amend_span(
470472
timestamp_unix_ns: begin_time_unix_ns,
471473
properties: raw_span.properties.clone(),
472474
};
473-
events.entry(parent_id).or_insert(vec![]).push(event);
475+
events.entry(parent_id).or_default().push(event);
474476
return;
475477
}
476478

minitrace/src/collector/id.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ impl SpanId {
1414
#[inline]
1515
/// Create a non-zero `SpanId`
1616
pub(crate) fn next_id() -> SpanId {
17-
LOCAL_ID_GENERATOR.with(|g| {
18-
let (prefix, mut suffix) = g.get();
17+
LOCAL_ID_GENERATOR
18+
.try_with(|g| {
19+
let (prefix, mut suffix) = g.get();
1920

20-
suffix = suffix.wrapping_add(1);
21+
suffix = suffix.wrapping_add(1);
2122

22-
g.set((prefix, suffix));
23+
g.set((prefix, suffix));
2324

24-
SpanId(((prefix as u64) << 32) | (suffix as u64))
25-
})
25+
SpanId(((prefix as u64) << 32) | (suffix as u64))
26+
})
27+
.unwrap_or_else(|_| SpanId(rand::random()))
2628
}
2729
}
2830

minitrace/src/collector/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ impl SpanContext {
167167

168168
#[cfg(feature = "enable")]
169169
{
170-
let stack = LOCAL_SPAN_STACK.with(Rc::clone);
170+
let stack = LOCAL_SPAN_STACK.try_with(Rc::clone).ok()?;
171+
171172
let mut stack = stack.borrow_mut();
172173
let collect_token = stack.current_collect_token()?[0];
173174

minitrace/src/event.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use std::borrow::Cow;
4-
use std::rc::Rc;
54

65
use crate::local::local_span_stack::LOCAL_SPAN_STACK;
76
use crate::Span;
@@ -55,9 +54,9 @@ impl Event {
5554
{
5655
#[cfg(feature = "enable")]
5756
{
58-
let stack = LOCAL_SPAN_STACK.with(Rc::clone);
59-
let mut stack = stack.borrow_mut();
60-
stack.add_event(name, properties);
57+
LOCAL_SPAN_STACK
58+
.try_with(|stack| stack.borrow_mut().add_event(name, properties))
59+
.ok();
6160
}
6261
}
6362
}

minitrace/src/local/local_collector.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::util::RawSpans;
4040
/// [`Span`]: crate::Span
4141
/// [`LocalSpan`]: crate::local::LocalSpan
4242
#[must_use]
43+
#[derive(Default)]
4344
pub struct LocalCollector {
4445
#[cfg(feature = "enable")]
4546
inner: Option<LocalCollectorInner>,
@@ -98,13 +99,14 @@ impl LocalCollector {
9899
pub fn start() -> Self {
99100
#[cfg(not(feature = "enable"))]
100101
{
101-
LocalCollector {}
102+
LocalCollector::default()
102103
}
103104

104105
#[cfg(feature = "enable")]
105106
{
106-
let stack = LOCAL_SPAN_STACK.with(Rc::clone);
107-
Self::new(None, stack)
107+
LOCAL_SPAN_STACK
108+
.try_with(|stack| Self::new(None, stack.clone()))
109+
.unwrap_or_default()
108110
}
109111
}
110112

minitrace/src/local/local_span.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::local::local_span_stack::LOCAL_SPAN_STACK;
1212
///
1313
/// [`Span`]: crate::Span
1414
#[must_use]
15+
#[derive(Default)]
1516
pub struct LocalSpan {
1617
#[cfg(feature = "enable")]
1718
inner: Option<LocalSpanInner>,
@@ -42,13 +43,14 @@ impl LocalSpan {
4243
pub fn enter_with_local_parent(name: &'static str) -> Self {
4344
#[cfg(not(feature = "enable"))]
4445
{
45-
LocalSpan {}
46+
LocalSpan::default()
4647
}
4748

4849
#[cfg(feature = "enable")]
4950
{
50-
let stack = LOCAL_SPAN_STACK.with(Rc::clone);
51-
Self::enter_with_stack(name, stack)
51+
LOCAL_SPAN_STACK
52+
.try_with(|stack| Self::enter_with_stack(name, stack.clone()))
53+
.unwrap_or_default()
5254
}
5355
}
5456

minitrace/src/local/span_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl SpanQueue {
3535

3636
let span = RawSpan::begin_with(
3737
SpanId::next_id(),
38-
self.next_parent_id.unwrap_or(SpanId::default()),
38+
self.next_parent_id.unwrap_or_default(),
3939
Instant::now(),
4040
name,
4141
false,
@@ -74,7 +74,7 @@ impl SpanQueue {
7474

7575
let mut span = RawSpan::begin_with(
7676
SpanId::next_id(),
77-
self.next_parent_id.unwrap_or(SpanId::default()),
77+
self.next_parent_id.unwrap_or_default(),
7878
Instant::now(),
7979
name,
8080
true,

minitrace/src/span.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::util::CollectToken;
2323

2424
/// A thread-safe span.
2525
#[must_use]
26+
#[derive(Default)]
2627
pub struct Span {
2728
#[cfg(feature = "enable")]
2829
pub(crate) inner: Option<SpanInner>,
@@ -196,9 +197,11 @@ impl Span {
196197
{
197198
#[cfg(not(test))]
198199
let collect = GlobalCollect;
199-
LOCAL_SPAN_STACK.with(move |stack| {
200-
Self::enter_with_stack(name, &mut (*stack).borrow_mut(), collect)
201-
})
200+
LOCAL_SPAN_STACK
201+
.try_with(move |stack| {
202+
Self::enter_with_stack(name, &mut (*stack).borrow_mut(), collect)
203+
})
204+
.unwrap_or_default()
202205
}
203206
}
204207

@@ -231,7 +234,9 @@ impl Span {
231234

232235
#[cfg(feature = "enable")]
233236
{
234-
LOCAL_SPAN_STACK.with(|s| self.attach_into_stack(s))
237+
LOCAL_SPAN_STACK
238+
.try_with(|s| self.attach_into_stack(s))
239+
.unwrap_or_default()
235240
}
236241
}
237242

minitrace/src/util/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ pub type CollectToken = Reusable<'static, Vec<CollectTokenItem>>;
3030

3131
impl Default for RawSpans {
3232
fn default() -> Self {
33-
RAW_SPANS_PULLER.with(|puller| puller.borrow_mut().pull())
33+
RAW_SPANS_PULLER
34+
.try_with(|puller| puller.borrow_mut().pull())
35+
.unwrap_or_else(|_| Reusable::new(&*RAW_SPANS_POOL, vec![]))
3436
}
3537
}
3638

3739
fn new_collect_token(items: impl IntoIterator<Item = CollectTokenItem>) -> CollectToken {
38-
let mut token = COLLECT_TOKEN_ITEMS_PULLER.with(|puller| puller.borrow_mut().pull());
40+
let mut token = COLLECT_TOKEN_ITEMS_PULLER
41+
.try_with(|puller| puller.borrow_mut().pull())
42+
.unwrap_or_else(|_| Reusable::new(&*COLLECT_TOKEN_ITEMS_POOL, vec![]));
3943
token.extend(items);
4044
token
4145
}

0 commit comments

Comments
 (0)