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

api: replace use of DevCreateObjectQuery with CM_Register_Notifcation… #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dblohm7
Copy link

@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant