Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid doing IO operations on Android main thread #235

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

pepone
Copy link
Member

@pepone pepone commented Jan 9, 2025

This PR updates Android hello demo to avoid starting async invocations on the main thread. With 3.8 this would result in NetworkOnMainThreadException.

@@ -17,54 +17,6 @@ enum DeliveryMode
DATAGRAM,
DATAGRAM_BATCH;

ObjectPrx apply(ObjectPrx prx)
Copy link
Member Author

@pepone pepone Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the logic to the only place that was used.

// Create a key manager manager factory that uses the certificates from our KeyStore.
KeyManagerFactory keyManagerFactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "password".toCharArray());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required for the client to provide a certificate, the hello server requires a client certificate by default.

}
ObjectPrx prx = _communicator.stringToProxy(s);
prx = _mode.apply(prx);
var prx = HelloPrx.createProxy(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified to use createProxy.

@@ -91,8 +92,6 @@ else if(m.what == MSG_EXCEPTION || m.what == MSG_RESPONSE)
{
InitializationData initData = new InitializationData();

initData.executor = (Runnable runnable, Connection connection) -> _uiHandler.post(runnable);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want all up-calls to run on the UI thread, this can result in NetworkOnMainThreadException.

For example I got this one:

android.os.NetworkOnMainThreadException
	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1675)
	at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:54)
	at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
	at sun.nio.ch.IOUtil.write(IOUtil.java:51)
	at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:512)
	at com.zeroc.Ice.StreamSocket.write(StreamSocket.java:176)
	at com.zeroc.Ice.StreamSocket.write(StreamSocket.java:133)
	at com.zeroc.Ice.TcpTransceiver.write(TcpTransceiver.java:44)
	at com.zeroc.Ice.IdleTimeoutTransceiverDecorator.write(IdleTimeoutTransceiverDecorator.java:62)
	at com.zeroc.Ice.ConnectionI.write(ConnectionI.java:2525)
	at com.zeroc.Ice.ConnectionI.sendMessage(ConnectionI.java:1938)
	at com.zeroc.Ice.ConnectionI.sendAsyncRequest(ConnectionI.java:334)
	at com.zeroc.Ice.OutgoingAsync.invokeRemote(OutgoingAsync.java:130)
	at com.zeroc.Ice.ConnectRequestHandler.flushRequestsImpl(ConnectRequestHandler.java:212)
	at com.zeroc.Ice.ConnectRequestHandler.flushRequests(ConnectRequestHandler.java:193)
	at com.zeroc.Ice.ConnectRequestHandler.setConnection(ConnectRequestHandler.java:93)
	at com.zeroc.Ice.RoutableReference$3.setConnection(RoutableReference.java:703)
	at com.zeroc.Ice.OutgoingConnectionFactory$ConnectCallback.setConnection(OutgoingConnectionFactory.java:711)
	at com.zeroc.Ice.OutgoingConnectionFactory.finishGetConnection(OutgoingConnectionFactory.java:477)
	at com.zeroc.Ice.OutgoingConnectionFactory.-$$Nest$mfinishGetConnection(Unknown Source:0)
	at com.zeroc.Ice.OutgoingConnectionFactory$ConnectCallback.connectionStartCompleted(OutgoingConnectionFactory.java:654)
	at com.zeroc.Ice.ConnectionI.upcall(ConnectionI.java:827)
	at com.zeroc.Ice.ConnectionI$4.run(ConnectionI.java:812)
	at android.os.Handler.handleCallback(Handler.java:958)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:205)

Copy link
Member

@externl externl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@pepone pepone merged commit 048e6ed into zeroc-ice:main Jan 9, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants