diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index c91169f1a6..564b7355fc 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -3723,3 +3723,23 @@ fn file_fetcher_preserves_permissions() { assert_contains!(output, "true"); }); } + +#[test] +fn stdio_streams_are_locked_in_permission_prompt() { + let _guard = util::http_server(); + util::with_pty(&[ + "repl", + "--allow-read=run/stdio_streams_are_locked_in_permission_prompt/worker.js,run/stdio_streams_are_locked_in_permission_prompt/text.txt" + ], |mut console| { + console.write_line( + r#"new Worker(`${Deno.cwd()}/run/stdio_streams_are_locked_in_permissions_prompt/worker.js`, { type: "module" }); + await Deno.writeTextFile("./run/stdio_streams_are_locked_in_permissions_prompt/text.txt", "some code");"#, + ); + console.write_line("y"); + console.write_line("close();"); + let output = console.read_all_output(); + + let expected_output = r#"\x1b[1;1H\x1b[0JAre you sure you want to continue?"#; + assert_eq!(output, expected_output); + }); +} diff --git a/cli/tests/testdata/run/stdio_streams_are_locked_in_permission_prompt/text.txt b/cli/tests/testdata/run/stdio_streams_are_locked_in_permission_prompt/text.txt new file mode 100644 index 0000000000..e6177e9cf8 --- /dev/null +++ b/cli/tests/testdata/run/stdio_streams_are_locked_in_permission_prompt/text.txt @@ -0,0 +1 @@ +\x1B[2J\x1B[1;1H \ No newline at end of file diff --git a/cli/tests/testdata/run/stdio_streams_are_locked_in_permission_prompt/worker.js b/cli/tests/testdata/run/stdio_streams_are_locked_in_permission_prompt/worker.js new file mode 100644 index 0000000000..6d67c51a2a --- /dev/null +++ b/cli/tests/testdata/run/stdio_streams_are_locked_in_permission_prompt/worker.js @@ -0,0 +1,2 @@ +console.clear(); +console.log("Are you sure you want to continue?"); diff --git a/runtime/permissions/prompter.rs b/runtime/permissions/prompter.rs index a738988355..ae07e004c8 100644 --- a/runtime/permissions/prompter.rs +++ b/runtime/permissions/prompter.rs @@ -192,6 +192,11 @@ impl PermissionPrompter for TtyPrompter { return PromptResponse::Deny; // don't grant permission if this fails } + // Lock stdio streams, so no other output is written while the prompt is + // displayed. + let _stdout_guard = std::io::stdout().lock(); + let _stderr_guard = std::io::stderr().lock(); + // print to stderr so that if stdout is piped this is still displayed. const OPTS: &str = "[y/n] (y = yes, allow; n = no, deny)"; eprint!("{} ┌ ", PERMISSION_EMOJI); @@ -238,6 +243,9 @@ impl PermissionPrompter for TtyPrompter { }; }; + drop(_stdout_guard); + drop(_stderr_guard); + value } }