Skip to content

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

Open

Description

@HEUBITLYJ

正如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

Activity

  1. poor-circle commented on Feb 14, 2025

    @poor-circle
    Collaborator

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

  2. poor-circle commented on Feb 14, 2025

    @poor-circle
    Collaborator

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

  3. HEUBITLYJ commented on Feb 14, 2025

    @HEUBITLYJ
    Author

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

  4. changed the title [-]coro_rpc如何在服务端正常关闭server退出程序[/-] [+][coro_io] dead lock when call stop() in io_context_pool's thread[/+] on Feb 14, 2025
  5. self-assigned this
    on Feb 14, 2025
  6. added
    bug
    on Feb 14, 2025
  7. poor-circle commented on Feb 14, 2025

    @poor-circle
    Collaborator

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

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

  8. 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[/+] on Feb 14, 2025
  9. HEUBITLYJ commented on Feb 14, 2025

    @HEUBITLYJ
    Author

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

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

    Image

  10. poor-circle commented on Feb 14, 2025

    @poor-circle
    Collaborator

    好吧,在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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions