Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 10 additions & 8 deletions .github/workflows/ci_test_executor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ jobs:
- uses: taiki-e/install-action@nextest
- name: Test loom on ${{ matrix.setup.os }} nightly ${{ matrix.setup.target }}
shell: bash
env:
LOOM_MAX_PREEMPTIONS: 4
run: |
set -ex

cargo miri test \
--test executor \
-p compio-executor
-p compio-executor \
--test executor

RUSTFLAGS="--cfg loom" cargo nextest run \
--no-fail-fast \
--failure-output final \
RUSTFLAGS="--cfg loom" \
LOOM_LOCATION=1 \
LOOM_LOG=info,compio_executor=trace \
cargo nextest run \
-p compio-executor \
--features enable_log \
--test loom \
-p compio-executor
--no-fail-fast \
--failure-output final
2 changes: 1 addition & 1 deletion compio-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ slotmap = { workspace = true }
loom = { version = "0.7", features = ["checkpoint"] }

[dev-dependencies]
compio-log = { workspace = true, features = ["enable_log"] }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[target.'cfg(unix)'.dev-dependencies]
nix = { workspace = true, features = ["resource", "signal"] }

[features]
notify-always = []
enable_log = ["compio-log/enable_log"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] }
45 changes: 35 additions & 10 deletions compio-executor/src/join_handle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{
error::Error,
fmt::Display,
marker::PhantomData,
mem::ManuallyDrop,
panic::resume_unwind,
Expand All @@ -7,7 +9,7 @@ use std::{
task::{Context, Poll},
};

use compio_log::instrument;
use compio_log::{instrument, trace};

use crate::{Panic, task::Task};

Expand All @@ -19,7 +21,7 @@ use crate::{Panic, task::Task};
#[derive(Debug)]
#[repr(transparent)]
pub struct JoinHandle<T> {
task: Task,
task: Option<Task>,
_marker: PhantomData<T>,
}

Expand All @@ -35,14 +37,14 @@ impl<T> Unpin for JoinHandle<T> {}
impl<T> JoinHandle<T> {
pub(crate) fn new(task: Task) -> Self {
Self {
task,
task: Some(task),
_marker: PhantomData,
}
}

/// Cancel the task and wait for the result, if any.
pub async fn cancel(self) -> Option<T> {
self.task.cancel(false);
self.task.as_ref()?.cancel(false);
self.await.ok()
}

Expand Down Expand Up @@ -91,14 +93,35 @@ impl JoinError {
}
}

impl Display for JoinError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JoinError::Cancelled => write!(f, "Task was cancelled"),
JoinError::Panicked(_) => write!(f, "Task has panicked"),
}
}
}

impl Error for JoinError {}

impl<T> Future for JoinHandle<T> {
type Output = Result<T, JoinError>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe { self.task.poll(cx) }.map(|res| match res {
Some(Ok(res)) => Ok(res),
Some(Err(e)) => Err(JoinError::Panicked(e)),
None => Err(JoinError::Cancelled),
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
instrument!(compio_log::Level::TRACE, "JoinHandle::poll");

let task = self.task.as_ref().expect("Cannot poll after completion");

unsafe { task.poll(cx) }.map(|res| {
trace!("Poll ready");

self.task = None;

match res {
Some(Ok(res)) => Ok(res),
Some(Err(e)) => Err(JoinError::Panicked(e)),
None => Err(JoinError::Cancelled),
}
})
}
}
Expand All @@ -107,6 +130,8 @@ impl<T> Drop for JoinHandle<T> {
fn drop(&mut self) {
instrument!(compio_log::Level::TRACE, "JoinHandle::drop");

self.task.cancel(true);
if let Some(task) = self.task.as_ref() {
task.cancel(true);
}
}
}
11 changes: 8 additions & 3 deletions compio-executor/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{Shared, task::Task, util::assert_not_impl};

new_key_type! { pub struct TaskId; }

use compio_log::instrument;
use compio_log::{instrument, trace};
use slotmap::SlotMap;

use crate::UnsafeCell;
Expand Down Expand Up @@ -78,11 +78,14 @@ impl TaskQueue {
///
/// Must only be called by `Executor`.
pub unsafe fn clear(&self) {
instrument!(compio_log::Level::DEBUG, "clear");
instrument!(compio_log::Level::DEBUG, "TaskQueue::clear");

trace!("Clearing");

unsafe {
self.with_inner(|inner| {
if inner.map.is_empty() {
trace!("Map empty, return");
return;
}

Expand All @@ -92,8 +95,10 @@ impl TaskQueue {
inner.cold.tail = None;

for task in inner.map.drain().filter_map(|(_, i)| i.task) {
task.unset_shared();
trace!(?task, "Dropping task");

task.drop();
task.wait_for_scheduling();
}

debug_assert!(inner.map.is_empty());
Expand Down
2 changes: 0 additions & 2 deletions compio-executor/src/task/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ impl<'a> Local<'a> {
}

pub unsafe fn poll<T>(&self, cx: &mut Context<'_>) -> Poll<Option<PanicResult<T>>> {
instrument!(compio_log::Level::TRACE, "Local::poll", id = ?self.header().id);

let state = self.state();
trace!(?state);

Expand Down
78 changes: 47 additions & 31 deletions compio-executor/src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use compio_log::{debug, instrument, trace};
use compio_send_wrapper::SendWrapper;

use crate::{
AtomicPtr, PanicResult, Shared, UnsafeCell, hint,
AtomicPtr, PanicResult, Shared, UnsafeCell,
queue::TaskId,
task::{
local::Local,
Expand Down Expand Up @@ -237,25 +237,24 @@ impl Task {
trace!("Run future");
let res = unsafe { (header.vtable.run_future)(self.0, ctx) };
if res.is_ready() {
debug!("Ready");
let mut state = header.state.finish_running();

// JoinHandle is polling the state when we're running and has not finished
// cloning yet. It will stop as soon as they found out that we have
// finished running or when finished cloning, so it's fine to busy spin here for
// a short burst.
while state.is_setting_waker() {
hint::spin_loop();
state = header.state.load::<Strong>();
}
debug!("Task finished");
let state = header.state.finish_running();

trace!(?state, "Try to wake up JoinHandle");

if state.has_waker() {
// JoinHandle will not set another waker after this check since we have set the
// state to completed before with Release order: they will either observe it and
// get the result, or not observe it and enter SETTING_WAKER critical section.
if state.has_waker() && !state.is_setting_waker() {
trace!("Waking up JoinHandle");
header
.waker
.with_mut(|ptr| unsafe { (*ptr).assume_init_ref() }.wake_by_ref());
}
} else {
trace!("Pending");
}
trace!("Pending");

res
})
}
Expand All @@ -265,8 +264,14 @@ impl Task {
/// This function can only be called by `JoinHandle`.
pub unsafe fn poll<T>(&self, cx: &mut Context<'_>) -> Poll<Option<PanicResult<T>>> {
match self.view() {
Ok(local) => unsafe { local.poll(cx) },
Err(remote) => unsafe { remote.poll(cx) },
Ok(local) => unsafe {
instrument!(compio_log::Level::TRACE, "Task::poll", id = ?self.header().id, remote = false);
local.poll(cx)
},
Err(remote) => unsafe {
instrument!(compio_log::Level::TRACE, "Task::poll", id = ?self.header().id, remote = true);
remote.poll(cx)
},
}
}

Expand All @@ -275,8 +280,8 @@ impl Task {
// future, result and/or waker, but the memory will be deallocated when the
// reference count reaches 0.
//
// The shared pointer is kept valid until the Executor itself is dropped, to
// avoid use-after-free issues with concurrent wakers.
// The shared pointer is also set to null to prevent any further scheduling or
// waker setting.
//
// # Safety
//
Expand All @@ -287,11 +292,14 @@ impl Task {
let header = self.header();
debug_assert!(
header.tracker.valid(),
"drop_future should only be called by Executor"
"drop should only be called by Executor"
);
let state = header.state.set_dropped();

header.shared.store(ptr::null_mut(), Release);

Comment thread
George-Miao marked this conversation as resolved.
Resolved
if !state.is_completed() {
trace!("Dropping future");
// The task has not completed yet, drop future
unsafe { (header.vtable.drop_future)(self.0, false) };
}
Expand All @@ -300,24 +308,22 @@ impl Task {
// afterwards and drop the waker. Otherwise, we drop the waker here if
// it exists.
if state.has_waker() && !state.is_setting_waker() {
trace!("Dropping waker");
crate::panic_guard!();

header
.waker
.with_mut(|ptr| unsafe { drop_in_place(ptr.cast::<Waker>()) });
}

trace!("Completed");
}

/// Unset the shared pointer to prevent wakers from accessing it after
/// Executor drop. This must only be called by the Executor during its
/// drop.
pub(crate) fn unset_shared(&self) {
/// Wait for wakers to finish scheduling, if any. This is necessary for
/// `Executor` to drop `Shared` since scheduling requires it.
pub(crate) fn wait_for_scheduling(&self) {
let header = self.header();

// Set shared pointer to null with Release ordering so concurrent schedulers see
// it
header.shared.store(ptr::null_mut(), Release);

// Wait for any ongoing scheduling to complete.
// We MUST do this to prevent use-after-free when we drop Shared.
// This is safe in Executor::drop context because:
Expand All @@ -337,12 +343,10 @@ impl Task {
#[inline(always)]
fn view(&self) -> Result<Local<'_>, Remote<'_>> {
if self.header().tracker.valid() {
trace!("Local view");
// SAFETY: We have checked that the tracker is valid, so this must be the same
// thread as the task allocation is created on.
Ok(unsafe { Local::new(self.0) })
} else {
trace!("Remote view");
Err(Remote::new(self.0))
}
}
Expand All @@ -352,21 +356,33 @@ impl Drop for Task {
fn drop(&mut self) {
let header = self.header();
let state = header.state.dec();
trace!(old = ?state, "Task dropped");
trace!(?state, "Task dropped");
if state.count() > 1 {
return;
};

crate::panic_guard!();

debug_assert!(state.is_completed() | state.is_cancelled());
debug_assert!(!state.is_setting_waker());
debug_assert!(!state.has_waker());

// If the result is still present, drop it now
// This happens when JoinHandle was dropped/detached without taking the result
if state.has_result() {
unsafe { (header.vtable.drop_future)(self.0, true) };
}

// If the waker is still present, drop it now
// This happens when the Task is dropped when JoinHandle was setting waker
// remotely.
if state.has_waker() {
trace!("Dropping waker");

header
.waker
.with_mut(|ptr| unsafe { drop_in_place(ptr.cast::<Waker>()) });
}

trace!("Task deallocated");
// SAFETY: We have checked that the reference count is 0, so no other reference
// to the allocation exists and we can safely deallocate it; and deallocation is
Expand Down
Loading