Skip to content

Commit

Permalink
fix: Fix console dropping.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jul 8, 2024
1 parent 0bb440f commit 7815fde
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ inquire = "0.7.5"
miette = { workspace = true }
parking_lot = "0.12.3"
starbase_styles = { workspace = true }
tracing = { workspace = true }

[lints]
workspace = true
21 changes: 14 additions & 7 deletions crates/console/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::mpsc::{self, Receiver, Sender, TryRecvError};
use std::sync::Arc;
use std::thread::{sleep, spawn, JoinHandle};
use std::time::Duration;
use tracing::{debug, warn};

#[derive(Clone, Copy)]
pub enum ConsoleStream {
Expand Down Expand Up @@ -82,6 +83,14 @@ impl ConsoleBuffer {
}

pub fn close(&self) -> miette::Result<()> {
debug!(
"Closing {} stream",
match self.stream {
ConsoleStream::Stderr => "stderr",
ConsoleStream::Stdout => "stdout",
}
);

self.flush()?;

// Send the closed message
Expand Down Expand Up @@ -150,12 +159,6 @@ impl ConsoleBuffer {
}
}

impl Drop for ConsoleBuffer {
fn drop(&mut self) {
self.close().unwrap();
}
}

impl Clone for ConsoleBuffer {
fn clone(&self) -> Self {
Self {
Expand Down Expand Up @@ -191,7 +194,11 @@ fn flush_on_loop(buffer: Arc<Mutex<Vec<u8>>>, stream: ConsoleStream, receiver: R

// Has the thread been closed?
match receiver.try_recv() {
Ok(true) | Err(TryRecvError::Disconnected) => {
Ok(true) => {
break;
}
Err(TryRecvError::Disconnected) => {
warn!("Console auto-flush has been disconnected, output will be heavily buffered");
break;
}
_ => {}
Expand Down
11 changes: 5 additions & 6 deletions crates/console/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use moon_common::is_formatted_output;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::JoinHandle;
use tracing::debug;

pub type ConsoleTheme = RenderConfig<'static>;

Expand All @@ -24,6 +25,8 @@ pub struct Console {

impl Console {
pub fn new(quiet: bool) -> Self {
debug!("Creating buffered console");

let quiet = Arc::new(AtomicBool::new(quiet || is_formatted_output()));

let mut err = ConsoleBuffer::new(ConsoleStream::Stderr);
Expand Down Expand Up @@ -56,6 +59,8 @@ impl Console {
}

pub fn close(&mut self) -> miette::Result<()> {
debug!("Closing console and flushing buffered output");

self.err.close()?;
self.out.close()?;

Expand Down Expand Up @@ -107,9 +112,3 @@ impl Clone for Console {
}
}
}

impl Drop for Console {
fn drop(&mut self) {
self.close().unwrap();
}
}
1 change: 1 addition & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Fixed `moon run --affected` not auto-installing dependencies in CI.
- Fixed an issue where the project graph cache would not invalidate based on certain environment
variables.
- Fixed an issue where our console would be prematurely dropped, causing output flushing issues.

## 1.26.6

Expand Down

0 comments on commit 7815fde

Please sign in to comment.