mirror of https://github.com/OpenTTD/OpenTTD
Fix: destroying a TCPConnecter that was still resolving made illegal writes
Basically, we should join the resolve thread before we destruct the object.pull/9217/head
parent
664a8c3e85
commit
1b75a29d12
|
@ -18,6 +18,7 @@
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
/** The states of sending the packets. */
|
/** The states of sending the packets. */
|
||||||
enum SendPacketsState {
|
enum SendPacketsState {
|
||||||
|
@ -65,6 +66,9 @@ public:
|
||||||
*/
|
*/
|
||||||
class TCPConnecter {
|
class TCPConnecter {
|
||||||
private:
|
private:
|
||||||
|
std::thread resolve_thread; ///< Thread used during resolving.
|
||||||
|
std::atomic<bool> is_resolved = false; ///< Whether resolving is done.
|
||||||
|
|
||||||
addrinfo *ai = nullptr; ///< getaddrinfo() allocated linked-list of resolved addresses.
|
addrinfo *ai = nullptr; ///< getaddrinfo() allocated linked-list of resolved addresses.
|
||||||
std::vector<addrinfo *> addresses; ///< Addresses we can connect to.
|
std::vector<addrinfo *> addresses; ///< Addresses we can connect to.
|
||||||
std::map<SOCKET, NetworkAddress> sock_to_address; ///< Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
|
std::map<SOCKET, NetworkAddress> sock_to_address; ///< Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
|
||||||
|
@ -73,7 +77,6 @@ private:
|
||||||
std::vector<SOCKET> sockets; ///< Pending connect() attempts.
|
std::vector<SOCKET> sockets; ///< Pending connect() attempts.
|
||||||
std::chrono::steady_clock::time_point last_attempt; ///< Time we last tried to connect.
|
std::chrono::steady_clock::time_point last_attempt; ///< Time we last tried to connect.
|
||||||
|
|
||||||
std::atomic<bool> is_resolved = false; ///< Whether resolving is done.
|
|
||||||
std::string connection_string; ///< Current address we are connecting to (before resolving).
|
std::string connection_string; ///< Current address we are connecting to (before resolving).
|
||||||
|
|
||||||
void Resolve();
|
void Resolve();
|
||||||
|
|
|
@ -32,13 +32,17 @@ TCPConnecter::TCPConnecter(const std::string &connection_string, uint16 default_
|
||||||
|
|
||||||
_tcp_connecters.push_back(this);
|
_tcp_connecters.push_back(this);
|
||||||
|
|
||||||
if (!StartNewThread(nullptr, "ottd:resolve", &TCPConnecter::ResolveThunk, this)) {
|
if (!StartNewThread(&this->resolve_thread, "ottd:resolve", &TCPConnecter::ResolveThunk, this)) {
|
||||||
this->Resolve();
|
this->Resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPConnecter::~TCPConnecter()
|
TCPConnecter::~TCPConnecter()
|
||||||
{
|
{
|
||||||
|
if (this->resolve_thread.joinable()) {
|
||||||
|
this->resolve_thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
for (const auto &socket : this->sockets) {
|
for (const auto &socket : this->sockets) {
|
||||||
closesocket(socket);
|
closesocket(socket);
|
||||||
}
|
}
|
||||||
|
@ -187,6 +191,7 @@ void TCPConnecter::Resolve()
|
||||||
|
|
||||||
this->ai = ai;
|
this->ai = ai;
|
||||||
this->OnResolved(ai);
|
this->OnResolved(ai);
|
||||||
|
|
||||||
this->is_resolved = true;
|
this->is_resolved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue