diff options
Diffstat (limited to 'crates/proc_macro_api')
-rw-r--r-- | crates/proc_macro_api/src/process.rs | 61 |
1 files changed, 20 insertions, 41 deletions
diff --git a/crates/proc_macro_api/src/process.rs b/crates/proc_macro_api/src/process.rs index 907cb3db7..301888a0e 100644 --- a/crates/proc_macro_api/src/process.rs +++ b/crates/proc_macro_api/src/process.rs | |||
@@ -19,7 +19,7 @@ use crate::{ | |||
19 | 19 | ||
20 | #[derive(Debug, Default)] | 20 | #[derive(Debug, Default)] |
21 | pub(crate) struct ProcMacroProcessSrv { | 21 | pub(crate) struct ProcMacroProcessSrv { |
22 | inner: Option<Weak<Sender<Task>>>, | 22 | inner: Weak<Sender<Task>>, |
23 | } | 23 | } |
24 | 24 | ||
25 | #[derive(Debug)] | 25 | #[derive(Debug)] |
@@ -42,7 +42,7 @@ impl ProcMacroProcessSrv { | |||
42 | }); | 42 | }); |
43 | 43 | ||
44 | let task_tx = Arc::new(task_tx); | 44 | let task_tx = Arc::new(task_tx); |
45 | let srv = ProcMacroProcessSrv { inner: Some(Arc::downgrade(&task_tx)) }; | 45 | let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) }; |
46 | let thread = ProcMacroProcessThread { handle, sender: task_tx }; | 46 | let thread = ProcMacroProcessThread { handle, sender: task_tx }; |
47 | 47 | ||
48 | Ok((thread, srv)) | 48 | Ok((thread, srv)) |
@@ -79,22 +79,18 @@ impl ProcMacroProcessSrv { | |||
79 | where | 79 | where |
80 | R: TryFrom<Response, Error = &'static str>, | 80 | R: TryFrom<Response, Error = &'static str>, |
81 | { | 81 | { |
82 | let sender = match &self.inner { | ||
83 | None => return Err(tt::ExpansionError::Unknown("No sender is found.".to_string())), | ||
84 | Some(it) => it, | ||
85 | }; | ||
86 | |||
87 | let (result_tx, result_rx) = bounded(0); | 82 | let (result_tx, result_rx) = bounded(0); |
88 | let sender = match sender.upgrade() { | 83 | let sender = match self.inner.upgrade() { |
89 | None => { | 84 | None => return Err(tt::ExpansionError::Unknown("proc macro process is closed".into())), |
90 | return Err(tt::ExpansionError::Unknown("Proc macro process is closed.".into())) | ||
91 | } | ||
92 | Some(it) => it, | 85 | Some(it) => it, |
93 | }; | 86 | }; |
94 | sender.send(Task { req, result_tx }).unwrap(); | 87 | sender |
88 | .send(Task { req, result_tx }) | ||
89 | .map_err(|_| tt::ExpansionError::Unknown("proc macro server crashed".into()))?; | ||
90 | |||
95 | let res = result_rx | 91 | let res = result_rx |
96 | .recv() | 92 | .recv() |
97 | .map_err(|_| tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?; | 93 | .map_err(|_| tt::ExpansionError::Unknown("proc macro server crashed".into()))?; |
98 | 94 | ||
99 | match res { | 95 | match res { |
100 | Some(Response::Error(err)) => { | 96 | Some(Response::Error(err)) => { |
@@ -109,32 +105,23 @@ impl ProcMacroProcessSrv { | |||
109 | } | 105 | } |
110 | 106 | ||
111 | fn client_loop(task_rx: Receiver<Task>, mut process: Process) { | 107 | fn client_loop(task_rx: Receiver<Task>, mut process: Process) { |
112 | let (mut stdin, mut stdout) = match process.stdio() { | 108 | let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio"); |
113 | None => return, | ||
114 | Some(it) => it, | ||
115 | }; | ||
116 | |||
117 | for task in task_rx { | ||
118 | let Task { req, result_tx } = task; | ||
119 | 109 | ||
110 | for Task { req, result_tx } in task_rx { | ||
120 | match send_request(&mut stdin, &mut stdout, req) { | 111 | match send_request(&mut stdin, &mut stdout, req) { |
121 | Ok(res) => result_tx.send(res).unwrap(), | 112 | Ok(res) => result_tx.send(res).unwrap(), |
122 | Err(_err) => { | 113 | Err(_err) => { |
114 | log::error!( | ||
115 | "proc macro server crashed, server process state: {:?}", | ||
116 | process.child.try_wait() | ||
117 | ); | ||
123 | let res = Response::Error(ResponseError { | 118 | let res = Response::Error(ResponseError { |
124 | code: ErrorCode::ServerErrorEnd, | 119 | code: ErrorCode::ServerErrorEnd, |
125 | message: "Server closed".into(), | 120 | message: "proc macro server crashed".into(), |
126 | }); | 121 | }); |
127 | result_tx.send(res.into()).unwrap(); | 122 | result_tx.send(res.into()).unwrap(); |
128 | // Restart the process | 123 | // Exit the thread. |
129 | if process.restart().is_err() { | 124 | break; |
130 | break; | ||
131 | } | ||
132 | let stdio = match process.stdio() { | ||
133 | None => break, | ||
134 | Some(it) => it, | ||
135 | }; | ||
136 | stdin = stdio.0; | ||
137 | stdout = stdio.1; | ||
138 | } | 125 | } |
139 | } | 126 | } |
140 | } | 127 | } |
@@ -146,8 +133,6 @@ struct Task { | |||
146 | } | 133 | } |
147 | 134 | ||
148 | struct Process { | 135 | struct Process { |
149 | path: PathBuf, | ||
150 | args: Vec<OsString>, | ||
151 | child: Child, | 136 | child: Child, |
152 | } | 137 | } |
153 | 138 | ||
@@ -162,15 +147,9 @@ impl Process { | |||
162 | path: PathBuf, | 147 | path: PathBuf, |
163 | args: impl IntoIterator<Item = impl AsRef<OsStr>>, | 148 | args: impl IntoIterator<Item = impl AsRef<OsStr>>, |
164 | ) -> io::Result<Process> { | 149 | ) -> io::Result<Process> { |
165 | let args = args.into_iter().map(|s| s.as_ref().into()).collect(); | 150 | let args: Vec<OsString> = args.into_iter().map(|s| s.as_ref().into()).collect(); |
166 | let child = mk_child(&path, &args)?; | 151 | let child = mk_child(&path, &args)?; |
167 | Ok(Process { path, args, child }) | 152 | Ok(Process { child }) |
168 | } | ||
169 | |||
170 | fn restart(&mut self) -> io::Result<()> { | ||
171 | let _ = self.child.kill(); | ||
172 | self.child = mk_child(&self.path, &self.args)?; | ||
173 | Ok(()) | ||
174 | } | 153 | } |
175 | 154 | ||
176 | fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> { | 155 | fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> { |