Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
KEP-1120: Idle timeout of a session that was never open causes segfau…
Browse files Browse the repository at this point in the history
…lt (#242)
  • Loading branch information
isabelsavannah committed Feb 20, 2019
1 parent 209702c commit 84bad35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion node/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ session::close()
LOG(debug) << "closing session " << std::to_string(this->session_id);
this->io_context->post(this->shutdown_handler);

if (this->websocket->is_open())
if (this->websocket && this->websocket->is_open())
{
this->websocket->async_close(boost::beast::websocket::close_code::normal,
[self = shared_from_this()](auto ec)
Expand Down
6 changes: 5 additions & 1 deletion node/test/node_test_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <mocks/mock_boost_asio_beast.hpp>
#include <boost/asio.hpp>
#include <gmock/gmock.h>
#include <boost/asio/error.hpp>

using namespace ::testing;

Expand Down Expand Up @@ -45,6 +46,8 @@ namespace bzn

std::map<boost::asio::ip::tcp::socket::native_handle_type, size_t> socket_id_map;

bool tcp_connect_works = true;

smart_mock_io()
{
EXPECT_CALL(*(this->io_context), post(_)).WillRepeatedly(Invoke(
Expand Down Expand Up @@ -85,7 +88,8 @@ namespace bzn
EXPECT_CALL(*mock_socket, async_connect(_, _)).Times(AtMost(1)).WillOnce(Invoke(
[&](auto, auto handler)
{
this->real_io_context->post(std::bind(handler, boost::system::error_code{}));
this->real_io_context->post(std::bind(handler,
this->tcp_connect_works ? boost::system::error_code{} : boost::asio::error::connection_refused));
}));

return mock_socket;
Expand Down
15 changes: 15 additions & 0 deletions node/test/session_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,19 @@ namespace bzn
EXPECT_TRUE(this->mock.ws_closed.at(0));
}

TEST_F(session_test2, idle_timeout_after_connect_rejected)
{
bzn::smart_mock_io mock;
mock.tcp_connect_works = false;

auto session = std::make_shared<bzn::session>(mock.io_context, 0, TEST_ENDPOINT, this->mock_chaos, [](auto, auto){}, TEST_TIMEOUT, [](){}, nullptr);
session->open(mock.websocket);

this->yield();

// we are just testing that this doesn't cause a segfault
mock.timer_callbacks.at(0)(boost::system::error_code{});
mock.timer_callbacks.at(0)(boost::system::error_code{});
}

} // bzn

0 comments on commit 84bad35

Please sign in to comment.