React-native: Regression on 0.55.0 crash: WebSocketModule.close got 1 arguments, expected 3

Created on 4 Apr 2018  路  22Comments  路  Source: facebook/react-native

Closing a websocket crashes the app

Environment

$ react-native info
Scanning folders for symlinks in /media/dj/dev/scan-app/node_modules (9ms)

Environment:
  OS: Linux 4.13
  Node: 9.10.1
  Yarn: Not Found
  npm: 5.6.0
  Watchman: 4.7.0
  Xcode: N/A
  Android Studio: Not Found

Packages: (wanted => installed)
  react: ^16.3.0-rc.0 => 16.3.0-rc.0
  react-native: ^0.55.0 => 0.55.0

Steps to Reproduce

  1. open websocket connection. example:
const ws = new WebSocket(makeSocketURL())
  1. close websocket connection. example:
ws.close()

Expected Behavior


the websocket connection closes, no error

Actual Behavior


Logcat:

--------- beginning of crash
04-04 14:28:15.716 19745 19780 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
04-04 14:28:15.716 19745 19780 E AndroidRuntime: Process: com.chipta, PID: 19745
04-04 14:28:15.716 19745 19780 E AndroidRuntime: com.facebook.react.bridge.NativeArgumentsParseException: WebSocketModule.close got 1 arguments, expected 3
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:353)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:754)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:95)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:163)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)
04-04 14:28:15.716 19745 19780 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:760)
04-04 14:28:16.322 19745 19745 D ReactNative: ReactInstanceManager.detachViewFromInstance()

related:
https://github.com/facebook/react-native/issues/18623

Regression Locked

Most helpful comment

I just installed the current latest version "react-native": "^0.55.3" and (magically 馃槃 ) the problem disappeared.

All 22 comments

Thanks for posting this! It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest release, v0.54?

Thank you for your contributions.

same problem

This commit probably breaks it: https://github.com/facebook/react-native/commit/b9be289

Summary:
WebSocket uses the Platform module to check how many arguments for the close method should be used. In react-native-windows, we have the same number of arguments for close as Android, so we're prevented from using this module as-is because of the platform check (see https://github.com/Microsoft/react-native-windows/blob/master/Libraries/WebSocket/WebSocket.windows.js#L136).

In my code I use ws.close(). According to the websocket spec there can be 2 optional arguments. Despite them being optional I tried ws.close(1000, 'reason') and it did not make a difference.

close()

Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

void close(
in optional unsigned short code,
in optional DOMString reason
);

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

@kelset could you maybe remove the Old Version tag?

Also, I have made the repro steps more clear. I think this is all it takes to reproduce. If you agree, could you remove Needs Repro Steps? If you can not reproduce this way I am happy to make a more complete example.

If I just remove the call to ws.close(), my app works again and I can continue testing.

I don't see this as a stable workaround though. There used to be a bug (crash) with the websocket module trying to send a message over a websocket connection that did not exist any more. This happened despite closing the connection in js and it's supposed to be fixed, but the websocket connection should still be properly closed before doing anything else.

https://github.com/facebook/react-native/commit/b9be289 is definitely causing this issue, since WebSocketModule.close.length is always returning 0, which results in passing always only 1 parameter to close().

Since this really is a severe bug, causing the entire app to crash and not to respond whenever one tries to close a websocket connection, I hope someone from the react-native team is going to revert the referenced commit and publish 0.55.1 version as soon as possible. Maybe @hramos?

To discuss cherry-picks and critical things, there's a dedicated repo with a dedicated issue (this).

We'll keep you updated.

In the meantime I think pinging @rozele to know more about his commit is a good approach.

This is still an issue on RN-0.55.2. This is a very serious issue! Why was this closed?

@ujwal-setlur The fix is merged to master and a request was put in to cherry-pick it for RN 0.55.3.

Thanks @rozele!

@rozele do we have the release date of 0.55.3? We faced the same issue and this fix is critical for our application.

While we wait, i have a postinstall script to download the patch and apply it.

@ujwal-setlur any reference to this script is appreciated.

Please release v0.55.3 to fixed this

@vahanNasibyan, this is what I did:

In your package.json file, declare the postinstall script:

  "scripts": {
    "start": "watchman watch-del-all && (adb reverse tcp:3000 tcp:3000 || true) && node node_modules/react-native/local-cli/cli.js start --reset-cache",
    "test": "jest",
    "postinstall": "scripts/third-party-boo-boos.sh"
  },

In a scripts folder in root directory, I have a script third-party-boo-boos.sh:

#!/usr/bin/env bash
# Catch all script to fix/patch boo-boos in third-party libraries

# For RN 0.55.0 and up
# Download patch file and patch Websocket.js
wget https://github.com/facebook/react-native/commit/4b6e9d3dfd213689c5b319a4efee8d4f606d6f9d.patch
patch $PWD/node_modules/react-native/Libraries/WebSocket/WebSocket.js 4b6e9d3dfd213689c5b319a4efee8d4f606d6f9d.patch
rm -f 4b6e9d3dfd213689c5b319a4efee8d4f606d6f9d.patch

Delete your node_modules and reinstall them with npm install or yarn

@ujwal-setlur i'm working on Window, please help me!

I just installed the current latest version "react-native": "^0.55.3" and (magically 馃槃 ) the problem disappeared.

I'm still experiencing WebSocketModule.close got 1 arguments, expected 3 on React native version 0.55.4. Using expo sdk 28.0.1. Anyone else having this issue?

For those using expo. If anyone one else is having this issue, I resolved it by rm -rf node_modules and then running exp publish afterwards. It seems that exp still had version 28.0.0 cached from the node_modules

@wrightmk I don't think a 28.0.1 exists out there we only have 28.0.0 and 29.0.2-rc at the time of writing this. Looks like there is a 28.0.1 version of react-native (https://github.com/expo/react-native/archive/sdk-28.0.1.tar.gz)

Also expo requires you to publish with dependencies installed. So rm -rf node_modules && exp publish results in this error:

[12:46:21] Unable to find an existing exp instance for this directory, starting a new one...
[12:46:25] Error: node_modules directory is missing. Please run `npm install` in your project directory.
[12:46:25] Couldn't start project. Please fix the errors and restart the project.
[12:46:25] Set EXPO_DEBUG=true in your env to view the stack trace.

I tried setting my dependencies to:

"expo": "^28.0.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-28.0.1.tar.gz",

But still getting this error, what does your package.json look like @wrightmk ?

EDIT I can get past this bug temporarily by knocking down to these versions (Not ideal but until 29 comes out this could unblock you)

"expo": "^25.0.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-25.0.0.tar.gz",
Was this page helpful?
0 / 5 - 0 ratings