-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Make proc-macro bidirectional calls cancellation safe #21410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7dcfaf0
014327e
7b7077a
33012ff
c3c1ece
2992147
5f63aab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,10 +7,11 @@ use proc_macro_api::{ | |||||
| version::CURRENT_API_VERSION, | ||||||
| }; | ||||||
| use std::io; | ||||||
| use std::panic::panic_any; | ||||||
|
|
||||||
| use legacy::Message; | ||||||
|
|
||||||
| use proc_macro_srv::{EnvSnapshot, SpanId}; | ||||||
| use proc_macro_srv::{EnvSnapshot, ProcMacroCancelMarker, ProcMacroClientError, SpanId}; | ||||||
|
|
||||||
| use crate::ProtocolFormat; | ||||||
| struct SpanTrans; | ||||||
|
|
@@ -170,27 +171,49 @@ impl<'a, C: Codec> ProcMacroClientHandle<'a, C> { | |||||
| fn roundtrip( | ||||||
| &mut self, | ||||||
| req: bidirectional::SubRequest, | ||||||
| ) -> Option<bidirectional::BidirectionalMessage> { | ||||||
| ) -> Result<bidirectional::SubResponse, ProcMacroClientError> { | ||||||
| let msg = bidirectional::BidirectionalMessage::SubRequest(req); | ||||||
|
|
||||||
| if msg.write::<_, C>(&mut self.stdout.lock()).is_err() { | ||||||
| return None; | ||||||
| msg.write::<_, C>(&mut self.stdout.lock()).map_err(ProcMacroClientError::Io)?; | ||||||
|
|
||||||
| let msg = | ||||||
| bidirectional::BidirectionalMessage::read::<_, C>(&mut self.stdin.lock(), self.buf) | ||||||
| .map_err(ProcMacroClientError::Io)? | ||||||
| .ok_or(ProcMacroClientError::Eof)?; | ||||||
|
|
||||||
| match msg { | ||||||
| bidirectional::BidirectionalMessage::SubResponse(resp) => match resp { | ||||||
| bidirectional::SubResponse::Cancel { reason } => { | ||||||
| Err(ProcMacroClientError::Cancelled { reason }) | ||||||
| } | ||||||
| other => Ok(other), | ||||||
| }, | ||||||
| other => { | ||||||
| Err(ProcMacroClientError::Protocol(format!("expected SubResponse, got {other:?}"))) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| match bidirectional::BidirectionalMessage::read::<_, C>(&mut self.stdin.lock(), self.buf) { | ||||||
| Ok(Some(msg)) => Some(msg), | ||||||
| _ => None, | ||||||
| fn handle_failure(failure: Result<bidirectional::SubResponse, ProcMacroClientError>) -> ! { | ||||||
| match failure { | ||||||
| Err(ProcMacroClientError::Cancelled { reason }) => { | ||||||
| panic_any(ProcMacroCancelMarker { reason }); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There is no need to incur the panic handler in this case |
||||||
| } | ||||||
| Err(err) => { | ||||||
| panic!("proc-macro IPC failed: {err:?}"); | ||||||
| } | ||||||
| Ok(other) => { | ||||||
| panic!("unexpected SubResponse {other:?}"); | ||||||
| } | ||||||
|
Comment on lines
+203
to
208
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> { | ||||||
| fn file(&mut self, file_id: proc_macro_srv::span::FileId) -> String { | ||||||
| match self.roundtrip(bidirectional::SubRequest::FilePath { file_id: file_id.index() }) { | ||||||
| Some(bidirectional::BidirectionalMessage::SubResponse( | ||||||
| bidirectional::SubResponse::FilePathResult { name }, | ||||||
| )) => name, | ||||||
| _ => String::new(), | ||||||
| Ok(bidirectional::SubResponse::FilePathResult { name }) => name, | ||||||
| other => handle_failure(other), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -204,20 +227,16 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl | |||||
| start: range.start().into(), | ||||||
| end: range.end().into(), | ||||||
| }) { | ||||||
| Some(bidirectional::BidirectionalMessage::SubResponse( | ||||||
| bidirectional::SubResponse::SourceTextResult { text }, | ||||||
| )) => text, | ||||||
| _ => None, | ||||||
| Ok(bidirectional::SubResponse::SourceTextResult { text }) => text, | ||||||
Shourya742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| other => handle_failure(other), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn local_file(&mut self, file_id: proc_macro_srv::span::FileId) -> Option<String> { | ||||||
| match self.roundtrip(bidirectional::SubRequest::LocalFilePath { file_id: file_id.index() }) | ||||||
| { | ||||||
| Some(bidirectional::BidirectionalMessage::SubResponse( | ||||||
| bidirectional::SubResponse::LocalFilePathResult { name }, | ||||||
| )) => name, | ||||||
| _ => None, | ||||||
| Ok(bidirectional::SubResponse::LocalFilePathResult { name }) => name, | ||||||
| other => handle_failure(other), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -228,10 +247,10 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl | |||||
| ast_id: anchor.ast_id.into_raw(), | ||||||
| offset: range.start().into(), | ||||||
| }) { | ||||||
| Some(bidirectional::BidirectionalMessage::SubResponse( | ||||||
| bidirectional::SubResponse::LineColumnResult { line, column }, | ||||||
| )) => Some((line, column)), | ||||||
| _ => None, | ||||||
| Ok(bidirectional::SubResponse::LineColumnResult { line, column }) => { | ||||||
| Some((line, column)) | ||||||
| } | ||||||
| other => handle_failure(other), | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,19 @@ impl<'env> ProcMacroSrv<'env> { | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum ProcMacroClientError { | ||
| Cancelled { reason: String }, | ||
| Io(std::io::Error), | ||
| Protocol(String), | ||
| Eof, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ProcMacroCancelMarker { | ||
| pub reason: String, | ||
| } | ||
|
|
||
| pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Sync + Send); | ||
|
|
||
| pub trait ProcMacroClientInterface { | ||
|
|
@@ -104,6 +117,20 @@ pub trait ProcMacroClientInterface { | |
|
|
||
| const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024; | ||
|
|
||
| pub enum ExpandError { | ||
| Panic(PanicMessage), | ||
| Cancelled { reason: Option<String> }, | ||
| } | ||
|
|
||
| impl ExpandError { | ||
| pub fn into_string(self) -> Option<String> { | ||
| match self { | ||
| ExpandError::Panic(panic_message) => panic_message.into_string(), | ||
| ExpandError::Cancelled { reason } => reason, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ProcMacroSrv<'_> { | ||
| pub fn expand<S: ProcMacroSrvSpan>( | ||
| &self, | ||
|
|
@@ -117,10 +144,12 @@ impl ProcMacroSrv<'_> { | |
| call_site: S, | ||
| mixed_site: S, | ||
| callback: Option<ProcMacroClientHandle<'_>>, | ||
| ) -> Result<token_stream::TokenStream<S>, PanicMessage> { | ||
| ) -> Result<token_stream::TokenStream<S>, ExpandError> { | ||
| let snapped_env = self.env; | ||
| let expander = self.expander(lib.as_ref()).map_err(|err| PanicMessage { | ||
| message: Some(format!("failed to load macro: {err}")), | ||
| let expander = self.expander(lib.as_ref()).map_err(|err| { | ||
| ExpandError::Panic(PanicMessage { | ||
| message: Some(format!("failed to load macro: {err}")), | ||
| }) | ||
|
Comment on lines
+150
to
+152
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This then can also make use of that new internal error type since this is technically not a proc-macro panic |
||
| })?; | ||
|
|
||
| let prev_env = EnvChange::apply(snapped_env, env, current_dir.as_ref().map(<_>::as_ref)); | ||
|
|
@@ -138,8 +167,14 @@ impl ProcMacroSrv<'_> { | |
| ) | ||
| }); | ||
| match thread.unwrap().join() { | ||
| Ok(res) => res, | ||
| Err(e) => std::panic::resume_unwind(e), | ||
| Ok(res) => res.map_err(ExpandError::Panic), | ||
|
|
||
| Err(payload) => { | ||
| if let Some(cancel) = payload.downcast_ref::<ProcMacroCancelMarker>() { | ||
| return Err(ExpandError::Cancelled { reason: Some(cancel.reason.clone()) }); | ||
| } | ||
| std::panic::resume_unwind(payload) | ||
| } | ||
| } | ||
|
Comment on lines
169
to
178
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here we can then also downcast to that internal error variant as well |
||
| }); | ||
| prev_env.rollback(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would slightly prefer to have the callback type
+ UnwindSafethan thisAssertUnwindSafe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky because the callback captures &dyn ExpandDatabase (later at implementation), which is not
UnwindSafe. RequiringUnwindSafeon the callback would therefore force unwind-safety guarantees onExpandDatabase.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea the issue is that our database contains a
parking_lot::RwLock(due theDashMapusage) which is notRefUnwindSafeas unlike std's verison it does not implement poisoning.Let's put a comment note here though regarding that, we might be able to get rid of that issue in the future as this is all due to the VFS stuff only