Skip to content

Commit

Permalink
Merge pull request #744 from marhkb/fix/container-terminal/clsoe-proc…
Browse files Browse the repository at this point in the history
…esses

fix(container-terminal): Close processes on exit
  • Loading branch information
marhkb authored Nov 25, 2023
2 parents 51b32b3 + 6107aa7 commit 5410ee2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions data/com.github.marhkb.Pods.metainfo.xml.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<p>Pods 2.1.0 contains the following changes:</p>
<ul>
<li>Build about dialog from appdata. (#743)</li>
<li>Close all remaining processes after container terminal exits. (#744)</li>
</ul>
</description>
</release>
Expand Down
33 changes: 28 additions & 5 deletions src/view/container_terminal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::Cell;
use std::cell::RefCell;
use std::sync::OnceLock;

Expand Down Expand Up @@ -33,6 +34,7 @@ const ACTION_PASTE: &str = "container-terminal.paste";
enum ExecInput {
Data(Vec<u8>),
Resize { columns: usize, rows: usize },
Terminate,
}

mod imp {
Expand All @@ -43,7 +45,8 @@ mod imp {
#[template(resource = "/com/github/marhkb/Pods/ui/view/container_terminal.ui")]
pub(crate) struct ContainerTerminal {
pub(super) settings: utils::PodsSettings,
pub(super) tx_tokio: RefCell<Option<tokio::sync::mpsc::UnboundedSender<ExecInput>>>,
pub(super) tx_input: RefCell<Option<tokio::sync::mpsc::UnboundedSender<ExecInput>>>,
pub(super) keep_alive_on_next_unroot: Cell<bool>,
#[property(get, set, nullable)]
pub(super) container: glib::WeakRef<model::Container>,
#[template_child]
Expand Down Expand Up @@ -229,13 +232,23 @@ mod imp {

fn size_allocate(&self, width: i32, height: i32, baseline: i32) {
self.stack.allocate(width, height, baseline, None);
if let Some(tx_tokio) = &*self.tx_tokio.borrow() {
if let Some(tx_tokio) = &*self.tx_input.borrow() {
_ = tx_tokio.send(ExecInput::Resize {
columns: self.terminal.column_count() as usize,
rows: self.terminal.row_count() as usize,
});
}
}

fn unroot(&self) {
if !self.keep_alive_on_next_unroot.get() {
if let Some(tx_input) = &*self.tx_input.borrow() {
_ = tx_input.send(ExecInput::Terminate);
}
}
self.keep_alive_on_next_unroot.set(false);
self.parent_unroot();
}
}

#[gtk::template_callbacks]
Expand Down Expand Up @@ -352,6 +365,10 @@ glib::wrapper! {
}

impl ContainerTerminal {
pub(crate) fn keep_alive_on_next_unroot(&self) {
self.imp().keep_alive_on_next_unroot.set(true);
}

pub(crate) fn font_scale(&self) -> f64 {
self.imp().terminal.font_scale()
}
Expand All @@ -377,7 +394,7 @@ impl ContainerTerminal {
}));

let (tx_input, mut rx_input) = tokio::sync::mpsc::unbounded_channel::<ExecInput>();
imp.tx_tokio.replace(Some(tx_input.clone()));
imp.tx_input.replace(Some(tx_input.clone()));
imp.terminal.connect_commit(move |_, data, _| {
_ = tx_input.send(ExecInput::Data(data.as_bytes().to_vec()));
});
Expand Down Expand Up @@ -406,7 +423,7 @@ impl ContainerTerminal {
loop {
match future::select(Box::pin(rx_input.recv()), reader.next()).await {
future::Either::Left((buf, _)) => match buf {
Some(buf) => match buf {
Some(input) => match input {
ExecInput::Data(buf) => {
if let Err(e) = writer.write_all(&buf).await {
log::error!("Error on writing to terminal: {e}");
Expand All @@ -419,6 +436,7 @@ impl ContainerTerminal {
break;
}
}
ExecInput::Terminate => break,
},
None => break,
},
Expand All @@ -437,7 +455,12 @@ impl ContainerTerminal {
}
}

// Close all processes.
// Close remaining processes.
log::info!(
"Closing remaining processes of container {} for exec {}.",
container.id(),
exec.id()
);
while writer.write_all(&[3]).await.is_ok() && writer.write_all(&[4]).await.is_ok() {
}

Expand Down
2 changes: 2 additions & 0 deletions src/view/container_terminal_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ impl From<&model::Container> for ContainerTerminalPage {
impl ContainerTerminalPage {
pub(crate) fn pip_out(&self) {
if let Some(navigation_view) = utils::try_navigation_view(self.upcast_ref()) {
self.imp().terminal.keep_alive_on_next_unroot();

self.action_set_enabled(ACTION_PIP_OUT, false);

let animate_transitions = navigation_view.is_animate_transitions();
Expand Down

0 comments on commit 5410ee2

Please sign in to comment.