Skip to content

Commit

Permalink
properly handles eof for spawned process
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed May 6, 2024
1 parent d57e4f6 commit 7d7c6a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src-tauri/src/plugins/cmd.rs
@@ -1,4 +1,4 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::io::{Read, Write};
use std::sync::Arc;

Expand All @@ -13,6 +13,14 @@ use crate::event_channel::{EventChannel, EventHandler};
use crate::session_manager::{Proc, ProcCallback, ProcData, SessionManager};
use crate::spawn_manager::SpawnManager;

#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(tag = "type")]
pub(crate) enum SpawnResult {
Exit { status: i32 },
Signal { signal: u32 },
Closed,
}

#[tauri::command]
async fn exec<R: Runtime>(
app: AppHandle<R>,
Expand Down Expand Up @@ -87,7 +95,7 @@ fn proc_worker<R: Runtime>(
match proc.wait_close(&app.state::<SessionManager>()) {
Ok(r) => {
log::info!("{proc:?} closed with {r:?}");
channel.closed(&r);
channel.closed(SpawnResult::Exit { status: r });
}
Err(e) => {
log::warn!("{proc:?} closed with {e:?}");
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/session_manager/proc.rs
Expand Up @@ -78,7 +78,7 @@ impl Proc {
channel.request_exec(&self.command)?;
let mut buf = [0; 8192];
let mut interrupted = false;
while !channel.is_closed() {
while !channel.is_closed() && !channel.is_eof() {
if self.interrupted.lock().unwrap().eq(&true) {
channel.send_eof()?;
log::info!("interrupting luna-send");
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/services/remote-command.service.ts
Expand Up @@ -68,12 +68,12 @@ export declare class CommandData<T = Buffer | string> {
}

export class CommandSubject<T = Buffer | string> extends ReplaySubject<CommandData<T>> {
private channel: EventChannel<ProcData, any>;
private channel: EventChannel<ProcData, SpawnResult>;

constructor(zone: NgZone, token: string, command: string, encoding: 'buffer' | 'utf-8' | undefined) {
super();
const subject = this;
this.channel = new class extends EventChannel<ProcData, any> {
this.channel = new class extends EventChannel<ProcData, SpawnResult> {
stdout: string = '';
stderr: string = '';
interrupted = false;
Expand Down

0 comments on commit 7d7c6a0

Please sign in to comment.