Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 934e6f12 authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

floss: Synchronize readline with foreground action

While ForegroundAction::Readline is happening, the readline task should
wait until it's permitted to do readline again. Otherwise when the
program quits, the readline task will be interrupted before it cleans up
terminal resources causing terminal hang.

Bug: 199213563
Tag: #floss
Test: Manual

Change-Id: I87afde2be3445c381c8a61cf0ab56c87144b5d39
parent cb339a66
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -296,11 +296,23 @@ async fn start_interactive_shell(
) {
    let command_list = handler.get_command_list().clone();

    let semaphore_fg = Arc::new(tokio::sync::Semaphore::new(1));

    // Async task to keep reading new lines from user
    let semaphore = semaphore_fg.clone();
    tokio::spawn(async move {
        let editor = AsyncEditor::new(command_list);

        loop {
            // Wait until ForegroundAction::Readline finishes its task.
            let permit = semaphore.acquire().await;
            if permit.is_err() {
                break;
            };
            // Let ForegroundAction::Readline decide when it's done.
            permit.unwrap().forget();

            // It's good to do readline now.
            let result = editor.readline().await;
            let _ = tx.send(ForegroundActions::Readline(result)).await;
        }
@@ -340,9 +352,14 @@ async fn start_interactive_shell(
                        &String::from(cmd),
                        &command_vec[1..command_vec.len()].to_vec(),
                    );
                    // Ready to do readline again.
                    semaphore_fg.add_permits(1);
                }
            },
        }
    }

    semaphore_fg.close();

    print_info!("Client exiting");
}