Add get_unchecked() to async operations - #1565
Conversation
|
Not the hugest fan of the very_long_function_name but I don't have a better answer quite yet. Things that come to mind:
|
|
I think get_unchecked() is probably the best one, it immediately flags to the reader this requires extra carefulness while not being overly verbose. |
|
How about naming it |
There was a problem hiding this comment.
Pull request overview
This PR adds a new synchronous “get-like” API across the four WinRT async interfaces that intentionally bypasses the _DEBUG-only STA blocking assert, enabling blocking waits from STAs that are not presenting UI.
Changes:
- Added
get_only_safe_from_non_presenting_sta()to the consume extensions forIAsyncAction,IAsyncOperation<T>,IAsyncActionWithProgress<T>, andIAsyncOperationWithProgress<T, P>. - Added an internal helper
impl::wait_get_bypass_sta_check()mirroring the existing.get()wait/get behavior but skipping the STA assert. - Added a
test_nocorotest that invokes the new method from an STA thread usingWindows::Storage::PathIO::ReadTextAsync.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/test_nocoro/pch.h | Adds Windows.Storage include to support the new test’s WinRT API usage. |
| test/test_nocoro/get.cpp | Adds an STA-threaded test case exercising get_only_safe_from_non_presenting_sta(). |
| strings/base_coroutine_foundation.h | Implements the bypass helper and wires the new method into async consume templates. |
| cppwinrt/code_writers.h | Updates generated interface extension declarations to include the new method. |
Add a peer to .get() on IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, and IAsyncOperationWithProgress that skips the _DEBUG-only STA blocking assert. The existing .get() asserts !is_sta_thread() to guard against blocking UI threads. However, not all STAs are UI threads — some never present UI, haven't presented yet, or never will. The assert is also _DEBUG-only, making it invisible to codebases that don't build with _DEBUG (e.g. the Windows OS). The new method get_only_safe_from_non_presenting_sta() is functionally identical to .get() but omits the STA check. The intentionally long name communicates the risk to callers. Changes: - strings/base_coroutine_foundation.h: Add wait_get_bypass_sta_check() impl helper and get_only_safe_from_non_presenting_sta() for all 4 async consume templates - cppwinrt/code_writers.h: Add declaration to generated code for all 4 async types - test/test_nocoro: Add test calling the new method from an STA thread using a real WinRT async operation (PathIO::ReadTextAsync on C:\Windows\win.ini) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Seems good; the description says |
Dustin L. Howett (DHowett)
left a comment
There was a problem hiding this comment.
wait, I think Jon's observation cuts the other way. the code has the weird long name, the description has the agreed-upon short name?
|
Yeah, the title and description was just recently updated. I suppose a commit to use the new name is upcoming? |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
41e6fe7 to
e73140a
Compare
Summary
Add
get_unchecked()as a peer to.get()on all four WinRT async interfaces (IAsyncAction,IAsyncOperation,IAsyncActionWithProgress,IAsyncOperationWithProgress). It synchronously waits for the operation and returns the same result as.get(), but deliberately skips the STA blocking assertion.Motivation
The existing
.get()asserts!is_sta_thread()to guard against blocking a UI thread. However:_DEBUG-only —WINRT_ASSERTis a no-op in release builds, making this protection invisible to codebases that do not build with_DEBUG(for example, the Windows OS).get_unchecked()has the same synchronous wait and result/error behavior as.get(). Its only behavioral difference is that it does not call the STA check before waiting. Because blocking an STA that owns or presents UI can deadlock, callers must use this method only when they know the STA is non-presenting and the wait is safe.Changes
strings/base_coroutine_foundation.h: Add thewait_get_bypass_sta_check()implementation helper andget_unchecked()definitions for all four async consume templatescppwinrt/code_writers.h: Add theget_unchecked()declaration to generated output for all four async types, with a comment documenting the safety contracttest/test_nocoro: Add a test that callsget_unchecked()from an STA thread using a real WinRT async operation (PathIO::ReadTextAsynconC:\Windows\win.ini)Testing
The existing STA-specific test exercises the new API on a non-presenting STA and verifies that the asynchronous operation completes successfully. A local
test_nocorobuild with the newest MSVC toolset is currently blocked by the toolchain's hard error for the deprecated<experimental/coroutine>header; the repository's C++17/await:strictconfiguration needs a separate compatibility fix.