Skip to content

Conversation

@lucasfernog-crabnebula
Copy link
Contributor

@lucasfernog-crabnebula lucasfernog-crabnebula commented May 25, 2025

Motivation and Context

currently both RunningService::waiting and RunningService::cancel needs ownership of the RunningService instance
this makes it impossible to poll waiting() while other async task can cancel it

A cancellation_token() function is solves this, cloning the tokio_util::sync::CancellationToken
so we can dispatch a cancel from a separate task

How Has This Been Tested?

  • run a service
  • call cancellation_token() to grab its cancellation token
  • spawn a separate task to run RunningService::waiting() while other task calls cancel()

Breaking Changes

No breaking change

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

currently both RunningService::waiting and RunningService::cancel needs ownership of the RunningService instance, which makes it impossible to poll waiting() while other async task can cancel it

A split() function is a common pattern to solve this
@4t145
Copy link
Collaborator

4t145 commented May 25, 2025

Perhaps we can simply add a cancellation_token field on it, while remain the grop guard? So user can clone the cancellation token.

@lucasfernog-crabnebula
Copy link
Contributor Author

but DropGuard can't be cloned, unless you want to change how cancellation is handled (if you clone a cancellation token and keep a copy in the RunningService the drop will never be triggered - and currently it also needs an owned value)

@4t145
Copy link
Collaborator

4t145 commented May 26, 2025

I believe it still can be triggered, you can see the source code. https://docs.rs/tokio-util/0.7.15/src/tokio_util/sync/cancellation_token/guard.rs.html#22-26

@lucasfernog-crabnebula lucasfernog-crabnebula changed the title feat: add split method to RunningService feat: add cancellation_token method to RunningService May 26, 2025
@lucasfernog-crabnebula
Copy link
Contributor Author

@4t145 pushed

@lucasfernog-crabnebula
Copy link
Contributor Author

lucasfernog-crabnebula commented May 26, 2025

I explicitly added the cancellation token as part of the service struct because i don't want to call .disarm() on the drop guard - i want to preserve the "cancel on drop" functionality

alternatively we could also implement drop on our own

@4t145
Copy link
Collaborator

4t145 commented May 26, 2025

I explicitly added the cancellation token as part of the service struct because i don't want to call .disarm() on the drop guard - i want to preserve the "cancel on drop" functionality

alternatively we could also implement drop on our own

That's exactly what I meant

Copy link
Collaborator

@4t145 4t145 left a comment

Choose a reason for hiding this comment

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

LGTM

@4t145 4t145 merged commit 633d424 into modelcontextprotocol:main May 26, 2025
9 of 10 checks passed
@lucasfernog-crabnebula lucasfernog-crabnebula deleted the feat/running-service-split branch May 26, 2025 11:04
@github-actions github-actions bot mentioned this pull request Jul 2, 2025
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