Rapsssito / react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with SSL/TLS support.
MIT License
304 stars 80 forks source link

Crashing main thread if both server and socket created iOS #119

Closed ElchinIsmayilov closed 2 years ago

ElchinIsmayilov commented 2 years ago

Description

it seems that main thread is getting all the workload on ios and when data received on server and trying to connect to socket at the same time, it crashes the app. For our app, we need to create server on specific port and listen for any data it may receive and in same time be able to connect to socket if nessasary. I know that probably most of the users of this library is not using the library in this configuration, however, our app requires us to configure it this way. if there is any suggests , i would be happy to try.

Once again, our configuration is consist of one TcpSocket.createServer port 8100, which created when app is started and one TcpSocket.createConnection which is created whenever we are trying to connect to port 9100.

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a TcpSocket.createServer
  2. At random time send data to TcpSocket.createServer "data" listener and in same time try connecting to array of ip addresses with TcpSocket.createConnection
  3. it should crash the app and point to main thread on xcode

Current behavior

iOS app crashes whenever both server and socket connection is establish at the same time with data being process on server "data" listener

Expected behavior

App should not be crashing when there is two different types of connections.

Relevant information

OS iOS 14.6
react-native 0.63.4
react-native-tcp-socket 5.2.1
Rapsssito commented 2 years ago

@ElchinIsmayilov I am sorry for your issue. However, I do not understand how to reproduce your problem. The example code provides a server and a client created by the same host and works correctly. Could you please provide a reproducible code?

ElchinIsmayilov commented 2 years ago

Sorry, for the code format, github keeps trying to understand the code.

async function findLocters() { const ip = ("192.168.1.153").split("."); const baseIp = ip.slice(0, 3).join("."); const endings = range(256); endings.splice(parseInt(ip[3]), 1); // remove self ip

    const locters = [];
// Checking all the ips except its own.
    for (const [index, el] of endings.entries()) {
        const connection = await tryToConnect(`${baseIp}.${index}`, 30);
        locters.push(connection);
    }

// Returnin found ips
    return locters.filter(x => x.ok).map(x => x.ip);

}

function tryToConnect(host: string, timeout: number = 2000): Promise<{ ok: boolean, ip: string; }> { return new Promise((resolve) => { const socket = TcpSocket.createConnection({ port: 8100, host, timeout }, () => { socket.destroy(); console.log("connected"); return resolve({ ok: true, ip: host }); });

    socket.on("timeout", () => {
        socket.destroy();
        console.log("timeout");
        resolve({ ok: false, ip: host });
    });

    socket.on("error", (err: any) => {
        socket.destroy();
        console.log("error");
        resolve({ ok: false, ip: host });
    });
});

}

// Local server created once when app started let serverStarted: TcpSocket.Server; export async function startLS() { console.log("Start LS", serverStarted); if (serverStarted) { console.log("Server already listening on port:", port); return; }

serverStarted = TcpSocket.createServer(function (socket) {
    socket.on('data', (data) => {
        if (data) {
            console.log("received data", data);

    // sending response to received request
            socket.end([
                'HTTP/1.1 200 OK',
                'Content-Type: text/plain; charset=UTF-8',
                'Content-Encoding: UTF-8',
                'Accept: */*',
                'Connection: keep-alive',
                'Access-Control-Allow-Origin: *'
              ].join('\n') + '\n\n' + JSON.stringify({success: true, deviceId}));
        }
    });

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

    socket.on('close', (error) => {
        console.log('Closed connection with ', socket.address(), error);
    });
}).listen({ port, host: '0.0.0.0' });

serverStarted.on('error', error => {
    console.log('An error ocurred with the server', error);
});

serverStarted.on('close', () => {
    console.log('Server closed connection');
});

}

ElchinIsmayilov commented 2 years ago
Screen Shot 2021-08-17 at 15 34 50 Screen Shot 2021-08-17 at 15 38 06 Screen Shot 2021-08-17 at 15 37 46
Rapsssito commented 2 years ago

@ElchinIsmayilov, could you provide a minimal reproducible code so I can narrow down the issue?

github-actions[bot] commented 2 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.