Rapsssito / react-native-tcp-socket

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

Attempt to invoke virtual method 'java.io.InputStream java.net.Socket.getInputStream()' on a null object reference #188

Open sergeushenecz opened 2 months ago

sergeushenecz commented 2 months ago

Hello everyone. I see in the sentry sometimes this errors. Maybe someone are faced also with this issues?

marianolc commented 2 months ago

This is a bug(i think) that i "fixed" in the version I forked... i just changed the write() method in TcpSocketClient:

public void write(final int msgId, final byte[] data) {
        writeExecutor.execute(new Runnable() {
            @Override
            public void run() {
               // THIS IS THE FIX VALIDATION ***********************
                if (socket == null) {
                    receiverListener.onError(getId(), new IOException("Attempted to write to closed socket"));
                    return;
                }
                try {
                    socket.getOutputStream().write(data);
                    receiverListener.onWritten(getId(), msgId, null);
                } catch (IOException e) {
                    receiverListener.onWritten(getId(), msgId, e);
                    receiverListener.onError(getId(), e);
                }
            }
        });
    }
sergeushenecz commented 2 months ago

And this code will fix problem? Maybe need to add pull request for it?

sergeushenecz commented 2 months ago

@marianolc I've checked in library and see the same code as you wrote me. I don't understand where need to add fix.

image