react-native-tcp-socket icon indicating copy to clipboard operation
react-native-tcp-socket copied to clipboard

Memory Usage and thread count Increases Indefinitely with Simple TCP Server Code

Open GeorgeFlorian opened this issue 1 year ago • 3 comments

Description

Even the simplest code using react-native-tcp-socket causes increasing memory usage over time. The issue occurs with a basic TCP server that accepts connections, responds with an echo, and closes the socket immediately after responding. Despite not handling or processing any data, the server continuously increases memory usage indefinitely until Android OS kills the process and the app closes with no crash or log.

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a simple TCP server using react-native-tcp-socket that listens for client connections.
  2. Have the server respond with an echo message and immediately close the connection.
  3. Observe increasing memory usage over time, even with no data being processed.

Or code:

import React, { useEffect } from 'react';
import { View, Text } from 'react-native';
import TcpSocket from 'react-native-tcp-socket';

const TcpServer = () => { useEffect(() => { const server = TcpSocket.createServer(function (socket) { console.log('Client connected:', socket.address());

  socket.on('data', () => {
    console.log('Received data, but ignoring it.');
    socket.write('Echo server: I got your data');
    socket.end(); // Close connection immediately
  });

  socket.on('error', (error) => {
    console.log('An error occurred with client socket:', error);
    socket.destroy();
  });

  socket.on('close', () => {
    console.log('Closed connection with', socket.address());
  });
});

server.listen({ port: 3000, host: '0.0.0.0', reuseAddress: true }, () => {
  console.log('Server is listening on port 3000');
});

return () => {
  server.close(() => {
    console.log('Server has been closed');
  });
};

}, []);

return ( <View> <Text>TCP Server is running...</Text> </View> ); };

export default TcpServer;

Current behavior

Even with this minimal code, memory usage increases indefinitely as the server keeps accepting and closing connections, without processing or handling the data. The server does not leak data but causes increasing memory consumption due to uncleaned resources or improper socket handling.

Expected behavior

Memory usage should remain stable, even when handling multiple connections in such a simple use case. The server should close connections properly and free resources after responding to clients, with no noticeable increase in memory consumption over time.

Relevant information

   
OS Android 12
react-native 0.74.5
react-native-tcp-socket 6.2.0
Device Physical Device

GeorgeFlorian avatar Nov 27 '24 17:11 GeorgeFlorian