Skip to content

Commit

Permalink
Fix crash when trying to destroy channel/socket with non-empty buffer…
Browse files Browse the repository at this point in the history
… queue.

Remove unused code.
  • Loading branch information
atarassov-ttd committed Nov 20, 2023
1 parent 6e10239 commit b6e50ed
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ cmake_minimum_required (VERSION 3.8)

project ("vsock-bridge")

set(CMAKE_CXX_FLAGS_DEBUG "-ggdb")

add_subdirectory ("vsock-bridge")
5 changes: 5 additions & 0 deletions vsock-bridge/include/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ namespace vsockio
}
}

void terminate()
{
_taskQueue->enqueue([this] { delete this; });
}

bool canBeTerminated() const
{
return _a->closed() && _b->closed() && _a->ioEventCount() == 0 && _b->ioEventCount() == 0;
Expand Down
7 changes: 7 additions & 0 deletions vsock-bridge/include/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ namespace vsockio
if (!node->inUse() || node->_channel->canBeTerminated())
{
Logger::instance->Log(Logger::DEBUG, "destroying channel id=", it->first);

// any resources allocated on channel thread must be freed there
if (node->inUse())
{
node->_channel.release()->terminate();
}

it = _channels.erase(it);
}
else
Expand Down
10 changes: 0 additions & 10 deletions vsock-bridge/include/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ namespace vsockio
--_ioEventCount;
}

void onError()
{
handleError();
--_ioEventCount;
}

inline void setPeer(Peer* p)
{
_peer = p;
Expand Down Expand Up @@ -119,10 +113,6 @@ namespace vsockio

virtual bool writeToOutput() = 0;

virtual void handleError() = 0;

virtual int name() const = 0;

protected:
bool _inputReady = false;
bool _outputReady = false;
Expand Down
4 changes: 0 additions & 4 deletions vsock-bridge/include/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ namespace vsockio

bool writeToOutput() override;

void handleError() override;

void onPeerClosed() override;

void queue(std::unique_ptr<Buffer>&& buffer) override;

int name() const override { return _fd; }

private:
std::unique_ptr<Buffer> read();

Expand Down
11 changes: 0 additions & 11 deletions vsock-bridge/src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,6 @@ namespace vsockio
}
}

void Socket::handleError()
{
Logger::instance->Log(Logger::DEBUG, "[socket] got error from poller (fd=", _fd, ")");

_inputReady = true;
_outputReady = true;

readFromInput();
writeToOutput();
}

void Socket::onPeerClosed()
{
if (!closed())
Expand Down
5 changes: 5 additions & 0 deletions vsock-bridge/test/testmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ TEST_CASE("Dispatcher", "[dispatcher]")
REQUIRE(channel->_channel->canBeTerminated());

ex.taskloop();
for (; retry < 100 && channel->inUse(); retry++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

REQUIRE(channel->inUse() == false);

for (int i = 0; i < ThreadPool::threads.size(); i++)
Expand Down

0 comments on commit b6e50ed

Please sign in to comment.