Rapsssito / react-native-tcp-socket

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

Bug on TcpSocket.createConnection after creating/destroying 4/5 times #181

Open smartmedev opened 11 months ago

smartmedev commented 11 months ago

Description

I got crash when calling TcpSocket.createConnection, both on Android and iOS, after I create and destroy a tcp socket about 4/5 times.

I destroy the socket with two commands:

global.tcpClient.destroy();
global.tcpClient = null;

Then when I try to recreate socket calling

global.tcpClient = TcpSocket.createConnection({
    port: 9997,
    host: "192.168.3.1",
    interface: 'wifi',
    reuseAddress: false,
    keepAlive: false
}, () => { 
console.log('Connected!');
global.socketTcpIDInUse = global.tcpClient._id;
});

For the first 3/4 times it works fine, but from fourth or sometimes fifth times, the app crash just before logging "Connected!" in console.

The error printed in adb logcat says:

10-20 12:14:42.796  4153  4633 E AndroidRuntime: java.lang.IllegalArgumentException: No socket with id 5
10-20 12:14:42.796  4153  4633 E AndroidRuntime:    at com.asterinet.react.tcpsocket.TcpSocketModule.getTcpClient(TcpSocketModule.java:281)
10-20 12:14:42.796  4153  4633 E AndroidRuntime:    at com.asterinet.react.tcpsocket.TcpSocketModule.access$800(TcpSocketModule.java:28)
10-20 12:14:42.796  4153  4633 E AndroidRuntime:    at com.asterinet.react.tcpsocket.TcpSocketModule$2.run(TcpSocketModule.java:125)
10-20 12:14:42.796  4153  4633 E AndroidRuntime:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
10-20 12:14:42.796  4153  4633 E AndroidRuntime:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
10-20 12:14:42.796  4153  4633 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:1012)

Sometimes it happens during trying to creaeteConnection the fourth times, other on the fifth.

Betweene a creation/destruction of the socket, some action happens on it, basically writing messages, pause and resuming it.

I thought the bug might be caused by trying writing on socket that is destroyed, so i added a check just before writing on socket, and this is the method used to write, but nothing changed, the bug still happens.

export const sendTcpData = (messageJson) => {
    if(global.tcpClient!==null && global.tcpClient!==undefined) {
        if(global.socketInPause) {
            console.log("socket in pause!");
            global.tcpClient.resume();
            global.socketInPause = false;
            console.log("resumed socket before sending tcp data");
        }
        // Write on the socket
        if(global.tcpClient._readyState==="open") {
            if(global.socketTcpIDInUse!==null && global.tcpClient._id==global.socketTcpIDInUse) {
                console.log("socket open ID "+global.tcpClient._id+" - trying writing")
                global.tcpClient.write(JSON.stringify(messageJson)+'\n');
            } else {
                console.log("SEND TCP DATA ERROR - TRYING USING global.tcpClient._id "+global.tcpClient._id+" BUT global.socketTcpIDInUse is "+global.socketTcpIDInUse)
            }
        } else {
            console.log("SEND TCP DATA ERROR - SOCKET IS CLOSED")
        }
    } else {
        console.log("SEND TCP DATA ERROR - SOCKET NOT ALIVE!");
    }
}

Anyone can please help me to prevent the crash?

Relevant information

OS Android&iOS
react-native 0.70.8
react-native-tcp-socket 6.0.6
smartmedev commented 8 months ago

any suggestions here??

kayatovsky commented 6 months ago

+1

bartoszboruta commented 4 months ago

+1