MicrosoftDocs / winrt-api

WinRT reference content for developing Microsoft Universal Windows Platform (UWP) apps
Creative Commons Attribution 4.0 International
227 stars 493 forks source link

Documentation Clarification Request: IAsyncOperation<TResult> Completed race condition? #2455

Closed charlesnicholson closed 5 months ago

charlesnicholson commented 5 months ago

Hello WinRT team!

First off, I want to say that the C++ WinRT interface to the UWP stuff is very natural and easy to use. Thanks for all of your work on it; it's very natural and pleasant to write C++20 code that uses the new APIs.

I'm working with the BluetoothLE components of UWP in WinRT C++20. Coroutines don't work in the context I'm working in, so I'm using the IAsyncOperation "Completed" property to attach a method to be called upon completion.

I'm curious what machinery prevents the race condition where the IAsyncOperation completes between when the asynchronous function is invoked, and when I attach a method to the Completed property. My assumption is that there are provisions for this that I'm having trouble finding, so I'm assuming this is a documentation (or dumb user) issue. :)

For example, this image is from https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/handle-events#delegate-types-for-asynchronous-actions-and-operations

Screenshot 2024-02-15 175543

In the above example, if the operation represented by async_op_with_progress completes before the Completed property is set with the lambda, does the lambda ever get called? If it doesn't, what pattern should be used to safely detect this race scenario in a heavily multi-threaded environment?

(The above example is not related to the BluetoothLE UWP API but behaves similarly and is possibly even more prone to races: Some of the APIs like GattCharacteristic.WriteValueAsync simply queue up data for transmission and complete quickly, without waiting for any network activity. It's easy to imagine Windows choosing to schedule the thread that enqueues the data before even returning to my thread, which would complete the operation before I've had a chance to set a Complete handler!)

Thanks in advance for reading.

stevewhims commented 5 months ago

Hi, @charlesnicholson, and thanks for filing this issue.

Hello, WinRT team!

Just for awareness, the feedback channel you're using here (Submit and view feedback for > This page) is for feedback about the documentation in this topic itself, as published. For example, if the content is incorrect or incomplete. So issues filed this way don't go to "the Windows Runtime team". That is, not to the team responsible for designing and implementing WinRT APIs. And they don't go to "the C++/WinRT team". That is, not to the team responsible for designing and implementing the WinRT language projection known as C++/WinRT. Instead, they go to folks who write/edit/publish documentation covering what they know about the aforementioned things; mostly based on info they get from the aforementioned folks. :)

Here, the question seems to be "Assuming there is a race condition in this situation, then how does the platform handle that and/or how should the developer handle it." So any race condition is currently hypothetical (although I can very much sympathize with your reasoning that one might exist). So my feeling is that this issue is a little more in the realm of a product question than an issue with the documentation (although, I might be wrong).

Anyway, I'd recommend clicking Submit and view feedback for > This product, and you'll be taken to Microsoft Q&A (more info about that below). I imagine you'll be able to copy-paste the markdown of the question.

Here are all of the developer support resources, for completeness.

I'll go ahead and close this issue now, since there's no immediate action that I can take in the documentation. You can always reply to a closed issue, or even re-open it if you feel it's been closed in error.

Thanks! -Steve

charlesnicholson commented 5 months ago

Hey @stevewhims - thanks for the response! That's the trouble with undocumented behavior; it leaves a lot to the imagination :)

I posted my question here https://stackoverflow.com/questions/78007860/iasyncoperation-completed-handler-race-conditions and @oldnewthing chimed in to confirm that there's no race condition and that it's simply a missing explanation in the docs. He said he'd make a note to improve the docs but I'm assuming that since this ticket has had no activity, that it's being tracked elsewhere.

So perhaps this turned out to be the right place to file the request, despite my organizational ignorance?

stevewhims commented 5 months ago

Cool, it sounds like @oldnewthing is tracking this already. I could go in now and just write "There's no race condition in the example above" (for the sake of expediency), but I think it'd be better to let @oldnewthing explain it better than I could. :)

-Steve