Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): synchronize async stdio/file reads and writes #15092

Merged
merged 16 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ use test_util as util;
use test_util::TempDir;

itest!(stdout_write_all {
args: "run --quiet stdout_write_all.ts",
output: "stdout_write_all.out",
args: "run --quiet run/stdout_write_all.ts",
output: "run/stdout_write_all.out",
});

itest!(stdin_read_all {
args: "run --quiet run/stdin_read_all.ts",
output: "run/stdin_read_all.out",
input: Some("01234567890123456789012345678901234567890123456789"),
});

itest!(_001_hello {
Expand Down
1 change: 1 addition & 0 deletions cli/tests/testdata/run/stdin_read_all.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
01234567890123456789012345678901234567890123456789
17 changes: 17 additions & 0 deletions cli/tests/testdata/run/stdin_read_all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const encoder = new TextEncoder();

const pending = [];

// do this a bunch of times to ensure it doesn't race
// and everything happens in order
for (let i = 0; i < 50; i++) {
const buf = new Uint8Array(1);
pending.push(
Deno.stdin.read(buf).then(() => {
return Deno.stdout.write(buf);
}),
);
}

await Promise.all(pending);
await Deno.stdout.write(encoder.encode("\n"));
100 changes: 100 additions & 0 deletions cli/tests/testdata/run/stdout_write_all.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Hello, world! 0
Hello, world! 1
Hello, world! 2
Hello, world! 3
Hello, world! 4
Hello, world! 5
Hello, world! 6
Hello, world! 7
Hello, world! 8
Hello, world! 9
Hello, world! 10
Hello, world! 11
Hello, world! 12
Hello, world! 13
Hello, world! 14
Hello, world! 15
Hello, world! 16
Hello, world! 17
Hello, world! 18
Hello, world! 19
Hello, world! 20
Hello, world! 21
Hello, world! 22
Hello, world! 23
Hello, world! 24
Hello, world! 25
Hello, world! 26
Hello, world! 27
Hello, world! 28
Hello, world! 29
Hello, world! 30
Hello, world! 31
Hello, world! 32
Hello, world! 33
Hello, world! 34
Hello, world! 35
Hello, world! 36
Hello, world! 37
Hello, world! 38
Hello, world! 39
Hello, world! 40
Hello, world! 41
Hello, world! 42
Hello, world! 43
Hello, world! 44
Hello, world! 45
Hello, world! 46
Hello, world! 47
Hello, world! 48
Hello, world! 49
Hello, world! 50
Hello, world! 51
Hello, world! 52
Hello, world! 53
Hello, world! 54
Hello, world! 55
Hello, world! 56
Hello, world! 57
Hello, world! 58
Hello, world! 59
Hello, world! 60
Hello, world! 61
Hello, world! 62
Hello, world! 63
Hello, world! 64
Hello, world! 65
Hello, world! 66
Hello, world! 67
Hello, world! 68
Hello, world! 69
Hello, world! 70
Hello, world! 71
Hello, world! 72
Hello, world! 73
Hello, world! 74
Hello, world! 75
Hello, world! 76
Hello, world! 77
Hello, world! 78
Hello, world! 79
Hello, world! 80
Hello, world! 81
Hello, world! 82
Hello, world! 83
Hello, world! 84
Hello, world! 85
Hello, world! 86
Hello, world! 87
Hello, world! 88
Hello, world! 89
Hello, world! 90
Hello, world! 91
Hello, world! 92
Hello, world! 93
Hello, world! 94
Hello, world! 95
Hello, world! 96
Hello, world! 97
Hello, world! 98
Hello, world! 99
13 changes: 13 additions & 0 deletions cli/tests/testdata/run/stdout_write_all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const encoder = new TextEncoder();

const pending = [];

// do this a bunch of times to ensure it doesn't race
// and everything happens in order
for (let i = 0; i < 100; i++) {
pending.push(Deno.stdout.write(encoder.encode("Hello, ")));
pending.push(Deno.stdout.write(encoder.encode(`world! ${i}`)));
pending.push(Deno.stdout.write(encoder.encode("\n")));
}

await Promise.all(pending);
1 change: 0 additions & 1 deletion cli/tests/testdata/stdout_write_all.out

This file was deleted.

8 changes: 0 additions & 8 deletions cli/tests/testdata/stdout_write_all.ts

This file was deleted.

3 changes: 3 additions & 0 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod ops_metrics;
mod resources;
mod runtime;
mod source_map;
mod task_queue;

// Re-exports
pub use anyhow;
Expand Down Expand Up @@ -102,6 +103,8 @@ pub use crate::runtime::RuntimeOptions;
pub use crate::runtime::SharedArrayBufferStore;
pub use crate::runtime::Snapshot;
pub use crate::source_map::SourceMapGetter;
pub use crate::task_queue::TaskQueue;
pub use crate::task_queue::TaskQueuePermit;
pub use deno_ops::op;

pub fn v8_version() -> &'static str {
Expand Down
142 changes: 142 additions & 0 deletions core/task_queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use std::collections::LinkedList;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use crate::futures::task::AtomicWaker;
use crate::futures::Future;
use crate::parking_lot::Mutex;

#[derive(Default)]
struct TaskQueueTaskWaker {
is_ready: AtomicBool,
waker: AtomicWaker,
}

#[derive(Default)]
struct TaskQueueTasks {
is_running: bool,
wakers: LinkedList<Arc<TaskQueueTaskWaker>>,
}

/// A queue that executes tasks sequentially one after the other
/// ensuring order and that no task runs at the same time as another.
#[derive(Clone, Default)]
pub struct TaskQueue {
dsherret marked this conversation as resolved.
Show resolved Hide resolved
tasks: Arc<Mutex<TaskQueueTasks>>,
}

impl TaskQueue {
pub fn new() -> Self {
Default::default()
}

pub async fn queue<R>(&self, future: impl Future<Output = R>) -> R {
let _permit = self.acquire().await;
future.await
}

pub async fn acquire(&self) -> TaskQueuePermit {
let acquire = TaskQueuePermitAcquire::new(self.tasks.clone());
acquire.await;
TaskQueuePermit {
tasks: self.tasks.clone(),
}
}
}

/// A permit that when dropped will allow another task to proceed.
pub struct TaskQueuePermit {
tasks: Arc<Mutex<TaskQueueTasks>>,
}

impl Drop for TaskQueuePermit {
fn drop(&mut self) {
let next_item = {
let mut tasks = self.tasks.lock();
let next_item = tasks.wakers.pop_front();
tasks.is_running = next_item.is_some();
next_item
};
if let Some(next_item) = next_item {
next_item.is_ready.store(true, Ordering::SeqCst);
next_item.waker.wake();
}
}
}

struct TaskQueuePermitAcquire {
tasks: Arc<Mutex<TaskQueueTasks>>,
initialized: AtomicBool,
waker: Arc<TaskQueueTaskWaker>,
}

impl TaskQueuePermitAcquire {
pub fn new(tasks: Arc<Mutex<TaskQueueTasks>>) -> Self {
Self {
tasks,
initialized: Default::default(),
waker: Default::default(),
}
}
}

impl Future for TaskQueuePermitAcquire {
type Output = ();

fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
// update with the latest waker
self.waker.waker.register(cx.waker());

// ensure this is initialized
if !self.initialized.swap(true, Ordering::SeqCst) {
let mut tasks = self.tasks.lock();
if !tasks.is_running {
tasks.is_running = true;
return std::task::Poll::Ready(());
}
tasks.wakers.push_back(self.waker.clone());
return std::task::Poll::Pending;
}

// check if we're ready to run
if self.waker.is_ready.load(Ordering::SeqCst) {
std::task::Poll::Ready(())
} else {
std::task::Poll::Pending
}
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::TaskQueue;
use crate::parking_lot::Mutex;

#[tokio::test]
async fn task_queue_runs_one_after_other() {
let task_queue = TaskQueue::new();
let mut tasks = Vec::new();
let data = Arc::new(Mutex::new(0));
for i in 0..100 {
let data = data.clone();
tasks.push(task_queue.queue(async move {
tokio::task::spawn_blocking(move || {
let mut data = data.lock();
if *data != i {
panic!("Value was not equal.");
}
*data = i + 1;
})
.await
.unwrap();
}));
}
futures::future::join_all(tasks).await;
}
}
Loading