Skip to content

Commit 6bfb54b

Browse files
authored
fix: Improve transport synchronization and consume response (#260)
1 parent 5eb00f6 commit 6bfb54b

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

sentry-core/src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ impl Client {
279279
/// If no timeout is provided the client will wait for as long a
280280
/// `shutdown_timeout` in the client options.
281281
pub fn close(&self, timeout: Option<Duration>) -> bool {
282-
if let Some(transport) = self.transport.write().unwrap().take() {
282+
let transport_opt = self.transport.write().unwrap().take();
283+
if let Some(transport) = transport_opt {
283284
sentry_debug!("client close; request transport to shut down");
284285
transport.shutdown(timeout.unwrap_or(self.options.shutdown_timeout))
285286
} else {

sentry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ sentry-log = { version = "0.20.0", path = "../sentry-log", optional = true }
7272
sentry-panic = { version = "0.20.0", path = "../sentry-panic", optional = true }
7373
sentry-slog = { version = "0.20.0", path = "../sentry-slog", optional = true }
7474
log_ = { package = "log", version = "0.4.8", optional = true, features = ["std"] }
75-
reqwest_ = { package = "reqwest", version = "0.10.1", optional = true, features = ["blocking", "json"], default-features = false }
75+
reqwest_ = { package = "reqwest", version = "0.10.8", optional = true, features = ["blocking", "json"], default-features = false }
7676
curl_ = { package = "curl", version = "0.4.25", optional = true }
7777
surf_ = { package = "surf", version = "=2.0.0-alpha.4", optional = true }
7878
http-client = { version = "3.0", optional = true }

sentry/src/transport.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ macro_rules! implement_http_transport {
7878
shutdown_signal: Arc<Condvar>,
7979
shutdown_immediately: Arc<AtomicBool>,
8080
queue_size: Arc<Mutex<usize>>,
81-
_handle: Option<JoinHandle<()>>,
81+
handle: Option<JoinHandle<()>>,
8282
}
8383

8484
impl $typename {
@@ -104,7 +104,7 @@ macro_rules! implement_http_transport {
104104
#[allow(clippy::mutex_atomic)]
105105
let queue_size = Arc::new(Mutex::new(0));
106106
let http_client = http_client(options, $hc_client);
107-
let _handle = Some(spawn(
107+
let handle = Some(spawn(
108108
options,
109109
receiver,
110110
shutdown_signal.clone(),
@@ -117,7 +117,7 @@ macro_rules! implement_http_transport {
117117
shutdown_signal,
118118
shutdown_immediately,
119119
queue_size,
120-
_handle,
120+
handle,
121121
}
122122
}
123123
}
@@ -135,14 +135,18 @@ macro_rules! implement_http_transport {
135135

136136
fn shutdown(&self, timeout: Duration) -> bool {
137137
sentry_debug!("shutting down http transport");
138-
let guard = self.queue_size.lock().unwrap();
139-
if *guard == 0 {
138+
if *self.queue_size.lock().unwrap() == 0 {
140139
true
141140
} else {
142141
if let Ok(sender) = self.sender.lock() {
143142
sender.send(None).ok();
144143
}
145-
self.shutdown_signal.wait_timeout(guard, timeout).is_ok()
144+
let guard = self.queue_size.lock().unwrap();
145+
if *guard > 0 {
146+
self.shutdown_signal.wait_timeout(guard, timeout).is_ok()
147+
} else {
148+
true
149+
}
146150
}
147151
}
148152
}
@@ -152,7 +156,11 @@ macro_rules! implement_http_transport {
152156
sentry_debug!("dropping http transport");
153157
self.shutdown_immediately.store(true, Ordering::SeqCst);
154158
if let Ok(sender) = self.sender.lock() {
155-
sender.send(None).ok();
159+
if sender.send(None).is_ok() {
160+
if let Some(handle) = self.handle.take() {
161+
handle.join().ok();
162+
}
163+
}
156164
}
157165
}
158166
}
@@ -225,6 +233,7 @@ implement_http_transport! {
225233
let mut body = Vec::new();
226234
envelope.to_writer(&mut body).unwrap();
227235

236+
sentry_debug!("Sending envelope");
228237
match http_client
229238
.post(url.as_str())
230239
.body(body)
@@ -242,9 +251,13 @@ implement_http_transport! {
242251
disabled = Some(retry_after);
243252
}
244253
}
254+
match resp.text() {
255+
Err(err) => { sentry_debug!("Failed to read sentry response: {}", err); },
256+
Ok(text) => { sentry_debug!("Get response: `{}`", text); },
257+
}
245258
}
246259
Err(err) => {
247-
sentry_debug!("Failed to send event: {}", err);
260+
sentry_debug!("Failed to send envelope: {}", err);
248261
}
249262
}
250263

0 commit comments

Comments
 (0)