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

[coro_io] dead lock when call server::stop() in server self's thread #905

Open
HEUBITLYJ opened this issue Feb 14, 2025 · 6 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@HEUBITLYJ
Copy link

HEUBITLYJ commented Feb 14, 2025

正如coro_rpc的example代码

#include <ylt/coro_rpc/coro_rpc_server.hpp>

#include "rpc_service.h"
using namespace coro_rpc;
using namespace async_simple;
using namespace async_simple::coro;
int main() {
  // init rpc server
  coro_rpc_server server(/*thread=*/std::thread::hardware_concurrency(),
                         /*port=*/8801);

  coro_rpc_server server2{/*thread=*/1, /*port=*/8802};

  // regist normal function for rpc
  server.register_handler<echo, async_echo_by_coroutine, async_echo_by_callback,
                          echo_with_attachment, nested_echo,
                          return_error_by_context, return_error_by_exception,
                          rpc_with_state_by_tag, get_ctx_info,
                          rpc_with_complete_handler, add>();

  // regist member function for rpc
  HelloService hello_service;
  server.register_handler<&HelloService::hello>(&hello_service);

  server2.register_handler<echo>();
  // async start server
  auto res = server2.async_start();
  assert(!res.hasResult());

  // sync start server & sync await server stop
  return !server.start();
}

启动以后程序会一直阻塞,那么如何正常关闭程序呢,如果直接停掉程序可能无法正常调用main函数里面的变量的析构函数,我尝试在一个rpc调用函数中调用server.stop()或者在set_complete_handler里面调用server.stop()都无法正常关闭,请问还有其他方法吗? coro_rpc_server will quit之后还是无法退出

Image

@poor-circle
Copy link
Collaborator

好吧,在rpc函数直接调用server.stop()似乎会导致死锁,原因是没检查stop请求是否在rpc线程池中发起。目前你可以把stop扔给另外一个线程完成。

@poor-circle
Copy link
Collaborator

死锁原因:
rpc函数->调用stop请求停止服务->等待当前的rpc函数执行完成->当前rpc函数正在执行stop

@HEUBITLYJ
Copy link
Author

死锁原因: rpc函数->调用stop请求停止服务->等待当前的rpc函数执行完成->当前rpc函数正在执行stop
后续会优化吗

@poor-circle poor-circle changed the title coro_rpc如何在服务端正常关闭server退出程序 [coro_io] dead lock when call stop() in io_context_pool's thread Feb 14, 2025
@poor-circle poor-circle self-assigned this Feb 14, 2025
@poor-circle poor-circle added the bug Something isn't working label Feb 14, 2025
@poor-circle
Copy link
Collaborator

poor-circle commented Feb 14, 2025

死锁原因: rpc函数->调用stop请求停止服务->等待当前的rpc函数执行完成->当前rpc函数正在执行stop
后续会优化吗

后续会看下怎么修复,目前你可以抛到另外一个线程去stop

@poor-circle poor-circle changed the title [coro_io] dead lock when call stop() in io_context_pool's thread [coro_io] dead lock when call server::stop() in server self's thread Feb 14, 2025
@HEUBITLYJ
Copy link
Author

好吧,在rpc函数直接调用server.stop()似乎会导致死锁,原因是没检查stop请求是否在rpc线程池中发起。目前你可以把stop扔给另外一个线程完成。

我在rpc函数中创建了另一个线程sleep 5s后调用stop函数,start函数返回了operation canceled,是否正常

Image

@poor-circle
Copy link
Collaborator

poor-circle commented Feb 14, 2025

好吧,在rpc函数直接调用server.stop()似乎会导致死锁,原因是没检查stop请求是否在rpc线程池中发起。目前你可以把stop扔给另外一个线程完成。

我在rpc函数中创建了另一个线程sleep 5s后调用stop函数,start函数返回了operation canceled,是否正常

Image

正常,服务器被用户停止,start()预期的返回值就应该是operation canceled。不就是因为应用程序请求所以中止IO操作了嘛

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

No branches or pull requests

2 participants