extra check on local socket

This commit is contained in:
Dongdong Zhou 2022-09-05 20:11:19 +01:00
parent 8471a25403
commit bf5a98a6d4
1 changed files with 24 additions and 3 deletions

View File

@ -2,7 +2,7 @@
cell::RefCell, cell::RefCell,
cmp::Ordering, cmp::Ordering,
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
io::BufReader, io::{BufReader, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
rc::Rc, rc::Rc,
sync::Arc, sync::Arc,
@ -297,11 +297,11 @@ fn listen_local_socket(event_sink: ExtEventSink) -> Result<()> {
let socket = let socket =
interprocess::local_socket::LocalSocketListener::bind(local_socket)?; interprocess::local_socket::LocalSocketListener::bind(local_socket)?;
for stream in socket.incoming().flatten() { for mut stream in socket.incoming().flatten() {
let mut reader = BufReader::new(stream);
let event_sink = event_sink.clone(); let event_sink = event_sink.clone();
thread::spawn(move || -> Result<()> { thread::spawn(move || -> Result<()> {
loop { loop {
let mut reader = BufReader::new(stream);
let msg: RpcMessage< let msg: RpcMessage<
CoreRequest, CoreRequest,
CoreNotification, CoreNotification,
@ -330,6 +330,10 @@ fn listen_local_socket(event_sink: ExtEventSink) -> Result<()> {
Target::Global, Target::Global,
); );
} }
stream = reader.into_inner();
let _ = stream.write_all(b"received");
let _ = stream.flush();
} }
}); });
} }
@ -352,6 +356,23 @@ pub fn check_local_socket(paths: Vec<PathBuf>) -> Result<()> {
files, files,
}); });
lapce_rpc::stdio::write_msg(&mut socket, msg)?; lapce_rpc::stdio::write_msg(&mut socket, msg)?;
let (tx, rx) = crossbeam_channel::bounded(1);
thread::spawn(move || {
let mut buf = [0; 100];
let received = if let Ok(n) = socket.read(&mut buf) {
&buf[..n] == b"received"
} else {
false
};
tx.send(received)
});
let received = rx.recv_timeout(std::time::Duration::from_millis(500))?;
if !received {
return Err(anyhow!("didn't receive response"));
}
Ok(()) Ok(())
} }
} }