facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.65k stars 24.29k forks source link

Xcode 10 runtime warning on fresh install: nw_socket_handle_socket_event #21030

Closed mog3n closed 5 years ago

mog3n commented 6 years ago

Description

While running the boilerplate react native app, I receive the runtime error: 2018-09-09 21:46:07.681496-0400 client[2035:560210] [] nw_socket_handle_socket_event [C6.2:1] Socket SO_ERROR [61: Connection refused] 2018-09-09 21:46:07.681861-0400 client[2035:560216] [] nw_connection_get_connected_socket [C6] Client called nw_connection_get_connected_socket on unconnected nw_connection 2018-09-09 21:46:07.681903-0400 client[2035:560216] TCP Conn 0x2821ac300 Failed : error 0:61 [61] which loops over and over for the duration of debugging the app on a device. I'm not sure exactly what this error is associated with, however in my other app I receive the same error. This may or may not be related to remote push notifications (APNs).

Environment

React Native Environment Info: System: OS: macOS 10.14 CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz Memory: 27.59 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 9.8.0 - /usr/local/bin/node Yarn: 1.5.1 - /usr/local/bin/yarn npm: 5.6.0 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3 IDEs: Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild npmPackages: react: 16.4.1 => 16.4.1 react-native: 0.56.0 => 0.56.0 npmGlobalPackages: create-react-native-app: 1.0.0 react-native-cli: 2.0.1

Reproducible Demo

  1. react-native init
  2. react-native run-ios
  3. In Capabilities, enable both 'Notifications' and Background Modes -> Enable 'Remote notifications'
  4. Open Xcode and select a connected iPhone
  5. Launch/debug the app and open the Xcode console

A couple of notes:

mog3n commented 6 years ago

UPDATE This only happens when I run it on an iOS 12 device!!! (Doesn't matter what I build with) Reverting back to iOS 11 for now.

yacut commented 6 years ago

same issue with 0.57.0 version:

react-native info
  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
      Memory: 690.77 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 8.11.4 - /usr/local/bin/node
      Yarn: 1.9.4 - /usr/local/bin/yarn
      npm: 5.6.0 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
    IDEs:
      Android Studio: 3.1 AI-173.4907809
      Xcode: 10.0/10A255 - /usr/bin/xcodebuild
    npmPackages:
      @types/react-native: ^0.56.21 => 0.56.21 
      react: 16.5.1 => 16.5.1 
      react-native: ^0.57.0 => 0.57.0 
nebulashine commented 6 years ago

Is this resolved? I'm also seeing messages like: 2018-09-18 15:25:36.721046-0700 AwesomeProject[18908:21074308] [] nw_socket_handle_socket_event [C4.1:1] Socket SO_ERROR [61: Connection refused] 2018-09-18 15:25:36.728264-0700 AwesomeProject[18908:21074308] [] nw_socket_handle_socket_event [C4.2:1] Socket SO_ERROR [61: Connection refused] 2018-09-18 15:25:36.734951-0700 AwesomeProject[18908:21074298] [] nw_connection_get_connected_socket [C4] Client called nw_connection_get_connected_socket on unconnected nw_connection

BANG88 commented 6 years ago

Temporary fix:

Edit Scheme => Run => Environment Variables => Add OS_ACTIVITY_MODE:disable

fulvio-m commented 6 years ago

Temporary fix:

Edit Scheme => Run => Environment Variables => Add OS_ACTIVITY_MODE:disable

This works fine, but keep in mind that it also suppresses all NSLog output you may need for debugging.

Jackyaung commented 6 years ago

I also have the same problem, it keeps running and the memory is getting big and big, when it reached 15G, my computer crushed

jnicholls commented 6 years ago

Any info on this? I am too seeing a spew of these errors in debug output when running my app on a device. I haven't tracked memory consumption on the device yet but I will do that soon to see if this is a show-stopping issue that would prevent me from releasing my app.

EDIT: While I haven't seen me any absurd memory growth, it is chewing through an abnormal amount of CPU while in this cycle of establishing connections.

anthonywebb commented 6 years ago

Still broke :(

divsbhalala commented 6 years ago

I also having same issue with xcode 10 and IOS12

vemundeldegard commented 6 years ago

Same issue on Xcode 10 and IOS12.

weitieda commented 6 years ago

Same issue on Xcode 10 and IOS12.

nerdycat commented 6 years ago

Same with iOS 12.

yehia123 commented 6 years ago

I am receiving this error ReactProject[12849:185872] [] nw_connection_get_connected_socket [C62] Client called nw_connection_get_connected_socket on unconnected nw_connection and my computer becomes completely slow while running it.

jaltin commented 6 years ago

+1

pukaponb commented 6 years ago

+1

abegehr commented 5 years ago

The same problem for me on Xcode 10 iOS 12 Simulator.

Temporary fix:

Edit Scheme => Run => Environment Variables => Add OS_ACTIVITY_MODE:disable

This fixes the issue on iOS 12 Simulator. Could someone explain what this does?

I will test later on iOS 11.

prasadpilla commented 5 years ago

I am facing the same issue as well.

wardhanster commented 5 years ago

same !

ZachOrr commented 5 years ago

Hey all - “Same” and “+1” are not particularly helpful for this discussion.

harrisrobin commented 5 years ago

Weird,

doing

rm -rf $TMPDIR/react-* && watchman watch-del-all && yarn cache clean

worked for me. Maybe it wouldn't hurt to add a --reset-cache after the start script, but I did not have to do that!

erez-guesty commented 5 years ago

Same issue on Xcode 10 and IOS12

cjadhav commented 5 years ago

If someone is looking for temporary get rid of this issue then apply filter in XCode console with [info][tid:com.facebook.react.JavaScript]

So we can see our required debug log.. 😆

bradennapier commented 5 years ago

Also running into this.

mmmulani commented 5 years ago

please stop commenting with +1/"me too". It's more frustrating than helpful.

about this issue, it's caused by Apple changing the error messages in their network stack making our fishhook logic no longer work. (Found here: https://github.com/facebook/react-native/blob/master/Libraries/WebSocket/RCTReconnectingWebSocket.m)

@maicki do you have any spare cycles to take a look at getting this working?

dbreese commented 5 years ago

No idea if this helps, but just an observation -- when I see this issue, I can watch the number of ports being reported for Xcode via Activity Monitor keeps climbing. On my Mac Pro trashcan, OSX will eventually become unusable and crash, usually when the ports climb to around 200,000.

Looks like the number of ports grows by about 20 every 2 seconds.

I don't see any odd data via netstat or lsof.

Hardly any CPU usage for me, just the ports slowly climbing and eventually OSX crashing and requiring a power cycle to recover.

xcode ports

janhesters commented 5 years ago

Temporary fix:

Edit Scheme => Run => Environment Variables => Add OS_ACTIVITY_MODE:disable

This fix is devious, because even though it suppresses the warning, your computer will eventually crash.

fulvio-m commented 5 years ago

When i run in Simulator or on Device, logs can be filtered using Console.app:

  1. Select your device (simulator or real device) on the left
  2. Right click on your application process and select "show process 'MyApp'"

Now you're only seeing output from your "MyApp" and you can start filtering out unwanted lines by right clicking them and selecting "Hide library 'theLibrary'". To get clean logs i hide:

It's a shame these filters cannot be saved, but at least console logging is now usable again.

anilmanukonda commented 5 years ago

Simplest way to turn off the Devtools connection in debug builds with embedded bundle file is, by adding these compiler flags into the Xcode project settings

ENABLE_PACKAGER_CONNECTION=0 RCT_ENABLE_INSPECTOR=0

Note: This completely disables debugging capability and will not even start the socket connection to the bundle location

msand commented 5 years ago

I have a workaround available, requires patching the inspector and react-devtools. Check the two PRs that just referenced this issue.

msand commented 5 years ago

If you don't want to wait for a new release to fix this, you can use patch-package and this for rn: react-native+0.57.5.patch

patch-package
--- a/node_modules/react-native/React/Inspector/RCTInspectorPackagerConnection.m
+++ b/node_modules/react-native/React/Inspector/RCTInspectorPackagerConnection.m
@@ -196,7 +196,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
     [self abort:@"Websocket exception"
       withCause:error];
   }
-  if (!_closed) {
+  if (!_closed && [error code] != ECONNREFUSED) {
     [self reconnect];
   }
 }

and react-devtools: react-devtools-core+3.4.3.patch

patch-package
--- a/node_modules/react-devtools-core/build/backend.js
+++ b/node_modules/react-devtools-core/build/backend.js
@@ -23,11 +23,16 @@
                     return connectToDevTools(options);
                 }, 2e3);
             }
-            function handleClose() {
-                hasClosed || (hasClosed = !0, scheduleRetry(), closeListeners.forEach(function(fn) {
-                    return fn();
-                }));
-            }
+          function handleClose() {
+            hasClosed || (hasClosed = !0, scheduleRetry(), closeListeners.forEach(function(fn) {
+              return fn();
+            }));
+          }
+          function handleError(err) {
+            hasClosed || (hasClosed = !0, closeListeners.forEach(function(fn) {
+              return fn();
+            }));
+          }
             function handleMessage(evt) {
                 var data;
                 try {
@@ -48,7 +53,7 @@
             } : _ref$isAppActive;
             if (!isAppActive()) return void scheduleRetry();
             var messageListeners = [], closeListeners = [], uri = "ws://" + host + ":" + port, ws = websocket ? websocket : new window.WebSocket(uri);
-            ws.onclose = handleClose, ws.onerror = handleClose, ws.onmessage = handleMessage, 
+            ws.onclose = handleClose, ws.onerror = handleError, ws.onmessage = handleMessage,
             ws.onopen = function() {
                 var wall = {
                     listen: function(fn) {
ghost commented 5 years ago

patch-package --- a/node_modules/react-native/React/Inspector/RCTInspectorPackagerConnection.m +++ b/node_modules/react-native/React/Inspector/RCTInspectorPackagerConnection.m @@ -196,7 +196,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) [self abort:@"Websocket exception" withCause:error]; }

rudyryk commented 5 years ago

Thank you @msand, you're saver!

I think those "+1" and "same here" were helpful after all. The problem here was not "how to fix it". Everything actually was working, it's just some React Native optional internals & tools were broken. Developers absolutely can live without them. And there were no info about how to just opt out dev tools and it's auto-reconnect.

Seriously. Why do we need to fix auto-reconnect logic for dev-tools? We may just disable dev-tools and work happily.

Patch did it's work perfectly! But also we could solve that much easier by just disabling auto-reconnect at all and save tons of nerves many months ago. And yes, it wasn't clear at all where these log lines come from and how to disable dev-tools auto-reconnect.

It's sometimes amazing how React Native is trying to force us to use something that we developers actually don't need and something that makes experience even worse.

nihp commented 5 years ago

In my Log I am also facing the same issue. Also Command + r not refreshing the simulator

riobijanero commented 5 years ago

If you don't want to wait for a new release to fix this, you can use patch-package and this for rn: react-native+0.57.5.patch

patch-package
--- a/node_modules/react-native/React/Inspector/RCTInspectorPackagerConnection.m
+++ b/node_modules/react-native/React/Inspector/RCTInspectorPackagerConnection.m
@@ -196,7 +196,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
     [self abort:@"Websocket exception"
       withCause:error];
   }
-  if (!_closed) {
+  if (!_closed && [error code] != ECONNREFUSED) {
     [self reconnect];
   }
 }

and react-devtools: react-devtools-core+3.4.3.patch

patch-package
--- a/node_modules/react-devtools-core/build/backend.js
+++ b/node_modules/react-devtools-core/build/backend.js
@@ -23,11 +23,16 @@
                     return connectToDevTools(options);
                 }, 2e3);
             }
-            function handleClose() {
-                hasClosed || (hasClosed = !0, scheduleRetry(), closeListeners.forEach(function(fn) {
-                    return fn();
-                }));
-            }
+          function handleClose() {
+            hasClosed || (hasClosed = !0, scheduleRetry(), closeListeners.forEach(function(fn) {
+              return fn();
+            }));
+          }
+          function handleError(err) {
+            hasClosed || (hasClosed = !0, closeListeners.forEach(function(fn) {
+              return fn();
+            }));
+          }
             function handleMessage(evt) {
                 var data;
                 try {
@@ -48,7 +53,7 @@
             } : _ref$isAppActive;
             if (!isAppActive()) return void scheduleRetry();
             var messageListeners = [], closeListeners = [], uri = "ws://" + host + ":" + port, ws = websocket ? websocket : new window.WebSocket(uri);
-            ws.onclose = handleClose, ws.onerror = handleClose, ws.onmessage = handleMessage, 
+            ws.onclose = handleClose, ws.onerror = handleError, ws.onmessage = handleMessage,
             ws.onopen = function() {
                 var wall = {
                     listen: function(fn) {

@msand how do I apply this patch?

msand commented 5 years ago

@riobijanero check the documentation of patch-package https://github.com/ds300/patch-package/blob/master/README.md#set-up

msand commented 5 years ago

In short, save those two patches as two files in a folder called patches in your project root (same as where your package.json lives) add patch-package as a dev dependency, and add it as a postinstall script to be run on each yarn/npm install

msand commented 5 years ago

Btw, I mostly made these patches to point out that there's significant value in changing the logic in at least these two places to something else, but the change I made isn't anywhere near the best logic for handling these errors in general. Ideally it would treat different types of errors differently, and warn if e.g. the devtools / inspector ports aren't open, and, at least if the connection attempt fails some number of times without a single successful attempt, it would stop trying. The option not to even attempt connecting would also be a nice option to have easily available/documented.

wanghe826 commented 5 years ago

please stop commenting with +1/"me too". It's more frustrating than helpful.

about this issue, it's caused by Apple changing the error messages in their network stack making our fishhook logic no longer work. (Found here: https://github.com/facebook/react-native/blob/master/Libraries/WebSocket/RCTReconnectingWebSocket.m)

@maicki do you have any spare cycles to take a look at getting this working?

stop Remote JS Debugging fix this issue .

mmmulani commented 5 years ago

I would much rather fix the logging issue than disable RCTReconnectingWebSocket. Right now it's used to let people connect the packager at any time instead of only looking for it on startup.

If we were to remove it, I'm sure we'd get questions from people asking why their app isn't connecting to the packager

gatspy commented 5 years ago

update 0.57.0 to 0.57.8, i have same issue.

react-native info:

  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
      Memory: 88.89 MB / 16.00 GB
      Shell: 5.6.2 - /usr/local/bin/zsh
    Binaries:
      Node: 10.15.0 - ~/.node/v10.15.0/bin/node
      Yarn: 1.13.0 - ~/.node/v10.15.0/bin/yarn
      npm: 6.5.0 - ~/.node/v10.15.0/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        Build Tools: 20.0.0, 21.1.2, 22.0.1, 23.0.1, 23.0.3, 24.0.3, 25.0.0, 25.0.3, 26.0.2, 26.0.3, 27.0.3, 28.0.3
        API Levels: 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28
    IDEs:
      Android Studio: 3.2 AI-181.5540.7.32.5056338
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.7.0 => 16.7.0
      react-native: 0.57.8 => 0.57.8
    npmGlobalPackages:
      react-native-cli: 2.0.1
ronp001 commented 5 years ago

@riobijanero your patch worked wonderfully with react-devtools-core@3.3.4, but react-devtools-core@3.6.0 is minified, so unfortunately the patch is no longer functional.

dslounge commented 5 years ago

@rickhanlonii This is the issue I mentioned on Twitter. I think this has been a historical dev-ex issue in RN. What I've heard is that RN is set up to try to connect to react-devtools because that's how FB works internally. But a lot of us using the open source build don't use react-devtools, so we get a lot of connection error spam looking at the logs. A solution might be for RN to give up trying to connect to devtools after 10 seconds or so.

A related issue is that react-native log-ios hasn't worked for a while (cc @nparashuram) . I think xcode moved the logs at some point, and so you can't easily look at the ios console.logs without opening xcode, knowing the magical log command log stream --predicate '(processImagePath contains \"AppName\") and senderImageUUID == processImageUUID', or using a tool like reactotron.

If we could get react-native log-ios to work well without having to open xcode and without getting a lot of connection errors it'd make the developer experience 💯

rudyryk commented 5 years ago

@mmmulani Logging garbage is not coming from packager connection errors. I got packager running all the time and it's working as expected but I still can see a huge bunch of such logs. BTW on device there's twice much of them vs simulator.

mmmulani commented 5 years ago

oh interesting, could you set a breakpoint in RCTReconnectingWebSocket and see what’s creating it then?

rudyryk commented 5 years ago

@mmccartney Can't get breakpoint event triggered in RCTReconnectingWebSocket but I get it in RCTInspectorPackagerConnection.m @ method:

// analogous to InspectorPackagerConnection.Connection.onFailure(...)
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
{
  if (_webSocket) {
    [self abort:@"Websocket exception"
      withCause:error]; // ! breakpoint is activated here
  }
  if (!_closed) {
    [self reconnect]; // ! breakpoint is activated here
  }
}
zabojad commented 5 years ago

I do not use remote js debugging and haxe those logs generated every 2 or 3 seconds. Where does it come from exactly?

dslounge commented 5 years ago

This is coming from React native trying to connect to react-devtools, not the packaging server

On Fri, Jan 25, 2019, 09:14 Thomas Fétiveau <notifications@github.com wrote:

I do not use remote js debugging and haxe those logs generated every 2 or 3 seconds. Where does it come from exactly?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/facebook/react-native/issues/21030#issuecomment-457584735, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeDc5mSD-BX0d4GyvR-o3QSq0_MNqtUks5vGxFKgaJpZM4WglZ1 .

zabojad commented 5 years ago

trying to connect to react-devtools, not the packaging server

@dslounge you mean trying to connect to react-devtools instead of the packaging server?

I don't understand how did this happen, I did not have this error few days ago and I haven't changed anything to my config nor dependencies versions...

dslounge commented 5 years ago

I think it's been a constant issue for about a year or longer. Try turning on react-devtools and you'll see it go away.

On Fri, Jan 25, 2019, 11:34 Thomas Fétiveau <notifications@github.com wrote:

trying to connect to react-devtools, not the packaging server

@dslounge https://github.com/dslounge you mean trying to connect to react-devtools instead of the packaging server?

I don't understand how did this happen, I did not have this error few days ago and I haven't changed anything to my config nor dependencies versions...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebook/react-native/issues/21030#issuecomment-457632345, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeDczh_SAdJ-562jAAz1MTf_aGDRSCBks5vGzIngaJpZM4WglZ1 .

noahtallen commented 5 years ago

@msand Thanks for putting together those PRs! Do you have any updates or knowledge about when / if they'll be merged?

It's still an issue on 0.58.3 :)