Skip to content

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

@blackspherefollower

Description

@blackspherefollower

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));
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions