Skip to content

api: replace use of DevCreateObjectQuery with CM_Register_Notifcation…#7

Closed
dblohm7 wants to merge 1 commit intoWireGuard:masterfrom
dblohm7:adapter_inst_event
Closed

api: replace use of DevCreateObjectQuery with CM_Register_Notifcation…#7
dblohm7 wants to merge 1 commit intoWireGuard:masterfrom
dblohm7:adapter_inst_event

Conversation

@dblohm7
Copy link
Copy Markdown

@dblohm7 dblohm7 commented Mar 23, 2023

… and ensure that the device interface list is explicitly queried immediately after notification registration

Tailscale has several open issues concerning WinTun adapter installations that hang indefinitely, stuck waiting on an event that never triggers. Based on analysis of Tailscale logs and examination of WinTun's adapter installation code, I believe we are seeing a race condition that only manifests under specific timing scenarios that are relatively infrequent for most users, but are common for some.

It is hard to reason about DevCreateObjectQuery because it is an undocumented API. Based upon my reading of the existing code and examination of existing documentation, it looks like CM_Register_Notification is the documented counterpart. Its documentation makes it pretty clear[1] that these notifications are essentially edge-triggered; we need to call CM_Get_Device_Interface_List immediately after registering the notification to ensure that the arrival event has not already happened.

This patch replaces DevCreateObjectQuery with CM_Register_Notification because using a documented API gives us a better understanding of the semantics involved and makes this code easier to reason about. Then we add a call to AdapterGetDeviceObjectFileName to handle the aforementioned explicit check, thus ensuring that we are not waiting on an event that might have occurred prior to registering for event notifications.

[1] https://learn.microsoft.com/en-us/windows/win32/api/cfgmgr32/nf-cfgmgr32-cm_register_notification#remarks

… and ensure that the device interface list is explicitly queried immediately after notification registration

Tailscale has several open issues concerning WinTun adapter
installations that hang indefinitely, stuck waiting on an event that
never triggers. Based on analysis of Tailscale logs and examination of WinTun's
adapter installation code, I believe we are seeing a race condition that only
manifests under specific timing scenarios that are relatively infrequent for
most users, but are common for some.

It is hard to reason about DevCreateObjectQuery because it is an
undocumented API. Based upon my reading of the existing code and examination of
existing documentation, it looks like CM_Register_Notification is the documented
counterpart. Its documentation makes it pretty clear[1] that these
notifications are essentially edge-triggered; we need to call
CM_Get_Device_Interface_List immediately after registering the
notification to ensure that the arrival event has not already happened.

This patch replaces DevCreateObjectQuery with CM_Register_Notification
because using a documented API gives us a better understanding of the semantics
involved and makes this code easier to reason about. Then we add a call to
AdapterGetDeviceObjectFileName to handle the aforementioned explicit
check, thus ensuring that we are not waiting on an event that might have
occurred prior to registering for event notifications.

[1] https://learn.microsoft.com/en-us/windows/win32/api/cfgmgr32/nf-cfgmgr32-cm_register_notification#remarks

Signed-off-by: Aaron Klotz <aaron@tailscale.com>
@lezzi
Copy link
Copy Markdown

lezzi commented Aug 31, 2024

Hey @dblohm7 👋 . Were you ever able to somehow reproduce this scenario?

@xwwang
Copy link
Copy Markdown

xwwang commented Oct 25, 2024

I started to use WireGuard thoes months, and calling WireGuardCreateAdapter might fail by accident. The error message is: "Timed out waiting for device query". It seems like the device create event is missing and result in WaitForSingleObject interface timeout.

Most times recall WireGuardCreateAdapter will success, but rarely recall didn't work and I should restart the OS.

It's hard to reproduce this problem Cause I don't know how it happened.....

@dblohm7 Am I caught in the same situation which you're trying to fix? If so I'd like to pull this commit and see whether it works.

@zx2c4-bot zx2c4-bot force-pushed the master branch 2 times, most recently from 4937075 to dd05092 Compare February 20, 2026 13:31
@WireGuard WireGuard deleted a comment from durongze May 7, 2026
@zx2c4
Copy link
Copy Markdown
Member

zx2c4 commented May 7, 2026

@dblohm7 I tried this here: https://git.zx2c4.com/wireguard-nt/commit/?id=94fc6a4b8dcf82ef64bb451a38e79746f53e7407 as a candidate to porting to wintun (a refresh is coming soon I hope), and it seemed to work well. But then I tried on the 1507 build of Windows, and it fell over:

2026-05-07 14:15:26.986: [TUN] [herpderp] Creating network adapter
2026-05-07 14:15:27.130: [TUN] [herpderp] Using existing driver 1.1
2026-05-07 14:15:27.135: [TUN] [herpderp] Creating adapter
2026-05-07 14:15:27.151: [TUN] [herpderp] Failed to query registry value \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\Class\{62f9c741-b25a-46ce-b54c-9bccce08b6f2}\0005\NetCfgInstanceId: The system cannot find the file specified. (Code 0x00000002)
2026-05-07 14:15:27.151: [TUN] [herpderp] Failed to get \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\Class\{62f9c741-b25a-46ce-b54c-9bccce08b6f2}\0005\NetCfgInstanceId
2026-05-07 14:15:27.151: [TUN] [herpderp] Failed to populate adapter data
2026-05-07 14:15:27.503: [TUN] [herpderp] Unable to create network adapter: Error creating adapter: The system cannot find the file specified.
2026-05-07 14:15:27.513: [TUN] [herpderp] Shutting down

So I'm backing this change out. It would appear it also needs some registry waiting or some other notification mechanism.

@zx2c4 zx2c4 closed this May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants