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

解决老中医提出的不能 syncAwait 然后捕获异常的问题。测试用例见 test7 #43

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

microcai
Copy link
Member

先提交下。有问题了留言。

@microcai microcai force-pushed the syncAwait-with-try-catch branch 2 times, most recently from ebffbd4 to 6eed51d Compare October 21, 2024 00:15
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
tests/test7/test.cpp Outdated Show resolved Hide resolved
@microcai microcai force-pushed the syncAwait-with-try-catch branch 2 times, most recently from baaf174 to edec9d4 Compare October 21, 2024 11:30
@microcai microcai force-pushed the syncAwait-with-try-catch branch 2 times, most recently from 9c97d76 to b599c70 Compare October 21, 2024 13:12
Copy link
Member

@Jackarain Jackarain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

添加的 sync_await 看起来没什么大的问题了。

但是虽添加了 sync_await 且解决了异常传播的问题,而 coro_start 对于异常传播的问题,并未作修复。

include/ucoro/awaitable.hpp Outdated Show resolved Hide resolved
@microcai microcai force-pushed the syncAwait-with-try-catch branch from b599c70 to 37ab113 Compare October 21, 2024 15:43
@microcai microcai requested a review from Jackarain October 21, 2024 15:45
简单的来说,就是

```c++
try
{
    start_coro(some_coro_that_throw());
}
catch(...)
{
    这里抓不到异常
}
```

原因是 start_coro 实际上同 asio 的 co_spawn 一样,分叉了。
这样就没办法抓异常了,除非修改some_coro_that_throw()本身的代码。

如果是
```c++
co_await start_coro(some_coro_that_throw());
```

固然这样写,就抓的到异常了。但是在非协程上下文里,就无法使用了。

因此需要一种在非协程上下文环境里,同步等待协程的方式,并且能抓到异常。

所以这个补丁就是新增了一个 sync_await() 函数。具体使用就是

```c++
void 常规函数()
{
    try
    {
        sync_await(some_coro_that_throw());
    }
    catch(...)
    {
        这里能抓到异常
    }
}
```
@microcai microcai force-pushed the syncAwait-with-try-catch branch from 37ab113 to 0dca587 Compare October 21, 2024 15:49
@microcai
Copy link
Member Author

修改好了

@microcai microcai merged commit 3e4d044 into master Oct 28, 2024
12 checks passed
@microcai microcai deleted the syncAwait-with-try-catch branch October 28, 2024 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants