Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smartmedev opened this issue Oct 20, 2023 · 3 comments
Open
Labels
bug Something isn't working

Comments

@smartmedev
Copy link
Contributor

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 smartmedev added the bug Something isn't working label Oct 20, 2023
@smartmedev
Copy link
Contributor Author

any suggestions here??

@kayatovsky
Copy link

+1

1 similar comment
@bartoszboruta
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants