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

Windows 11 WinRT initialization failure #31

Closed
Andrey1994 opened this issue Nov 4, 2021 · 46 comments
Closed

Windows 11 WinRT initialization failure #31

Andrey1994 opened this issue Nov 4, 2021 · 46 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@Andrey1994
Copy link
Contributor

Andrey1994 commented Nov 4, 2021

After updating from Windows 10 to Windows 11 in Python I get a deadlock(never ends, 0% CPU usage). Its weird that I cannot reproduce it in C++

Connected with a debugger to python and got that call stack:

image

In _attempt_connect method at GetGattServicesAsync async_get never ends due to some reason.

Not a solution for this particular issue but maybe it makes sense to add an option to configure the timeout for waiting? Non-zero exit code or exception is definitely better than deadlock

@Andrey1994

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald kdewald added bug Something isn't working help wanted Extra attention is needed labels Dec 19, 2021
@Andrey1994

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@Andrey1994

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@kdewald

This comment was marked as outdated.

@kdewald
Copy link
Member

kdewald commented Jul 1, 2022

Considering it has been 6 months since this issue was last updated and new exception handling will be part of v0.5.0. I think we can close this issue. Feel free to open a new one if problems arise.

@kdewald kdewald closed this as completed Jul 1, 2022
@Andrey1994
Copy link
Contributor Author

Actually it still doesnt work for me and I have no ideas how to investigate it further. And I am still not sure if its only for my laptop or not

@kdewald
Copy link
Member

kdewald commented Jul 9, 2022

So far I haven't been able to replicate it and no other reports have come in about this. I suspect there might be some weird deadlock or concurrency issue that could be affecting, but there are plenty of additional checks right now to prevent this stuff from happening. Are you using the current commit at the tip of main? That one should be the most stable version.

@Andrey1994
Copy link
Contributor Author

Using the latest commit instead deadlock and winrt exception I see that it fails to connect and its definitely better.

But I reproduce it 100% times and in exactly the same place and only when I call it from python. It works for all other languages.

@Andrey1994
Copy link
Contributor Author

Andrey1994 commented Jul 9, 2022

I think nobody else reported it probably because more likely not many people tried to call it from python like this and using win11..

@Andrey1994
Copy link
Contributor Author

Andrey1994 commented Jul 9, 2022

btw it connects using python_simpleble and connect.py example

@kdewald
Copy link
Member

kdewald commented Jul 9, 2022

Can you link again the latest version of the code where this is happening? I think we have some multi-threading problems happening.

@Andrey1994

This comment was marked as outdated.

@Andrey1994
Copy link
Contributor Author

simpleble_peripheral_connect fails with timeout in _attempt_connect and async_get

@kdewald
Copy link
Member

kdewald commented Jul 11, 2022

Can you print the output that you get from src/windows/Utils.cpp in the initialize_winrt function? I suspect there's an initialization error there.

@kdewald
Copy link
Member

kdewald commented Jul 11, 2022

Also, in Utils.h you should check what error is returned in the catch statements of async_get, that could also give a clue.

@Andrey1994
Copy link
Contributor Author

@kdewald thanks for the hint.
I added:

     WINRT_IMPL_CoGetApartmentType(&cotype, &qualifier);

+    std::cerr << "cotype is " << cotype << "qualifier " << qualifier << std::endl;
+
     if (cotype == -1 /* APTTYPE_CURRENT */) {
         // TODO: Investigate if multi or single threaded initialization is needed.
         winrt::apartment_type const type = winrt::apartment_type::multi_threaded;
         winrt::hresult const result = WINRT_IMPL_CoInitializeEx(nullptr, static_cast<uint32_t>(type));
+        std::cerr << (long)result << std::endl;
     }

And:

        return async.GetResults();
     } catch (const winrt::hresult_error& err) {
+        std::cerr << (long)err.code().value << " " << winrt::to_string(err.message()) << std::endl;
         throw SimpleBLE::Exception::WinRTException(err.code().value, winrt::to_string(err.message()));
     } catch (const std::exception& err) {
+        std::cerr << err.what() << std::endl;

The output is:

cotype is 3qualifier 0
-2147483634 A method was called at an unexpected time. // this one from attempt_connect

So, WINRT_IMPL_CoInitializeEx is not even called

@Andrey1994
Copy link
Contributor Author

for other languages cotype is -1 and its executed. But I have not checked other languages with GIL...

@Andrey1994
Copy link
Contributor Author

I've tried to force call it wo if check for single threaded and multiple threaded and it didnt work either

@kdewald
Copy link
Member

kdewald commented Jul 13, 2022

I think you need to call that function from within your library in the main thread. I'll keep looking to see if anything else comes up.

@kdewald kdewald reopened this Jul 13, 2022
@kdewald kdewald changed the title Windows 11 python deadlock Windows 11 WinRT initialization failure Jul 13, 2022
@kdewald
Copy link
Member

kdewald commented Jul 13, 2022

Also, what Python version are you using? That could also be related.

@Andrey1994
Copy link
Contributor Author

its in the main thread. Since simpleble creates its own threads for callbacks,etc I dont need to create any other threads manually
python 3.9.6 x64

@Andrey1994
Copy link
Contributor Author

wow, it works if I use 3.10.5! Thanks a lot I was struggling with it for months!

@eriklins
Copy link
Contributor

eriklins commented Sep 1, 2023

Hey all, seems I'm running into the same issue now with my InsideBlue tool, which use my Pascal bindings. Got a new laptop running Windows 11 and I can no longer connect to any BLE device from InsideBlue. Scanning works as usual but connecting attempt takes long and finally fails everytime. I recompiled the SimpleBLE DLLs with the latest pull from github on Windows 11, but no change. Since the issue has been close quite a while back, what did fix the issue with Python? Any other clues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants