question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App terminated in background when WebSocket is open

See original GitHub issue

I using WebSocket in my app when app move to background it terminated in short time, but I expect OS suspended app. Test on IOS 13.1.2 release mode

log from device

default	21:26:15.204466 +0300	SpringBoard	Firing exit handlers for 63390 with context <RBSProcessExitContext; specific: <RBSProcessExitStatus; domain: runningboard (15); code: 0x2182baad>; terminationContext: <RBSTerminateContext: 0x6000032f29c0; code: 0x2182BAAD; explanation: "Timed-out waiting for process [application<org.reactjs.native.example.test-bg>:63390] to invalidate assertion with identifier 4170-63390-4366 and explanation 'Shared Background Assertion 2 for org.reactjs.native.example.test-bg'. Direct this report to owners of that process"; reportType: 0; maxRole: 7; maxTerminationResistance: 3>>
default	21:26:15.207509 +0300	SpringBoard	[application<org.reactjs.native.example.test-bg>:63390]* Process exited: <RBSProcessExitContext; specific: <RBSProcessExitStatus; domain: runningboard (15); code: 0x2182baad>; terminationContext: <RBSTerminateContext: 0x6000032f29c0; code: 0x2182BAAD; explanation: "Timed-out waiting for process [application<org.reactjs.native.example.test-bg>:63390] to invalidate assertion with identifier 4170-63390-4366 and explanation 'Shared Background Assertion 2 for org.reactjs.native.example.test-bg'. Direct this report to owners of that process"; reportType: 0; maxRole: 7; maxTerminationResistance: 3>>.
default	21:26:15.207568 +0300	SpringBoard	[application<org.reactjs.native.example.test-bg>:63390]* Setting process task state to: Not Running
default	21:26:15.207611 +0300	SpringBoard	[application<org.reactjs.native.example.test-bg>:63390]* Setting process visibility to: Unknown
default	21:26:15.207704 +0300	SpringBoard	Removing: <FBApplicationProcess: 0x7feb8f7225d0; application<org.reactjs.native.example.test-bg>:63390>
default	21:26:15.207906 +0300	SpringBoard	Process exited: <FBApplicationProcess: 0x7feb8f7225d0; application<org.reactjs.native.example.test-bg>:63390> -> <RBSProcessExitContext; specific: <RBSProcessExitStatus; domain: runningboard (15); code: 0x2182baad>; terminationContext: <RBSTerminateContext: 0x6000032f29c0; code: 0x2182BAAD; explanation: "Timed-out waiting for process [application<org.reactjs.native.example.test-bg>:63390] to invalidate assertion with identifier 4170-63390-4366 and explanation 'Shared Background Assertion 2 for org.reactjs.native.example.test-bg'. Direct this report to owners of that process"; reportType: 0; maxRole: 7; maxTerminationResistance: 3>>
default	21:26:15.208345 +0300	SpringBoard	Application process state changed for org.reactjs.native.example.test-bg: (null)

full log https://github.com/fonov/rn-websocket/blob/master/device_log.txt

it look like socket can’t finished task and OS kill app

React Native version:

  IDEs:
    Xcode: 11.0/11A420a - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.2 => 0.61.2 
  npmGlobalPackages:
    react-native-cli: 2.0.1

Steps To Reproduce

https://github.com/fonov/rn-websocket

i create fresh rn project and add this code

componentDidMount() {
    const ws = new WebSocket('wss://echo.websocket.org');

    ws.onopen = () => {
      ws.send('something');
    };

    ws.onmessage = e => {
      setTimeout(() => {
        ws.send('something');
      }, 1000);
    };

    ws.onerror = e => {
      // an error occurred
      console.warn(e.message);
    };

    ws.onclose = e => {
      // connection closed
      console.warn(e.code, e.reason);
    };
  }

in App.js

  1. clone repo
  2. run release app from the Home screen rather than from Xcode
  3. go to home screen
  4. wait a little bit and app is terminated

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
oleksandr-dziubancommented, Oct 8, 2019

@xiao99xiao @fonov @radko93 I have reproduced this even on fresh react-native app without any additional code!

Steps:

  1. Use iOS 13.0 or 13.1 Simulator, Xcode 11.0 or 11.1, react-native@0.61.2
  2. Generate fresh app: react-native init TestApp
  3. Run iOS app in Simulator: react-native run-ios --simulator="iPhone 11"
  4. Wait until application will be assembled and home screen activated
  5. Click on Hardware Power Button on the right side in Simulator, device will activate sleep mode
  6. Wait ~ 1 min and Unlock Simulator screen
  7. Application will be crashed
1reaction
fonovcommented, Oct 6, 2019

I feel a misunderstanding of my issue.

ios suspend app and it absolutely right, i expected this behavior, it part of app life cycle, after suspend app user may return to app and see the state he was in when he left the app, but terminated app is reset inner state

Normally change application life cycles

user open app, state changes from

not running -> inactive -> active

user make some work in app and go to home screen

active -> inactive -> background -> suspended

then user open app again and app move active state

suspended -> inactive -> active

and inner state app not reset, user stay on screen when he stay last time

With WebSocket change application life cycles

user open app, state changes from

not running -> inactive -> active

user make some work in app and go to home screen

active -> inactive -> background -> not running (terminated)

then user open app again

not running -> inactive -> active

inner state is lost

Now if used WebSocket from react-native, app when going in background mode can’t to complete task for a certain number of time and system terminated app and innner state reset

I expect when i use websocket in my app after hide app websoket finish tasks and app move to suspend mode and inner state not reset

I don’t think it’s react native issue then?

it is react-native issue, because websocket is part of RN

Also you can clone my test repo and test self terminated app in background and lost inner state

App-Based Life-Cycle Events https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle?language=objc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keep a live websocket when app is in terminated ( kill ) state
This websocket works fine in foreground and background state, but when I terminate ( kill ) app then websocket connection breaks.
Read more >
Websocket connection in background without VOIP
Hello, We need our app to be able to send coordinates in background and hold connection with server via websocktes.
Read more >
Problem in communication with SwiftNIO when server is in a ...
I.e. regular apps are stopped when they enter the background. If you have server components in the app, that usually means that you...
Read more >
react-native-background-fetch - npm
When your app is terminated, iOS no longer fires events — There is no such thing as stopOnTerminate: false for iOS.
Read more >
Is it possible to maintain a persistent websocket connection in ...
Websocket from background will likely mean your app will be killed off rater quickly, ... But a persistant websocket connection in the background...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found