Skip to content

Commit cd3c7d9

Browse files
committed
fix panic when agent is not configured #70
1 parent 17cba9b commit cd3c7d9

File tree

2 files changed

+7
-41
lines changed

2 files changed

+7
-41
lines changed

pyroscope_ffi/ffikit/src/lib.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bincode::{config, Decode, Encode};
22
use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
33
use lazy_static::lazy_static;
4-
use pyroscope::error::Result;
4+
use pyroscope::error::{Result, PyroscopeError};
55
use std::{
66
io::{BufReader, Read, Write},
77
sync::{
@@ -137,40 +137,17 @@ pub fn initialize_ffi() -> Result<Receiver<Signal>> {
137137
}
138138

139139
pub fn send(signal: Signal) -> Result<()> {
140-
// Check if SENDER is set.
141-
// Send signal through forked process.
142140
if get_parent_pid() != std::process::id() {
143141
let socket_address = format!("/tmp/PYROSCOPE-{}", get_parent_pid());
144-
145-
log::trace!(
146-
target: LOG_TAG,
147-
"Sending signal {:?} through socket {}",
148-
signal,
149-
&socket_address
150-
);
151-
152-
// Connect to the socket.
153142
let mut conn = LocalSocketStream::connect(socket_address)?;
154-
155-
// encode signal
156143
let buffer = bincode::encode_to_vec(&signal, config::standard()).unwrap();
157-
158-
// Write the message.
159144
conn.write_all(&buffer)?;
160-
161-
// Flush the connection.
162145
conn.flush()?;
163146
} else {
164-
// Send signal through parent process.
165147
if let Some(sender) = &*SENDER.lock()? {
166-
log::trace!(
167-
target: LOG_TAG,
168-
"Sending signal {:?} through FFI channel",
169-
signal
170-
);
171148
sender.send(signal)?;
172149
} else {
173-
log::error!(target: LOG_TAG, "FFI channel not initialized");
150+
return Err(PyroscopeError::new( "FFI channel not initialized"));
174151
}
175152
}
176153

pyroscope_ffi/python/lib/src/lib.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,7 @@ pub extern "C" fn initialize_agent(
151151

152152
#[no_mangle]
153153
pub extern "C" fn drop_agent() -> bool {
154-
// Send Kill signal to the FFI merge channel.
155-
ffikit::send(ffikit::Signal::Kill).unwrap();
156-
157-
true
154+
return ffikit::send(ffikit::Signal::Kill).is_ok();
158155
}
159156

160157
#[no_mangle]
@@ -170,9 +167,7 @@ pub extern "C" fn add_thread_tag(thread_id: u64, key: *const c_char, value: *con
170167
hasher.write_u64(thread_id % pid as u64);
171168
let id = hasher.finish();
172169

173-
ffikit::send(ffikit::Signal::AddThreadTag(id, key, value)).unwrap();
174-
175-
true
170+
return ffikit::send(ffikit::Signal::AddThreadTag(id, key, value)).is_ok();
176171
}
177172

178173
#[no_mangle]
@@ -190,9 +185,7 @@ pub extern "C" fn remove_thread_tag(
190185
hasher.write_u64(thread_id % pid as u64);
191186
let id = hasher.finish();
192187

193-
ffikit::send(ffikit::Signal::RemoveThreadTag(id, key, value)).unwrap();
194-
195-
true
188+
return ffikit::send(ffikit::Signal::RemoveThreadTag(id, key, value)).is_ok();
196189
}
197190

198191
#[no_mangle]
@@ -203,9 +196,7 @@ pub extern "C" fn add_global_tag(key: *const c_char, value: *const c_char) -> bo
203196
.unwrap()
204197
.to_owned();
205198

206-
ffikit::send(ffikit::Signal::AddGlobalTag(key, value)).unwrap();
207-
208-
true
199+
return ffikit::send(ffikit::Signal::AddGlobalTag(key, value)).is_ok();
209200
}
210201

211202
#[no_mangle]
@@ -216,9 +207,7 @@ pub extern "C" fn remove_global_tag(key: *const c_char, value: *const c_char) ->
216207
.unwrap()
217208
.to_owned();
218209

219-
ffikit::send(ffikit::Signal::RemoveGlobalTag(key, value)).unwrap();
220-
221-
true
210+
return ffikit::send(ffikit::Signal::RemoveGlobalTag(key, value)).is_ok();
222211
}
223212

224213
// Convert a string of tags to a Vec<(&str, &str)>

0 commit comments

Comments
 (0)