Skip to content

Commit

Permalink
Don't wait for runInTerminal response.
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimcn committed Jun 7, 2020
1 parent f9c57ee commit 4469ecc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 60 deletions.
2 changes: 1 addition & 1 deletion adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde_json = "1.0.40"
superslice = "1.0.0"

futures = "0.3.0"
tokio = { version = "0.2", features = ["rt-threaded", "rt-util", "stream", "macros", "io-util", "fs", "tcp", "time", "sync", "blocking"] }
tokio = { version = "0.2", features = ["rt-threaded", "rt-util", "stream", "macros", "io-util", "fs", "tcp", "dns", "time", "sync", "blocking"] }
tokio-util = { version = "0.3.0", features = ["codec"] }

lldb = { path = "deps/lldb" }
Expand Down
2 changes: 1 addition & 1 deletion adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub extern "C" fn entry(port: u16, multi_session: bool, adapter_params: Option<&
let mut rt = tokio::runtime::Builder::new() //
.threaded_scheduler()
.core_threads(2)
.enable_io()
.enable_all()
.build()
.unwrap();
rt.block_on(run_debug_server(addr, adapter_settings, multi_session));
Expand Down
80 changes: 22 additions & 58 deletions adapter/src/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use log::debug;
use std::io::{self, BufRead};
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::time::{Duration, Instant};

use crate::dap_session::DAPSession;
use crate::debug_protocol::*;
use crate::error::Error;

use std::time::Duration;
use tokio::io::BufReader;
use tokio::net::{TcpListener, TcpStream};
use tokio::prelude::*;

pub struct Terminal {
#[allow(unused)]
connection: TcpStream,
Expand Down Expand Up @@ -44,42 +43,30 @@ impl Terminal {
let executable = std::env::current_exe()?.to_str().unwrap().into();
let args = vec![executable, "terminal-agent".into(), format!("--port={}", addr.port())];
let req_args = RunInTerminalRequestArguments {
args: vec![reset_sequence.into()],
args: args,
cwd: String::new(),
env: None,
kind: Some(terminal_kind.clone()),
title: Some(title.clone()),
kind: Some(terminal_kind),
title: Some(title),
};
dap_session.send_request(RequestArguments::runInTerminal(req_args)).await?;
}
let _resp = dap_session.send_request(RequestArguments::runInTerminal(req_args));

let mut listener = TcpListener::bind("127.0.0.1:0")?;
let addr = listener.local_addr()?;
let (stream, _remote_addr) = listener.accept().await?;

// Run codelldb in a terminal agent mode, which sends back the tty device name (Unix)
// or its own process id (Windows), then waits till the socket gets closed from our end.
let executable = std::env::current_exe()?.to_str().unwrap().into();
let args = vec![executable, "terminal-agent".into(), format!("--port={}", addr.port())];
let req_args = RunInTerminalRequestArguments {
args: args,
cwd: String::new(),
env: None,
kind: Some(terminal_kind),
title: Some(title),
};
dap_session.send_request(RequestArguments::runInTerminal(req_args)).await?;

let stream = accept_with_timeout(&mut listener, Duration::from_millis(5000))?;
let stream2 = stream.try_clone()?;
let mut reader = BufReader::new(stream);
let mut data = String::new();
reader.read_line(&mut data).await?;

let mut reader = io::BufReader::new(stream);
let mut data = String::new();
reader.read_line(&mut data)?;
Ok(Terminal {
connection: reader.into_inner(),
data: data.trim().to_owned(),
})
};

Ok(Terminal {
connection: stream2,
data: data.trim().to_owned(),
})
match tokio::time::timeout(Duration::from_secs(5), terminal_fut).await {
Ok(res) => res,
Err(_) => bail!("Terminal agent did not respond within the allotted time."),
}
}

pub fn input_devname(&self) -> &str {
Expand Down Expand Up @@ -123,26 +110,3 @@ impl Terminal {
f()
}
}

// No set_accept_timeout() in std :(
fn accept_with_timeout(listener: &mut TcpListener, timeout: Duration) -> Result<TcpStream, Error> {
listener.set_nonblocking(true)?;
let started = Instant::now();
let stream = loop {
match listener.accept() {
Ok((stream, _addr)) => break stream,
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
bail!(e);
} else {
thread::sleep(Duration::from_millis(100));
}
}
}
if started.elapsed() > timeout {
bail!("Terminal agent did not respond within the allotted time.");
}
};
stream.set_nonblocking(false)?;
Ok(stream)
}

0 comments on commit 4469ecc

Please sign in to comment.