Skip to content

Use Corouitne/Task id#431

Merged
loongs-zhang merged 1 commit intoacl-dev:masterfrom
loongs-zhang:add-id
Apr 1, 2026
Merged

Use Corouitne/Task id#431
loongs-zhang merged 1 commit intoacl-dev:masterfrom
loongs-zhang:add-id

Conversation

@loongs-zhang
Copy link
Copy Markdown
Member

// Describe your PR here; eg. Fixes #issueNo

Make sure that:

  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed.

@loongs-zhang loongs-zhang force-pushed the add-id branch 3 times, most recently from 4a6ddef to bf88689 Compare April 1, 2026 08:49
@loongs-zhang loongs-zhang changed the title Add Use Corouitne/Task id Apr 1, 2026
@loongs-zhang loongs-zhang requested a review from Copilot April 1, 2026 08:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to replace string-based coroutine/task identifiers with numeric IDs across the scheduler, coroutine pool, and event-loop APIs (including the C hook layer), reducing string handling and avoiding name lifetime/leak patterns.

Changes:

  • Switch coroutine cancellation/resumption and scheduler result keys from coroutine name (&str/String) to u64 coroutine IDs.
  • Switch task submission/join/cancellation from task name strings to u64 task IDs (including JoinHandle and the C hook bindings).
  • Minor selector registration fix (Result::or_else) and coroutine-pool stop/emptiness logic adjustments.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
hook/src/lib.rs Updates C-facing cancellation to use JoinHandle task IDs.
core/tests/scheduler.rs Updates scheduler cancel test to use coroutine IDs.
core/tests/co_pool.rs Updates pool cancel test to use task IDs.
core/src/scheduler.rs Converts internal syscall/cancel tracking and schedule results to coroutine IDs.
core/src/net/selector/mod.rs Makes register fallback lazy via or_else to avoid eager evaluation.
core/src/net/mod.rs Plumbs task ID through EventLoops::{submit_task, try_cancel_task} and JoinHandle.
core/src/net/join.rs Changes JoinHandle storage/validation from C string name to u64 task ID.
core/src/net/event_loop.rs Changes token derivation and resume path to use coroutine IDs / hashing.
core/src/coroutine/mod.rs Exposes a coroutine id() accessor.
core/src/coroutine/korosensei.rs Adds id: u64 to coroutine and initializes it from a hash of the name.
core/src/co_pool/task.rs Adds id: u64 to tasks; renames get_name() to name() and adds id().
core/src/co_pool/mod.rs Converts pool tracking/results/waits/cancel structures to task IDs; tweaks stop loop and is_empty().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/src/net/event_loop.rs Outdated
Comment thread core/src/net/event_loop.rs Outdated
Comment thread core/src/coroutine/mod.rs Outdated
Comment thread core/src/net/join.rs
Comment on lines 21 to +38
pub(crate) fn err(pool: &'static Arc<EventLoop<'static>>) -> Self {
Self::new(pool, "")
Self::new(pool, 0)
}

/// create `JoinHandle` instance.
pub(crate) fn new(pool: &'static Arc<EventLoop<'static>>, name: &str) -> Self {
let boxed: &'static mut CString = Box::leak(Box::from(
CString::new(name).expect("init JoinHandle failed!"),
));
let cstr: &'static CStr = boxed.as_c_str();
JoinHandle(pool, cstr.as_ptr())
pub(crate) fn new(pool: &'static Arc<EventLoop<'static>>, task_id: u64) -> Self {
JoinHandle(pool, task_id)
}

/// get the task name.
/// get the task id.
///
/// # Errors
/// if the task name is invalid.
pub fn get_name(&self) -> std::io::Result<&str> {
unsafe { CStr::from_ptr(self.1) }
.to_str()
.map_err(|_| Error::new(ErrorKind::InvalidInput, "Invalid task name"))
/// if the task id is invalid.
pub fn id(&self) -> std::io::Result<u64> {
if 0 == self.1 {
return Err(Error::new(ErrorKind::InvalidInput, "Invalid task id"));
}
Ok(self.1)
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JoinHandle treats task_id == 0 as invalid, but task IDs are now generated via hashing, which can legitimately produce 0. This would make a valid task un-joinable and prevent cleanup in Drop. Consider using Option<u64>/NonZeroU64 for the sentinel, or ensure generated task IDs are never 0 (e.g., remap 0 to another value).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread hook/src/lib.rs Outdated
Comment thread core/src/co_pool/task.rs
Comment on lines +30 to 35
let mut hasher = DefaultHasher::new();
name.hash(&mut hasher);
let id = hasher.finish();
Task {
id,
name,
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Task IDs are derived from DefaultHasher(name), which is not collision-free. Because task_id is now used as the unique key in global maps (RUNNING_TASKS, CANCEL_TASKS, results, waits), a collision would cause incorrect cancellation/result delivery. Prefer generating IDs from a monotonic counter/UUID (or at least detect and resolve collisions when inserting into maps).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment on lines +432 to 438
let mut hasher = DefaultHasher::new();
name.hash(&mut hasher);
let id = hasher.finish();
#[allow(unused_mut)]
let mut co = Coroutine {
id,
name,
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coroutine IDs are derived from DefaultHasher(name). Since co_id is used as the unique key for cancellation, syscall tables, and resumption, any hash collision would conflate two coroutines. Prefer a collision-free ID source (e.g., atomic counter or UUID bytes) instead of a hash, or add collision detection when inserting into syscall/RUNNING_COROUTINES/CANCEL_COROUTINES.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread core/src/co_pool/mod.rs
Comment thread core/src/co_pool/task.rs Outdated
@loongs-zhang
Copy link
Copy Markdown
Member Author

@copilot go to work base on our conversation, fix the bugs I marked and need your feedback.

@loongs-zhang loongs-zhang merged commit 5fabaa8 into acl-dev:master Apr 1, 2026
14 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants