Skip to contentSkip to conversation

api: use mutex when removing instance on close adapter

PATCH In review Jean-Alexandre CHRONE 1 participant 2 messages Feb 8 – Mar 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 api: use mutex when removing instance on close adapter

From 298267d3ead7110880a5dc0f5d0e8822600d30c0 Mon Sep 17 00:00:00 2001
From: "jean-alexandre.chrone" <[email protected]>
Date: Mon, 2 Feb 2026 15:24:12 +0100
Subject: [PATCH] api: use mutex when removing instance on close adapter

on successive create and close there might be some error logged

---
 api/adapter.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
api/adapter.c +10 -1
index 0dd8c42..042645c 100644
@@ -158,7 +158,8 @@ VOID AdapterCleanupOrphanedDevices(VOID)
             continue;
         }
         ULONG Status, Code;
-        if (CM_Get_DevNode_Status(&Status, &Code, DevInfoData.DevInst, 0) == CR_SUCCESS && !(Status & DN_HAS_PROBLEM))
+        CONFIGRET Ret = CM_Get_DevNode_Status(&Status, &Code, DevInfoData.DevInst, 0);
+        if ((Ret == CR_SUCCESS && !(Status & DN_HAS_PROBLEM)) || Ret == CR_NO_SUCH_DEVNODE)
             continue;
 
         DEVPROPTYPE PropType;
@@ -195,8 +196,16 @@ WintunCloseAdapter(WINTUN_ADAPTER *Adapter)
         SwDeviceClose(Adapter->SwDevice);
     if (Adapter->DevInfo)
     {
+        HANDLE DeviceInstallationMutex = NamespaceTakeDeviceInstallationMutex();
+        if (DeviceInstallationMutex == NULL)
+            LOG_LAST_ERROR(L"Failed to take device installation mutex");
+
         if (!AdapterRemoveInstance(Adapter->DevInfo, &Adapter->DevInfoData))
             LOG_LAST_ERROR(L"Failed to remove adapter when closing");
+
+        if (DeviceInstallationMutex != NULL)
+            NamespaceReleaseMutex(DeviceInstallationMutex);
+
         SetupDiDestroyDeviceInfoList(Adapter->DevInfo);
     }
     Free(Adapter);
Signature · 1 line
2.52.0.windows.1
#1 lore raw

Hello,

Did anyone have the same issue as mentioned in the earlier message?
We encountered it any time we had to reset an adapter multiple times in a short lapse of time.

Best Regards,
Jean-Alexandre

-----Original message-----

From 298267d3ead7110880a5dc0f5d0e8822600d30c0 Mon Sep 17 00:00:00 2001
From: "jean-alexandre.chrone" <[email protected]>
Date: Mon, 2 Feb 2026 15:24:12 +0100
Subject: [PATCH] api: use mutex when removing instance on close adapter

on successive create and close there might be some error logged

---
 api/adapter.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
api/adapter.c +3 -1
@@ -158,7 +158,8 @@ VOID AdapterCleanupOrphanedDevices(VOID)
             continue;
         }
         ULONG Status, Code;
-        if (CM_Get_DevNode_Status(&Status, &Code, DevInfoData.DevInst, 0) == CR_SUCCESS && !(Status & DN_HAS_PROBLEM))
+        CONFIGRET Ret = CM_Get_DevNode_Status(&Status, &Code, DevInfoData.DevInst, 0);
+        if ((Ret == CR_SUCCESS && !(Status & DN_HAS_PROBLEM)) || Ret == 
+ CR_NO_SUCH_DEVNODE)
             continue;
 
         DEVPROPTYPE PropType;
@@ -195,8 +196,16 @@ WintunCloseAdapter(WINTUN_ADAPTER *Adapter)
         SwDeviceClose(Adapter->SwDevice);
     if (Adapter->DevInfo)
     {

+ HANDLE DeviceInstallationMutex = NamespaceTakeDeviceInstallationMutex();
+ if (DeviceInstallationMutex == NULL)
+ LOG_LAST_ERROR(L"Failed to take device installation
+ mutex");
+

         if (!AdapterRemoveInstance(Adapter->DevInfo, &Adapter->DevInfoData))
             LOG_LAST_ERROR(L"Failed to remove adapter when closing");

+
+ if (DeviceInstallationMutex != NULL)
+ NamespaceReleaseMutex(DeviceInstallationMutex);
+

         SetupDiDestroyDeviceInfoList(Adapter->DevInfo);
     }
     Free(Adapter);

--
2.52.0.windows.1