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

Added a way to obtain an indication of the current task state #19

Merged
merged 4 commits into from
Mar 4, 2022
Merged

Added a way to obtain an indication of the current task state #19

merged 4 commits into from
Mar 4, 2022

Conversation

VictorKoenders
Copy link
Contributor

I ran into the issue a couple of times where I spawned a task with async_std::task::spawn, and I was interested in getting an indication of the state of this task.

In my specific case I'm spawning up a task that takes data from a TcpStream and sends it to a Sender. This task does not have a noticeable output, however I would like to periodically check if this task is still running and didn't panic or something.

let handle = task::spawn(receiver_loop(stream.clone(), self.sender.clone()));
self.stream = Some((
	stream,
	handle
));

// Periodically run this:
if let Some((_, handle)) = self.stream.as_mut() {
	if handle.task().state() == TaskState::Completed {
		// Something went wrong
		log_error("...");
		self.reconnect()
	}
}

@VictorKoenders
Copy link
Contributor Author

VictorKoenders commented Jan 10, 2022

Some options to remove the #[non_exhaustive]:

  • Have an exhaustive enum
  • Create a struct that contains the enum, with functions on it
  • Add #[doc(hidden)] __Nonexhaustive
  • Something else?

@taiki-e
Copy link
Collaborator

taiki-e commented Jan 22, 2022

I'm fine with bumping MSRV to 1.40.

@VictorKoenders
Copy link
Contributor Author

Rebased onto master

src/task.rs Outdated
Comment on lines 408 to 412
if state & (CLOSED | COMPLETED) != 0 {
TaskState::Completed
} else {
TaskState::Running
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Considering that TaskState is non_exhaustive, the user can really only rely on state==Completed (since state==Running may change its meaning in the future).

So I am starting to think that it might actually be preferable to have a method that returns a bool like is_completed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

std JoinHandle has a similar unstable feature, which appears to be planned to be renamed is_finished.
rust-lang/rust#94549

@VictorKoenders
Copy link
Contributor Author

Switched to .is_finished()

@taiki-e
Copy link
Collaborator

taiki-e commented Mar 4, 2022

LGTM. Could you revert MSRV bump?

Copy link
Collaborator

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

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

Thanks!

@taiki-e taiki-e merged commit 67d2a17 into smol-rs:master Mar 4, 2022
@taiki-e
Copy link
Collaborator

taiki-e commented Mar 4, 2022

Published in 4.2.0.

@VictorKoenders VictorKoenders deleted the task_state branch March 4, 2022 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants