[Connection] Fix race condition (#5154)
authorchleun-moon <32117100+chleun-moon@users.noreply.github.com>
Wed, 5 Apr 2023 02:42:07 +0000 (11:42 +0900)
committerGitHub <noreply@github.com>
Wed, 5 Apr 2023 02:42:07 +0000 (11:42 +0900)
* [Connection] Fix race condition

* Add locks

* Use lock objects

src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs

index 41dbedc..a622a28 100644 (file)
@@ -78,6 +78,12 @@ namespace Tizen.Network.Connection
         private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
         private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
 
+        private static readonly object _ConnectionTypeChangedLock = new object();
+        private static readonly object _IPAddressChangedLock = new object();
+        private static readonly object _EthernetCableStateChangedLock = new object();
+        private static readonly object _ProxyAddressChangedLock = new object();
+
+
         private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
         private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
         private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
@@ -126,32 +132,38 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_ConnectionTypeChanged == null)
+                lock (_ConnectionTypeChangedLock)
                 {
-                    try
-                    {
-                        ConnectionTypeChangedStart();
-                    }
-                    catch (Exception e)
+                    if (_ConnectionTypeChanged == null)
                     {
-                        Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
-                        return;
+                        try
+                        {
+                            ConnectionTypeChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
+                            return;
+                        }
                     }
+                    _ConnectionTypeChanged += value;
                 }
-                _ConnectionTypeChanged += value;
             }
             remove
             {
-                _ConnectionTypeChanged -= value;
-                if (_ConnectionTypeChanged == null)
+                lock (_ConnectionTypeChangedLock)
                 {
-                    try
-                    {
-                        ConnectionTypeChangedStop();
-                    }
-                    catch (Exception e)
+                    _ConnectionTypeChanged -= value;
+                    if (_ConnectionTypeChanged == null)
                     {
-                        Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
+                        try
+                        {
+                            ConnectionTypeChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
+                        }
                     }
                 }
             }
@@ -191,32 +203,38 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_EthernetCableStateChanged == null)
+                lock(_EthernetCableStateChangedLock)
                 {
-                    try
-                    {
-                        EthernetCableStateChangedStart();
-                    }
-                    catch (Exception e)
+                    if (_EthernetCableStateChanged == null)
                     {
-                        Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
-                        return;
+                        try
+                        {
+                            EthernetCableStateChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
+                            return;
+                        }
                     }
+                    _EthernetCableStateChanged += value;
                 }
-                _EthernetCableStateChanged += value;
             }
             remove
             {
-                _EthernetCableStateChanged -= value;
-                if (_EthernetCableStateChanged == null)
+                lock(_EthernetCableStateChangedLock)
                 {
-                    try
-                    {
-                        EthernetCableStateChangedStop();
-                    }
-                    catch (Exception e)
+                    _EthernetCableStateChanged -= value;
+                    if (_EthernetCableStateChanged == null)
                     {
-                        Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
+                        try
+                        {
+                            EthernetCableStateChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
+                        }
                     }
                 }
             }
@@ -260,33 +278,39 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_IPAddressChanged == null)
+                lock (_IPAddressChangedLock)
                 {
-                    try
-                    {
-                        IPAddressChangedStart();
-                    }
-                    catch (Exception e)
+                    if (_IPAddressChanged == null)
                     {
-                        Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
-                        return;
+                        try
+                        {
+                            IPAddressChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
+                            return;
+                        }
                     }
+                    _IPAddressChanged += value;
                 }
-                _IPAddressChanged += value;
             }
 
             remove
             {
-                _IPAddressChanged -= value;
-                if (_IPAddressChanged == null)
+                lock (_IPAddressChangedLock)
                 {
-                    try
+                    _IPAddressChanged -= value;
+                    if (_IPAddressChanged == null)
                     {
-                        IPAddressChangedStop();
-                    }
-                    catch (Exception e)
-                    {
-                        Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
+                        try
+                        {
+                            IPAddressChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
+                        }
                     }
                 }
             }
@@ -330,32 +354,38 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_ProxyAddressChanged == null)
+                lock (_ProxyAddressChangedLock)
                 {
-                    try
+                    if (_ProxyAddressChanged == null)
                     {
-                        ProxyAddressChangedStart();
-                    }
-                    catch (Exception e)
-                    {
-                        Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
-                        return;
+                        try
+                        {
+                            ProxyAddressChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
+                            return;
+                        }
                     }
+                    _ProxyAddressChanged += value;
                 }
-                _ProxyAddressChanged += value;
             }
             remove
             {
-                _ProxyAddressChanged -= value;
-                if (_ProxyAddressChanged == null)
+                lock (_ProxyAddressChangedLock)
                 {
-                    try
+                    _ProxyAddressChanged -= value;
+                    if (_ProxyAddressChanged == null)
                     {
-                        ProxyAddressChangedStop();
-                    }
-                    catch (Exception e)
-                    {
-                        Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
+                        try
+                        {
+                            ProxyAddressChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
+                        }
                     }
                 }
             }