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

Cancelation token on ConnectAsync will kill a connection after it's returned #685

Open
blackspherefollower opened this issue Nov 8, 2023 · 1 comment

Comments

@blackspherefollower
Copy link
Contributor

Given the very simple WinForms example:

using System;
using System.Threading;
using System.Windows.Forms;
using Buttplug.Client;
using Buttplug.Client.Connectors.WebsocketConnector;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        ButtplugWebsocketConnector myConnector;
        ButtplugClient client = new ButtplugClient("Test Client");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var cts = new CancellationTokenSource();
            cts.CancelAfter(TimeSpan.FromSeconds(5));
            client.ConnectAsync(new ButtplugWebsocketConnector(new Uri("ws://127.0.0.1:12345")), cts.Token).GetAwaiter().GetResult();
        }
    }
}

The ConnectAsync() will succeed and show in intiface as connected, but will then disconnect when the token times out.

Workaround: After return, extend the timeout:

        private void button1_Click(object sender, EventArgs e)
        {
            var cts = new CancellationTokenSource();
            cts.CancelAfter(TimeSpan.FromSeconds(5));
            client.ConnectAsync(new ButtplugWebsocketConnector(new Uri("ws://127.0.0.1:12345")), cts.Token).GetAwaiter().GetResult();
            cts.CancelAfter(TimeSpan.FromDays(5));
        }
@qdot
Copy link
Member

qdot commented Nov 8, 2023

The websocket uses the user provided token to shut down its event loop on cancel, which shouldn't happen. It should use a token owned by the client, not one passed in by the user.

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

No branches or pull requests

2 participants