-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Improve stdio_transport: error handling & channel buffering #20479
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
Closed
KushalMeghani1644
wants to merge
2
commits into
rust-lang:master
from
KushalMeghani1644:improve-stdio-transport
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,53 @@ | ||
use crate::Message; | ||
use crossbeam_channel::{Receiver, Sender, bounded}; | ||
use log::debug; | ||
use std::{ | ||
io::{self, stdin, stdout}, | ||
thread, | ||
}; | ||
|
||
use log::debug; | ||
|
||
use crossbeam_channel::{Receiver, Sender, bounded}; | ||
|
||
use crate::Message; | ||
|
||
/// Creates an LSP connection via stdio. | ||
pub(crate) fn stdio_transport() -> (Sender<Message>, Receiver<Message>, IoThreads) { | ||
let (drop_sender, drop_receiver) = bounded::<Message>(0); | ||
let (writer_sender, writer_receiver) = bounded::<Message>(0); | ||
let (drop_sender, drop_receiver) = bounded::<Message>(1); // Changed from 0 -> 1 for minimal buffering | ||
let (writer_sender, writer_receiver) = bounded::<Message>(1); // Changed from 0 -> 1 | ||
|
||
let writer = thread::Builder::new() | ||
.name("LspServerWriter".to_owned()) | ||
.spawn(move || { | ||
.spawn(move || -> io::Result<()> { | ||
let stdout = stdout(); | ||
let mut stdout = stdout.lock(); | ||
writer_receiver.into_iter().try_for_each(|it| { | ||
for it in writer_receiver { | ||
let result = it.write(&mut stdout); | ||
let _ = drop_sender.send(it); | ||
result | ||
}) | ||
let _ = drop_sender.send(it); // Fixed: was `let * = drop*sender.send(it);` | ||
result?; // Propagate error instead of unwrap | ||
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. We are not unwrapping error here in |
||
} | ||
Ok(()) | ||
}) | ||
.unwrap(); | ||
.expect("Failed to spawn writer thread"); // Replaced unwrap with expect for better context | ||
|
||
let dropper = thread::Builder::new() | ||
.name("LspMessageDropper".to_owned()) | ||
.spawn(move || drop_receiver.into_iter().for_each(drop)) | ||
.unwrap(); | ||
let (reader_sender, reader_receiver) = bounded::<Message>(0); | ||
.expect("Failed to spawn dropper thread"); | ||
|
||
let (reader_sender, reader_receiver) = bounded::<Message>(1); | ||
let reader = thread::Builder::new() | ||
.name("LspServerReader".to_owned()) | ||
.spawn(move || { | ||
.spawn(move || -> io::Result<()> { | ||
let stdin = stdin(); | ||
let mut stdin = stdin.lock(); | ||
while let Some(msg) = Message::read(&mut stdin)? { | ||
let is_exit = matches!(&msg, Message::Notification(n) if n.is_exit()); | ||
|
||
debug!("sending message {msg:#?}"); | ||
if let Err(e) = reader_sender.send(msg) { | ||
return Err(io::Error::other(e)); | ||
} | ||
|
||
reader_sender.send(msg).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; // Better error propagation | ||
if is_exit { | ||
break; | ||
} | ||
} | ||
Ok(()) | ||
}) | ||
.unwrap(); | ||
.expect("Failed to spawn reader thread"); | ||
|
||
let threads = IoThreads { reader, writer, dropper }; | ||
(writer_sender, reader_receiver, threads) | ||
} | ||
|
@@ -71,21 +69,10 @@ | |
|
||
impl IoThreads { | ||
pub fn join(self) -> io::Result<()> { | ||
match self.reader.join() { | ||
Ok(r) => r?, | ||
Err(err) => std::panic::panic_any(err), | ||
} | ||
match self.dropper.join() { | ||
Ok(_) => (), | ||
Err(err) => { | ||
std::panic::panic_any(err); | ||
} | ||
} | ||
match self.writer.join() { | ||
Ok(r) => r, | ||
Err(err) => { | ||
std::panic::panic_any(err); | ||
} | ||
} | ||
self.reader | ||
.join() | ||
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e:?}")))??; | ||
self.dropper.join().map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e:?}")))?; // Fixed: was std::io::Error::Other | ||
self.writer.join().map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e:?}")))? // Fixed: was std::io::Error::Other and missing ? | ||
KushalMeghani1644 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think
try_for_each
is good enough here