Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26030] Android: TCP read() and write() methods cause a crash when running on main UI thread

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2018-09-24T23:18:06.000+0000
Affected Version/sn/a
Fix Version/sRelease 7.5.0
ComponentsAndroid
Labelsandroid, socket, tcp
ReporterJoshua Quick
AssigneeJoshua Quick
Created2018-05-11T03:09:09.000+0000
Updated2018-11-16T19:23:50.000+0000

Description

*Summary:* If the "tiapp.xml" file property run-on-main-thread is set to true, then calling a Ti.Networking.Socket.TCP object's blocking read() or write() method will cause a crash on Android. *Steps to reproduce:*

Open the project's "tiapp.xml" file.

Make sure property "run-on-main-thread" is set to true.

Set up an Android device with Internet access.

Build and run [^TcpClientBlockingTest.js] on the Android device.

*Result:* The app crashes with a Java NetworkOnMainThreadException. *Cause:* The Java java.net.ServerSocket class will throw a NetworkOnMainThreadException when used on the main UI thread. It can only be used by a non-UI thread. *Work-Around:* Simplest solution is to set the tiapp.xml property "run-on-main-thread" to false. Best solution is to not use the blocking read() and write() methods and use the async Ti.Stream.pump() and Ti.Stream.write() methods instead as documented here... https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Network.Socket.TCP Note that even if we do fix the crash, we still don't recommend using the synchronous read() and write() method calls since they'll block the UI thread. This will make the UI appear to stutter or hang, providing a poor experience for the end-user.

Attachments

FileDateSize
TcpClientBlockingTest.js2018-05-11T03:01:19.000+00001207

Comments

  1. Joshua Quick 2018-09-24

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/10338
  2. Lokesh Choudhary 2018-09-24

    FR Passed. PR Merged.
  3. Keerthi Mahalingam 2018-10-08

    Verified the fix on SDK 7.5.0.v20181005164109. No Crashing. Works fine. Closing.
       Operating System
         Name                        = Mac OS X
         Version                     = 10.13.6
         Architecture                = 64bit
       Node.js
         Node.js Version             = 8.9.1
         npm Version                 = 5.5.1
       Titanium CLI
         CLI Version                 = 5.1.1
       Titanium SDK
         SDK Version                 =7.5.0.v20181005164109
       Device			      =
       					Samsung s5 android 6
       					Pixel 2 Android 8 emulator
       

JSON Source