[TIMOB-26030] Android: TCP read() and write() methods cause a crash when running on main UI thread
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2018-09-24T23:18:06.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 7.5.0 |
Components | Android |
Labels | android, socket, tcp |
Reporter | Joshua Quick |
Assignee | Joshua Quick |
Created | 2018-05-11T03:09:09.000+0000 |
Updated | 2018-11-16T19:23:50.000+0000 |
Description
*Summary:*
If the "tiapp.xml" file property Make sure property "run-on-main-thread" is set to
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 JavaNetworkOnMainThreadException
.
*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
File | Date | Size |
---|---|---|
TcpClientBlockingTest.js | 2018-05-11T03:01:19.000+0000 | 1207 |
PR (master): https://github.com/appcelerator/titanium_mobile/pull/10338
FR Passed. PR Merged.
Verified the fix on SDK 7.5.0.v20181005164109. No Crashing. Works fine. Closing.