Beckhoff / TF6000_ADS_DOTNET_V5_Samples

Sample code for the Version 6.X series of the TwinCAT ADS .NET Packages
https://infosys.beckhoff.com/content/1033/tc3_ads.net/9407515403.html?id=6770980177009971601
BSD Zero Clause License
37 stars 15 forks source link

AdsServer ConnectServer throws exception on parallel Connect #31

Closed enzo-ae closed 1 year ago

enzo-ae commented 2 years ago

When I try to run the AdsClient Connect method in parallel, the following exception is thrown:

AmsConnect: Cannot register Port '0' (Error: ClientPortNotOpen)" at TwinCAT.Ads.Server.AdsServer.ConnectServer() at TwinCAT.Ads.AdsClient.Connect(AmsAddress address)

Test-Code to reproduce:

using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using TwinCAT.Ads;
using TwinCAT.Ads.Server;
using Xunit;

namespace Beckhoff.TwinCAT.Ads;

public class AdsTest
{
    [Fact]
    public void ParallelTest()
    {
        var addresses = new List<AmsAddress>()
        {
            new AmsAddress("127.0.0.1.1.1", 851),
            new AmsAddress("127.0.0.1.1.1", 851),
        };

        new AdsClient().Invoking(client =>
        {
            addresses.AsParallel().Select(address =>
            {
                client.Connect(address);
                return true;
            }).Count();
        }).Should().NotThrow<AdsServerException>();
    }
}
MichaelKno commented 1 year ago

We have optimized the behavior. Can you please test with the current release? (V6.0.194)