diff --git a/CMakeLists.txt b/CMakeLists.txt index 060f33d..9f53b4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,4 +2,6 @@ cmake_minimum_required (VERSION 3.8) project ("vsock-bridge") +set(CMAKE_CXX_FLAGS_DEBUG "-ggdb") + add_subdirectory ("vsock-bridge") diff --git a/vsock-bridge/include/channel.h b/vsock-bridge/include/channel.h index 0fa379e..62a17ca 100644 --- a/vsock-bridge/include/channel.h +++ b/vsock-bridge/include/channel.h @@ -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; diff --git a/vsock-bridge/include/dispatcher.h b/vsock-bridge/include/dispatcher.h index 4615f82..7ef0868 100644 --- a/vsock-bridge/include/dispatcher.h +++ b/vsock-bridge/include/dispatcher.h @@ -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 diff --git a/vsock-bridge/include/peer.h b/vsock-bridge/include/peer.h index 11ca678..6ea5ce6 100644 --- a/vsock-bridge/include/peer.h +++ b/vsock-bridge/include/peer.h @@ -84,12 +84,6 @@ namespace vsockio --_ioEventCount; } - void onError() - { - handleError(); - --_ioEventCount; - } - inline void setPeer(Peer* p) { _peer = p; @@ -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; diff --git a/vsock-bridge/include/socket.h b/vsock-bridge/include/socket.h index 2e89584..fd0a03b 100644 --- a/vsock-bridge/include/socket.h +++ b/vsock-bridge/include/socket.h @@ -54,14 +54,10 @@ namespace vsockio bool writeToOutput() override; - void handleError() override; - void onPeerClosed() override; void queue(std::unique_ptr&& buffer) override; - int name() const override { return _fd; } - private: std::unique_ptr read(); diff --git a/vsock-bridge/src/socket.cpp b/vsock-bridge/src/socket.cpp index 0858ba0..f0ee138 100644 --- a/vsock-bridge/src/socket.cpp +++ b/vsock-bridge/src/socket.cpp @@ -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()) diff --git a/vsock-bridge/test/testmain.cpp b/vsock-bridge/test/testmain.cpp index f5ad1d5..da8a9e7 100644 --- a/vsock-bridge/test/testmain.cpp +++ b/vsock-bridge/test/testmain.cpp @@ -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++)