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

WriteAsync does not use the CancellationToken that is given to it #824

Open
minno726 opened this issue Mar 6, 2024 · 4 comments
Open

WriteAsync does not use the CancellationToken that is given to it #824

minno726 opened this issue Mar 6, 2024 · 4 comments
Labels

Comments

@minno726
Copy link

minno726 commented Mar 6, 2024

Steps to reproduce

  1. Call WriteAsync by passing a CancellationToken to it.

  2. Before the call completes, cancel the token. This is easiest to do by passing a very small timeout in the constructor for CancellationTokenSource.

Expected behavior

The operation should be canceled.

Actual behavior

The operation ignores the cancellation.

Configuration

Version of the Plugin: 3.0.0

Platform: Observed on iOS due to #785, but the problem is in shared code.

The source of the problem is here:

public Task WriteAsync(byte[] data, CancellationToken cancellationToken = default)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
return WriteNativeAsync(data);
}

The code does not use the cancellation token that was passed in as a function parameter. See the documentation on how to use cancellation tokens here: https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads, or remove the parameter if cancellation isn't practical to implement.

This is related to #785, since it prevents one workaround for that issue from working.

@smsissuechecker
Copy link

Hi @minno726,

I'm the friendly issue checker.
It seems like (25.00 %) you haven't used our issue template 😢 I think it is very frustrating for the repository owners, if you ignore them.

If you think it's fine to make an exception, just ignore this message.
But if you think it was a mistake to delete the template, please close the issue and create a new one.

Thanks!

@ygl-rg
Copy link

ygl-rg commented Mar 21, 2024

public static async Task<int> SendBytes(this ICharacteristic handle, byte[] data, CancellationToken token)
    {
        var cs = new TaskCompletionSource();
        token.Register(() =>
        {
            cs.TrySetResult();
        });
        var t1 = handle.WriteAsync(data, token);
        var t = await Task.WhenAny(cs.Task, t1);
        if (t == cs.Task)
            throw new TimeoutException("Send bytes timeout");
        return await t1;
    }

I use the above method for cancellation.

@bcaceiro
Copy link

bcaceiro commented May 21, 2024

Hi @janusw , are you planning on addressing this issue? We have a temp workaround, however would be best if the cancellation would respect the given CancellationToken. And great work on this package btw :)

@janusw
Copy link
Member

janusw commented May 25, 2024

Hi @janusw , are you planning on addressing this issue?

To be honest, I'm currently not planning to look into this myself (due to time constraints), but if someone is willing to contribute a PR, I'd be happy to review it and provide support.

In general, I'm not actully understanding my maintainer role here as "someone who is responsible for fixing all the bugs", but rather as "someone who is helping others to help themselves". 😄

And great work on this package btw :)

Thanks, but this compliment certainly does not go to me alone. A lot of people have contributed to this library over the years, and I'm only one of them. It's not "my" package in any way, it's a shared community effort.

My advice: If you need something fixed quickly, the best idea is usually to get involved yourself. 😉
(Or, in case you don't have the time or the skills for contributing a fix, but have some budget to spend: Find someone who can fix it, and pay him for it.)

@janusw janusw added the os:All label May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants