Skip to content

[Bug] Client.close might crash #550

Closed

Description

@BewareMyPower

Search before asking

  • I searched in the issues and found nothing similar.

Version

0c6a7c0

Minimal reproduce step

Run testCloseClient for a few times:

./build/tests/pulsar-tests --gtest_filter='*testCloseClient'

What did you expect to see?

passed

What did you see instead?

You might see the segmentation fault:

2026-03-09 11:22:47.077 ERROR [0x1ff497840] ClientImpl:228 | Error Checking/Getting Partition Metadata while creating producer on persistent://public/default/client-test-close-client-1773026566 -- ResultDisconnected
2026-03-09 11:22:47.077 INFO  [0x1ff497840] ClientImpl:687 | Closing Pulsar client with 0 producers and 0 consumers
2026-03-09 11:22:47.077 INFO  [0x16bb9f000] ClientConnection:1353 | [<none> -> pulsar://localhost:6650] Connection disconnected (refCnt: 1)
zsh: segmentation fault  ./build/tests/pulsar-tests --gtest_filter='*testCloseClient'

Anything else?

It seems that there was a regression on #136.

* thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0xc0c4bd980148)
    frame #0: 0x00000001000825d8 pulsar-tests`asio::detail::reactive_socket_service_base::close(this=0x0000c0c4bd980148, impl=0x0000600002f50128, ec=0x000000016fe866a0) at reactive_socket_service_base.ipp:109:5
   106 	    ASIO_HANDLER_OPERATION((reactor_.context(),
   107 	          "socket", &impl, impl.socket_, "close"));
   108
-> 109 	    reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
   110 	        (impl.state_ & socket_ops::possible_dup) == 0);
   111
   112 	    socket_ops::close(impl.socket_, impl.state_, false, ec);
Target 0: (pulsar-tests) stopped.
(lldb) bt
* thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0xc0c4bd980148)
  * frame #0: 0x00000001000825d8 pulsar-tests`asio::detail::reactive_socket_service_base::close(this=0x0000c0c4bd980148, impl=0x0000600002f50128, ec=0x000000016fe866a0) at reactive_socket_service_base.ipp:109:5
    frame #1: 0x00000001005b0014 pulsar-tests`asio::basic_socket<asio::ip::tcp, asio::any_io_executor>::close(this=0x0000600002f50120, ec=0x000000016fe866a0) at basic_socket.hpp:541:25
    frame #2: 0x00000001005f3fd0 pulsar-tests`void asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>::process<asio::ip::basic_resolver_iterator<asio::ip::tcp>>(this=0x000000016fe867e0, ec=(__val_ = 89, __cat_ = 0x00000001019f8078), start=0, begin=basic_resolver_iterator<asio::ip::tcp> @ 0x000000016fe86708, end=basic_resolver_iterator<asio::ip::tcp> @ 0x000000016fe866f0) at connect.hpp:370:21
    frame #3: 0x00000001005f3cdc pulsar-tests`asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>::operator()(this=0x000000016fe867e0, ec=(__val_ = 89, __cat_ = 0x00000001019f8078), start=0) at connect.hpp:347:13
    frame #4: 0x00000001005f53f8 pulsar-tests`asio::detail::binder1<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, std::__1::error_code>::operator()(this=0x000000016fe867e0) at bind_handler.hpp:114:5
    frame #5: 0x00000001005f5258 pulsar-tests`void asio::detail::handler_work<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, asio::any_io_executor, void>::complete<asio::detail::binder1<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, std::__1::error_code>>(this=0x000000016fe86870, function=0x000000016fe867e0, handler=0x000000016fe867e0) at handler_work.hpp:469:7
    frame #6: 0x00000001005f4f60 pulsar-tests`asio::detail::reactive_socket_connect_op<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, asio::any_io_executor>::do_complete(owner=0x000000015bf044f0, base=0x0000600003050000, (null)=0x000000016fe86ae8, (null)=0) at reactive_socket_connect_op.hpp:114:9
    frame #7: 0x0000000100067448 pulsar-tests`asio::detail::scheduler_operation::complete(this=0x0000600003050000, owner=0x000000015bf044f0, ec=0x000000016fe86ae8, bytes_transferred=0) at scheduler_operation.hpp:39:5
    frame #8: 0x0000000100066d68 pulsar-tests`asio::detail::scheduler::do_run_one(this=0x000000015bf044f0, lock=0x000000016fe869c8, this_thread=0x000000016fe86a00, ec=0x000000016fe86ae8) at scheduler.ipp:492:12
    frame #9: 0x00000001000669d0 pulsar-tests`asio::detail::scheduler::run(this=0x000000015bf044f0, ec=0x000000016fe86ae8) at scheduler.ipp:209:10
    frame #10: 0x000000010078b3f0 pulsar-tests`asio::io_context::run(this=0x0000600003e501a8) at io_context.ipp:63:24
    frame #11: 0x000000010078aed4 pulsar-tests`pulsar::ExecutorService::start()::$_0::operator()(this=0x0000600000b50168) const at ExecutorService.cc:39:29
    frame #12: 0x000000010078ad78 pulsar-tests`decltype(std::declval<pulsar::ExecutorService::start()::$_0>()()) std::__1::__invoke[abi:ne180100]<pulsar::ExecutorService::start()::$_0>(__f=0x0000600000b50168) at invoke.h:344:25
    frame #13: 0x000000010078ad54 pulsar-tests`void std::__1::__thread_execute[abi:ne180100]<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, pulsar::ExecutorService::start()::$_0>(__t=size=2, (null)=__tuple_indices<> @ 0x000000016fe86f7f) at thread.h:199:3
    frame #14: 0x000000010078a964 pulsar-tests`void* std::__1::__thread_proxy[abi:ne180100]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, pulsar::ExecutorService::start()::$_0>>(__vp=0x0000600000b50160) at thread.h:208:3
    frame #15: 0x000000019a3832e4 libsystem_pthread.dylib`_pthread_start + 136

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. BewareMyPower commented on Mar 11, 2026

    @BewareMyPower
    ContributorAuthor

    After applying the following patch that extends the lifetime of socket_:

    diff --git a/lib/ClientConnection.cc b/lib/ClientConnection.cc
    index 0a850ed..228c668 100644
    --- a/lib/ClientConnection.cc
    +++ b/lib/ClientConnection.cc
    @@ -642,12 +642,19 @@ void ClientConnection::handleResolve(ASIO_ERROR err, const tcp::resolver::result
             ptr->connectTimeoutTask_->stop();
         });
         connectTimeoutTask_->start();
    -    ASIO::async_connect(*socket_, results, [weakSelf](const ASIO_ERROR& err, const tcp::endpoint& endpoint) {
    -        auto self = weakSelf.lock();
    -        if (self) {
    -            self->handleTcpConnected(err, endpoint);
    -        }
    -    });
    +    // Capture socket_ by value (shared_ptr) to ensure the socket object stays alive for
    +    // the entire duration of async_connect. ASIO's range_connect_op holds a raw reference
    +    // to the socket and calls socket.close() between endpoint retry attempts; if
    +    // ClientConnection is destroyed (dropping the socket_ shared_ptr) before those
    +    // internal calls complete, that results in a use-after-free crash.
    +    auto socket = socket_;
    +    ASIO::async_connect(*socket_, results,
    +                        [weakSelf, socket](const ASIO_ERROR& err, const tcp::endpoint& endpoint) {
    +                            auto self = weakSelf.lock();
    +                            if (self) {
    +                                self->handleTcpConnected(err, endpoint);
    +                            }
    +                        });
     }
    
     void ClientConnection::readNextCommand() {

    The stacks become:

    * thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0x98)
        frame #0: 0x0000000100081180 pulsar-tests`asio::detail::kqueue_reactor::deregister_descriptor(this=0x000000013e81bda0, descriptor=8, descriptor_data=0x0000600003b891b0, closing=true) at kqueue_reactor.ipp:335:25
       332
       333 	  mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
       334
    -> 335 	  if (!descriptor_data->shutdown_)
       336 	  {
       337 	    if (closing)
       338 	    {
    Target 0: (pulsar-tests) stopped.
    (lldb) bt
    * thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0x98)
      * frame #0: 0x0000000100081180 pulsar-tests`asio::detail::kqueue_reactor::deregister_descriptor(this=0x000000013e81bda0, descriptor=8, descriptor_data=0x0000600003b891b0, closing=true) at kqueue_reactor.ipp:335:25
        frame #1: 0x000000010008258c pulsar-tests`asio::detail::reactive_socket_service_base::close(this=0x0000600000a913a8, impl=0x0000600003b891a8, ec=0x000000016fe86690) at reactive_socket_service_base.ipp:109:14
        frame #2: 0x00000001005affc8 pulsar-tests`asio::basic_socket<asio::ip::tcp, asio::any_io_executor>::close(this=0x0000600003b891a0, ec=0x000000016fe86690) at basic_socket.hpp:541:25
        frame #3: 0x00000001005f3fa8 pulsar-tests`void asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>::process<asio::ip::basic_resolver_iterator<asio::ip::tcp>>(this=0x000000016fe867d0, ec=(__val_ = 89, __cat_ = 0x00000001019f8078), start=0, begin=basic_resolver_iterator<asio::ip::tcp> @ 0x000000016fe866f8, end=basic_resolver_iterator<asio::ip::tcp> @ 0x000000016fe866e0) at connect.hpp:370:21
        frame #4: 0x00000001005f3ca0 pulsar-tests`asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>::operator()(this=0x000000016fe867d0, ec=(__val_ = 89, __cat_ = 0x00000001019f8078), start=0) at connect.hpp:347:13
        frame #5: 0x00000001005f53d4 pulsar-tests`asio::detail::binder1<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, std::__1::error_code>::operator()(this=0x000000016fe867d0) at bind_handler.hpp:114:5
        frame #6: 0x00000001005f5234 pulsar-tests`void asio::detail::handler_work<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, asio::any_io_executor, void>::complete<asio::detail::binder1<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, std::__1::error_code>>(this=0x000000016fe86870, function=0x000000016fe867d0, handler=0x000000016fe867d0) at handler_work.hpp:469:7
        frame #7: 0x00000001005f4f3c pulsar-tests`asio::detail::reactive_socket_connect_op<asio::detail::range_connect_op<asio::ip::tcp, asio::any_io_executor, asio::ip::basic_resolver_results<asio::ip::tcp>, asio::detail::default_connect_condition, pulsar::ClientConnection::handleResolve(std::__1::error_code, asio::ip::basic_resolver_results<asio::ip::tcp> const&)::$_1>, asio::any_io_executor>::do_complete(owner=0x000000013e81bc80, base=0x0000600002694000, (null)=0x000000016fe86ae8, (null)=0) at reactive_socket_connect_op.hpp:114:9
        frame #8: 0x00000001000673c8 pulsar-tests`asio::detail::scheduler_operation::complete(this=0x0000600002694000, owner=0x000000013e81bc80, ec=0x000000016fe86ae8, bytes_transferred=0) at scheduler_operation.hpp:39:5
        frame #9: 0x0000000100066ce8 pulsar-tests`asio::detail::scheduler::do_run_one(this=0x000000013e81bc80, lock=0x000000016fe869c8, this_thread=0x000000016fe86a00, ec=0x000000016fe86ae8) at scheduler.ipp:492:12
        frame #10: 0x0000000100066950 pulsar-tests`asio::detail::scheduler::run(this=0x000000013e81bc80, ec=0x000000016fe86ae8) at scheduler.ipp:209:10
        frame #11: 0x000000010078b3cc pulsar-tests`asio::io_context::run(this=0x0000600002a901a8) at io_context.ipp:63:24
        frame #12: 0x000000010078aeb0 pulsar-tests`pulsar::ExecutorService::start()::$_0::operator()(this=0x0000600001f93c08) const at ExecutorService.cc:39:29
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions