Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Crash multithreaded server  #175

Open
@ghost

Description

Hi,
I am using rpclib for microservices of system. I made multi-thread client and server and found bug.

I have error:

terminate called after throwing an instance of 'std::system_error'
  what():  close: Bad file descriptor
[1]    27051 abort      ./server

or

./server
[1]    22191 segmentation fault  ./server

I created multi-thread server and multi-thread client.

Server

#include <iostream>
#include "rpc/server.h"

int main(int argc, char *argv[]) 
{
    rpc::server srv(8088);
    srv.bind("add", [](int a, int b)
    {
        std::cout << "add" << std::endl;
        return a + b;
    });

    srv.suppress_exceptions(true);
    srv.async_run(8);
    std::cin.ignore();

    return 0;
}

Client

#include <thread>
#include "rpc/client.h"

int main() 
{
    while(true)
    {
        std::vector<std::thread> thread_pool(10);
        for (auto & client_thread : thread_pool)
        {
            client_thread = std::thread([]()
                                        {
                                            rpc::client client("127.0.0.1", 8088);
                                            int result;
                                            result = client.call("add", 2, 3).as<int>();
                                        });
        }

        for (auto & client_thread : thread_pool)
        {
            if (client_thread.joinable())
            {
                client_thread.join();
            }
        }
    }

    return 0;
}

Build
CXX_FLAGS "-std=c++14 -lpthread -O2

(gdb) backtrace in policy

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007ffff6fa519a in __GI_abort () at abort.c:89
#2  0x00007ffff78ceb85 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff78cc956 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff78cc9a1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff78f775e in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7bc3519 in start_thread (arg=0x7ffff5f6d700) at pthread_create.c:456
#7  0x00007ffff705ca5f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

or

(gdb) backtrace in master

terminate called after throwing an instance of 'std::bad_weak_ptr'
  what():  bad_weak_ptr

Thread 4 "server" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff5f6d700 (LWP 15501)]
0x00005555555db50e in clmdep_asio::detail::epoll_reactor::deregister_descriptor(int, clmdep_asio::detail::epoll_reactor::descriptor_state*&, bool) ()
(gdb) backtrace 
#0  0x00005555555db50e in clmdep_asio::detail::epoll_reactor::deregister_descriptor(int, clmdep_asio::detail::epoll_reactor::descriptor_state*&, bool) ()
#1  0x00005555555ecc32 in clmdep_asio::detail::reactive_socket_service_base::close(clmdep_asio::detail::reactive_socket_service_base::base_implementation_type&, std::error_code&) ()
#2  0x00005555555ef788 in clmdep_asio::stream_socket_service<clmdep_asio::ip::tcp>::close(clmdep_asio::detail::reactive_socket_service<clmdep_asio::ip::tcp>::implementation_type&, std::error_code&) ()
#3  0x00005555555eece7 in clmdep_asio::basic_socket<clmdep_asio::ip::tcp, clmdep_asio::stream_socket_service<clmdep_asio::ip::tcp> >::close() ()
#4  0x00005555555e8272 in rpc::detail::server_session::close()::{lambda()#1}::operator()() const ()
#5  0x00005555555eab74 in void clmdep_asio::clmdep_asio_handler_invoke<rpc::detail::server_session::close()::{lambda()#1}>(rpc::detail::server_session::close()::{lambda()#1}&, ...) ()
#6  0x00005555555ea6ef in void clmdep_asio_handler_invoke_helpers::invoke<rpc::detail::server_session::close()::{lambda()#1}, {lambda()#1}>(rpc::detail::server_session::close()::{lambda()#1}&, {lambda()#1}&) ()
#7  0x00005555555e9e9b in clmdep_asio::detail::completion_handler<rpc::detail::server_session::close()::{lambda()#1}>::do_complete(clmdep_asio::detail::task_io_service*, clmdep_asio::detail::task_io_service_operation*, std::error_code const&, unsigned long) ()
#8  0x00005555555da43e in clmdep_asio::detail::task_io_service_operation::complete(clmdep_asio::detail::task_io_service&, std::error_code const&, unsigned long) ()
#9  0x00005555555ed4a5 in clmdep_asio::detail::strand_service::do_complete(clmdep_asio::detail::task_io_service*, clmdep_asio::detail::task_io_service_operation*, std::error_code const&, unsigned long) ()
#10 0x00005555555da43e in clmdep_asio::detail::task_io_service_operation::complete(clmdep_asio::detail::task_io_service&, std::error_code const&, unsigned long) ()
#11 0x00005555555dca4c in clmdep_asio::detail::task_io_service::do_run_one(clmdep_asio::detail::scoped_lock<clmdep_asio::detail::posix_mutex>&, clmdep_asio::detail::task_io_service_thread_info&, std::error_code const&) ()
#12 0x00005555555dc57e in clmdep_asio::detail::task_io_service::run(std::error_code&) ()
#13 0x00005555555dcce9 in clmdep_asio::io_service::run() ()
#14 0x00005555555d8697 in rpc::server::async_run(unsigned long)::{lambda()#1}::operator()() const ()
#15 0x00005555555d894b in std::_Function_handler<void (), rpc::server::async_run(unsigned long)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
#16 0x00005555555e2a94 in std::function<void ()>::operator()() const ()
#17 0x00005555555e15da in void std::__invoke_impl<void, std::function<void ()>>(std::__invoke_other, std::function<void ()>&&) ()
#18 0x00005555555e0300 in std::__invoke_result<std::function<void ()>>::type std::__invoke<std::function<void ()>>(std::function<void ()>&&) ()
#19 0x00005555555e7e92 in decltype (__invoke((_S_declval<0ul>)())) std::thread::_Invoker<std::tuple<std::function<void ()> > >::_M_invoke<0ul>(std::_Index_tuple<0ul>) ()
#20 0x00005555555e7ceb in std::thread::_Invoker<std::tuple<std::function<void ()> > >::operator()() ()
#21 0x00005555555e7c0a in std::thread::_State_impl<std::thread::_Invoker<std::tuple<std::function<void ()> > > >::_M_run() ()
#22 0x00007ffff78f772f in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#23 0x00007ffff7bc3519 in start_thread (arg=0x7ffff5f6d700) at pthread_create.c:456
#24 0x00007ffff705ca5f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

I tryed branches:

  • master
  • dev
  • policy
  • 2.2.1

All have this bug.

Can you please tell me if there is any fix or workaround for this issue?

Thanks,
Alex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions