Ver código fonte

device: remove dummy client

It was being sent data all the time but never reading it. That caused the TCP read buffer to fill up
after a while, eventually blocking the output thread for all other clients.

Fixes: #191
Daniele Bartolini 1 ano atrás
pai
commit
4080cf7481

+ 1 - 0
docs/changelog.rst

@@ -10,6 +10,7 @@ Changelog
 
 **Runtime**
 
+* Fixed an issue that caused the Runtime to stop sending console messages after a while.
 * Data Compiler: fixed erratic texture compilation when both legacy properties and the new "output" object were specified in the .texture resource.
 
 0.52.0 --- 11 Nov 2024

+ 11 - 20
src/device/console_server.cpp

@@ -171,35 +171,26 @@ void ConsoleServer::listen(u16 port, bool wait)
 	_input_thread.start([](void *thiz) { return ((ConsoleServer *)thiz)->run_input_thread(); }, this);
 	_output_thread.start([](void *thiz) { return ((ConsoleServer *)thiz)->run_output_thread(); }, this);
 
-	// Connect a dummy client to the _server to
-	// unlock the input_thread later at exit.
-	_dummy_client.connect(IP_ADDRESS_LOOPBACK, _port);
-	_client_connected.wait();
-
 	// Wait for real clients to connect.
 	if (wait)
 		_client_connected.wait();
 }
 
-void ConsoleServer::close()
+void ConsoleServer::shutdown()
 {
+	if (!_server.is_open())
+		return;
+
 	_thread_exit = true;
 
 	// Unlock input thread if it is stuck inside the select().
-	u32 blank_header = 0;
-	if (_dummy_client.is_open())
-		_dummy_client.write(&blank_header, sizeof(blank_header));
-}
-
-void ConsoleServer::shutdown()
-{
-	close();
-	_dummy_client.close();
-
 	_handlers_semaphore.post();
 	if (_input_thread.is_running())
 		_input_thread.stop();
 
+	// Unlock execute_message_handlers in sync mode.
+	_input_semaphore.post();
+
 	_output_condition.signal();
 	if (_output_thread.is_running())
 		_output_thread.stop();
@@ -337,7 +328,7 @@ s32 ConsoleServer::run_input_thread()
 	while (!_thread_exit) {
 		// Wait for input from one of the sockets in _active_socket_set.
 		_read_socket_set = _active_socket_set;
-		SelectResult ret = _read_socket_set.select(UINT32_MAX);
+		SelectResult ret = _read_socket_set.select(100);
 		if (ret.error == SelectResult::GENERIC_ERROR) {
 			return -1;
 		} else if (ret.error == SelectResult::TIMEOUT) {
@@ -357,9 +348,6 @@ s32 ConsoleServer::run_input_thread()
 
 			// If ready socket is the one listening for incoming connections.
 			if (cur_socket == _server) {
-				if (_thread_exit)
-					break;
-
 				// Accept the incoming connection.
 				TCPSocket client;
 				AcceptResult ar = _server.accept_nonblock(client);
@@ -368,6 +356,9 @@ s32 ConsoleServer::run_input_thread()
 					_active_socket_set.set(&client);
 					_client_connected.post();
 				}
+
+				if (_thread_exit)
+					break;
 			} else { // Check if any other socket is ready for reading.
 				u32 msg_len = 0;
 				ReadResult rr = cur_socket.read(&msg_len, 4);

+ 0 - 4
src/device/console_server.h

@@ -45,7 +45,6 @@ struct ConsoleServer
 
 	u16 _port;
 	TCPSocket _server;
-	TCPSocket _dummy_client;
 	u32 _next_client_id;
 	Mutex _clients_mutex;
 	Vector<Client> _clients;
@@ -81,9 +80,6 @@ struct ConsoleServer
 	/// blocks until a client is connected.
 	void listen(u16 port, bool wait);
 
-	/// Closes the server.
-	void close();
-
 	/// Shutdowns the server.
 	void shutdown();
 

+ 0 - 3
src/device/device.cpp

@@ -684,9 +684,6 @@ void Device::run()
 void Device::quit()
 {
 	_quit = true;
-
-	if (_console_server)
-		_console_server->close();
 }
 
 int Device::argc() const

+ 2 - 2
src/resource/data_compiler.cpp

@@ -1454,7 +1454,7 @@ int main_data_compiler(const DeviceOptions &opts)
 			case CTRL_C_EVENT:
 				_quit = true;
 				if (console_server())
-					console_server()->close();
+					console_server()->shutdown();
 				return TRUE;
 
 			default:
@@ -1473,7 +1473,7 @@ int main_data_compiler(const DeviceOptions &opts)
 			case SIGTERM:
 				_quit = true;
 				if (console_server())
-					console_server()->close();
+					console_server()->shutdown();
 				break;
 
 			default: