cudaD3D9GetDevice is unreliable?

I have some code that relies on cudaD3D9GetDevice to determine, for each display, whether it is connected to an NVIDIA GPU or not. One of my screens is on an Intel GPU and sometimes cudaD3D9GetDevice returns a valid CUDevice for that screen. When I step through the code line by line, this doesn’t happen (it fails as it should). This all happens on the same thread, so this is not a race on my side.

This definitely looks like a bug in the driver. Is there a workaround?