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

fix(ws): fix #22037 - concurrent subscription requests #22309

Merged
merged 10 commits into from Apr 29, 2024

Conversation

pcriadoperez
Copy link
Contributor

@pcriadoperez pcriadoperez commented Apr 27, 2024

Any edits to this.clients, client.futures and client.subscriptions change to use ConcurrentyDictionary methods

@carlosmiei
Copy link
Collaborator

carlosmiei commented Apr 28, 2024

@pcriadoperez tryAdd is not available by using the interface, we need the proper type
image

@carlosmiei
Copy link
Collaborator

carlosmiei commented Apr 28, 2024

@pcriadoperez did you test the demo provided by the user? I get inconsistent results with this PR (sometimes we subscribe to both sometimes we don't)

    public class Demo22037
    {
        public async Task Run()
        {
            var exchange = new ccxt.pro.Bitget();
            exchange.apiKey = Environment.GetEnvironmentVariable("BITGET_APIKEY");
            exchange.secret = Environment.GetEnvironmentVariable("BITGET_SECRET");
            exchange.password = Environment.GetEnvironmentVariable("BITGET_PASSWORD");
            exchange.verbose = true;

            var task1 = Task.Run(async () =>
            {
                try
                {
                    Dictionary<string, object> _balanceParams = new Dictionary<string, object>();
                    _balanceParams.Add("type", "spot");
                    var balanceSpot = await exchange.WatchBalance(_balanceParams);
                    Console.WriteLine("spot received");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            });

            var task2 = Task.Run(async () =>
            {
                try
                {
                    Dictionary<string, object> _balanceParams = new Dictionary<string, object>();
                    _balanceParams.Add("type", "future");
                    var balanceFuture = await exchange.WatchBalance(_balanceParams);
                    Console.WriteLine("future received");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            });

            Task.WhenAll(task1, task2).Wait();
        }
    }

@pcriadoperez
Copy link
Contributor Author

haha these race conditions... 😅 After the first change, it seemed to be working and fixed one race condition but after your comment I tried putting in some Console.WriteLines and the second race condition appeared. With these changes I ran it several times and seems to be working consistently so far. Hopefully cought all of them

@carlosmiei
Copy link
Collaborator

@pcriadoperez Yeah I think it's working now

@carlosmiei carlosmiei self-assigned this Apr 29, 2024
@carlosmiei carlosmiei merged commit 4153572 into ccxt:master Apr 29, 2024
0 of 2 checks passed
kroitor pushed a commit that referenced this pull request Apr 29, 2024
* fix(ws): fix #22037 - concurrent subscription requests

* trigger build

* explitict cast

* fix second race condition

* add changes for watch multiple

* build fix

* build fix

---------

Co-authored-by: carlosmiei <[email protected]>

[ci skip]
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.

None yet

2 participants