From 4168bc466687194955c8f0b9949e6c1ac5b0d50b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 19 Jan 2024 15:20:47 +0100 Subject: [PATCH] less noisy logcat. catch illegal state exception as result of race condition --- .../transports/WebRTCDataChannelTransport.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/transports/WebRTCDataChannelTransport.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/transports/WebRTCDataChannelTransport.java index 74d9d91bf..e4dd730d5 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/transports/WebRTCDataChannelTransport.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/transports/WebRTCDataChannelTransport.java @@ -191,7 +191,6 @@ public class WebRTCDataChannelTransport implements Transport { new OnMessageObserver() { @Override public void onMessage(final DataChannel.Buffer buffer) { - Log.d(Config.LOGTAG, "onMessage() (the other one)"); try { WebRTCDataChannelTransport.this.writableByteChannel.write(buffer.data); } catch (final IOException e) { @@ -584,8 +583,7 @@ public class WebRTCDataChannelTransport implements Transport { Log.d(Config.LOGTAG, "DataChannelWriter reached EOF"); return; } - dataChannel.send( - new DataChannel.Buffer(ByteBuffer.wrap(buffer, 0, count), true)); + send(ByteBuffer.wrap(buffer, 0, count)); } } catch (final InterruptedException | InterruptedIOException e) { if (isSending.get()) { @@ -598,6 +596,16 @@ public class WebRTCDataChannelTransport implements Transport { } } + private void send(final ByteBuffer byteBuffer) throws IOException { + try { + dataChannel.send(new DataChannel.Buffer(byteBuffer, true)); + } catch (final IllegalStateException e) { + // dataChannel can be 'disposed' if we waited too long between `isSending` check and + // actually trying to send + throw new IOException(e); + } + } + public void close() { this.isSending.set(false); terminate(this.dataChannel);