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

Cannot destroy the connection from timeout while connecting #168

Closed
jesperjohansson opened this issue Jan 26, 2023 · 2 comments · Fixed by #169
Closed

Cannot destroy the connection from timeout while connecting #168

jesperjohansson opened this issue Jan 26, 2023 · 2 comments · Fixed by #169
Labels
bug Something isn't working released

Comments

@jesperjohansson
Copy link
Contributor

Description

Calling socket.destroy() doesn't work if the timeout timer timed out while connecting. It has no effect. This means we still get the error event later, when it actually times out.

I think its because of this this._pending check here?
https://github.com/Rapsssito/react-native-tcp-socket/blob/master/src/Socket.js#L308

return new Promise<void>((resolve, reject) => {
  const socket = TcpSocket.createConnection(
    {
      host: '192.168.0.70',
      port: 1337,
    },
    () => {
      socket.write(payload, 'binary', err => {
        // TODO
      })
    },
  ).setTimeout(5000)

  socket.on('timeout', () => {
    socket.destroy() // Has no effect
    reject(new Error('Timed out'))
  })

  // Gets called some time after the `timeout` callback
  socket.on('error', err => {
    socket.destroy()
    reject(err)
  })
})

Notice the timestamps:
image

Relevant information

OS iOS
react-native 0.65.1
react-native-tcp-socket 6.0.3
@jesperjohansson
Copy link
Contributor Author

Tested using patch-package and removing the this._pending check gives me the desired result.
PR: #169

patch-package:

diff --git a/node_modules/react-native-tcp-socket/src/Socket.js b/node_modules/react-native-tcp-socket/src/Socket.js
index 51ca3d6..4268015 100644
--- a/node_modules/react-native-tcp-socket/src/Socket.js
+++ b/node_modules/react-native-tcp-socket/src/Socket.js
@@ -305,7 +305,7 @@ export default class Socket extends EventEmitter {
      * Ensures that no more I/O activity happens on this socket. Destroys the stream and closes the connection.
      */
     destroy() {
-        if (this._pending || this._destroyed) return this;
+        if (this._destroyed) return this;
         this._destroyed = true;
         this._clearTimeout();
         Sockets.destroy(this._id);

@github-actions
Copy link

🎉 This issue has been resolved in version 6.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

1 participant