Run cancellable pdfTeX matching in native frontends
This commit is contained in:
1 parent
091458d329
commit
6e573dc1ce
21 files changed
+665
-9
No files matched your search
@@ -0,0 +1,143 @@
|
||||
use std::{
|
||||
ffi::OsStr,
|
||||
io::{self, BufRead, BufReader, Write},
|
||||
process::{Child, Command, Stdio},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
mpsc::{self, Receiver, Sender},
|
||||
},
|
||||
thread::{self, JoinHandle},
|
||||
time::Duration,
|
||||
};
|
||||
use texrra_tex_core::execution::{Cancellation, RegexOutcome, RegexRequest, match_pdf_regex};
|
||||
|
||||
const WORKER_ARGUMENT: &str = "--internal-pdf-regex-worker";
|
||||
static WORKER_ENABLED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
pub(crate) fn dispatch() -> Option<i32> {
|
||||
let mut args = std::env::args_os().skip(1);
|
||||
if args.next().as_deref() != Some(OsStr::new(WORKER_ARGUMENT)) {
|
||||
WORKER_ENABLED.store(true, Ordering::Relaxed);
|
||||
return None;
|
||||
}
|
||||
Some(match serve() {
|
||||
Ok(()) => 0,
|
||||
Err(error) => {
|
||||
eprintln!("pdfTeX regex worker: {error}");
|
||||
1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn enabled() -> bool {
|
||||
WORKER_ENABLED.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn serve() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut input = io::stdin().lock();
|
||||
let mut output = io::stdout().lock();
|
||||
let mut request = Vec::new();
|
||||
loop {
|
||||
request.clear();
|
||||
if input.read_until(b'\n', &mut request)? == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let request: RegexRequest = serde_json::from_slice(&request)?;
|
||||
serde_json::to_writer(&mut output, &match_pdf_regex(&request))?;
|
||||
output.write_all(b"\n")?;
|
||||
output.flush()?;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Worker {
|
||||
child: Child,
|
||||
requests: Option<Sender<Vec<u8>>>,
|
||||
answers: Receiver<Result<RegexOutcome, String>>,
|
||||
pipe: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
pub(crate) fn start() -> Result<Self, String> {
|
||||
// Re-execute the current image, even if an installer has replaced its
|
||||
// original path since the process opened the project.
|
||||
let mut child = Command::new("/proc/self/exe")
|
||||
.arg(WORKER_ARGUMENT)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.map_err(|e| format!("pdfTeX regex worker launch: {e}"))?;
|
||||
let mut input = child.stdin.take().expect("worker stdin is piped");
|
||||
let mut output = BufReader::new(child.stdout.take().expect("worker stdout is piped"));
|
||||
let (requests, waiting) = mpsc::channel::<Vec<u8>>();
|
||||
let (answer, answers) = mpsc::sync_channel(1);
|
||||
let pipe = thread::spawn(move || {
|
||||
let mut response = Vec::new();
|
||||
for request in waiting {
|
||||
let result = (|| {
|
||||
input.write_all(&request).map_err(|e| e.to_string())?;
|
||||
input.write_all(b"\n").map_err(|e| e.to_string())?;
|
||||
response.clear();
|
||||
if output
|
||||
.read_until(b'\n', &mut response)
|
||||
.map_err(|e| e.to_string())?
|
||||
== 0
|
||||
{
|
||||
return Err("pdfTeX regex worker closed its response pipe".into());
|
||||
}
|
||||
serde_json::from_slice::<Result<RegexOutcome, String>>(&response)
|
||||
.map_err(|e| format!("pdfTeX regex response: {e}"))?
|
||||
})();
|
||||
if answer.send(result).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(Self {
|
||||
child,
|
||||
requests: Some(requests),
|
||||
answers,
|
||||
pipe: Some(pipe),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn request(
|
||||
&mut self,
|
||||
request: &RegexRequest,
|
||||
cancellation: Option<&Cancellation>,
|
||||
) -> Result<RegexOutcome, String> {
|
||||
let payload = serde_json::to_vec(request).map_err(|e| e.to_string())?;
|
||||
self.requests
|
||||
.as_ref()
|
||||
.ok_or("pdfTeX regex worker has stopped")?
|
||||
.send(payload)
|
||||
.map_err(|_| "pdfTeX regex worker has stopped")?;
|
||||
loop {
|
||||
if cancellation.is_some_and(Cancellation::is_cancelled) {
|
||||
self.stop();
|
||||
return Err("native compilation cancelled".into());
|
||||
}
|
||||
match self.answers.recv_timeout(Duration::from_millis(2)) {
|
||||
Ok(result) => return result,
|
||||
Err(mpsc::RecvTimeoutError::Timeout) => {}
|
||||
Err(mpsc::RecvTimeoutError::Disconnected) => {
|
||||
return Err("pdfTeX regex worker lost its response pipe".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stop(&mut self) {
|
||||
self.requests.take();
|
||||
if let Some(pipe) = self.pipe.take() {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
let _ = pipe.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Worker {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user