Android 15+ socket connection terminates after about 4 seconds


I have an app that acts as a conduit between a primary app and a PC program. The conduit app responds to a broadcast from the primary app by opening socket communications with the PC program. It then sends data to the PC and receives a response using a single stream write/read:

socket = new Socket(hostName, port);
socket.setSoTimeout(120000);
...
os = socket.getOutputStream();
writeData(requestData, os);
// read response - status and (possibly) data
is = socket.getInputStream();
int status = is.read();

The data exchange normally takes a fraction of a second, unless user intervention is needed at the PC end which may delay the read completing for seconds or even minutes.

The app has worked without issue for 10 years or so, but now on Android >13 (I've tested 15 & 16, but not 14 or 17), if the read is delayed for more than approx. 4 seconds because of user intervention on the PC, an exception occurs: java.net.SocketException: Software caused connection abort

If the data exchange is fast (< 4 secs) then the problem doesn't happen.

The user interacts with the primary app, so the conduit app is always in the background. I've read that recent Android versions have become more aggressive with regard to background execution, so I followed the recommendation and wrapped the communications task (SyncTask in the log below) in a background Worker class, but this doesn't seem to have had any beneficial effect.

My observations so far:

  1. It's not a socket timeout I'm getting (which is set to 2 mins anyway)

  2. The time between read() and the socket closing is repeatably just under 4 seconds

  3. The issue doesn't appear to be the termination of background execution - the subsequent code (broadcast response sent) is being executed. It's just that the socket is being closed.

I've included a snippet from the logcat output. There's a bunch of messages from the WorkManager that I don't understand and that could be affecting how the worker task is being handled, but in view of observation 3. above they may not be relevant.

2026-07-27 10:25:38.227 23800-23800 SyncRequestReceiver     com.ceperman.sync   D  onReceive: sync request received
2026-07-27 10:25:38.372 23800-23816 JobInfo                 com.ceperman.sync   W  Requested important-while-foreground flag for job4 is ignored and takes no effect
2026-07-27 10:25:38.373 23800-23816 WM-SystemJobScheduler   com.ceperman.sync   D  Scheduling work ID d032d43e-76a2-4d1e-9714-b0459ddd53dbJob ID 4
2026-07-27 10:25:38.385 23800-23816 WM-GreedyScheduler      com.ceperman.sync   D  Starting work for d032d43e-76a2-4d1e-9714-b0459ddd53db
2026-07-27 10:25:38.390 23800-23800 WM-SystemJobService     com.ceperman.sync   D  onStartJob for WorkGenerationalId(workSpecId=d032d43e-76a2-4d1e-9714-b0459ddd53db, generation=0)
2026-07-27 10:25:38.410 23800-23816 WM-Processor            com.ceperman.sync   D  Processor: processing WorkGenerationalId(workSpecId=d032d43e-76a2-4d1e-9714-b0459ddd53db, generation=0)
2026-07-27 10:25:38.429 23800-23816 WM-Processor            com.ceperman.sync   D  Work WorkGenerationalId(workSpecId=d032d43e-76a2-4d1e-9714-b0459ddd53db, generation=0) is already enqueued for processing
2026-07-27 10:25:38.471 23800-23800 WM-WorkerWrapper        com.ceperman.sync   D  Starting work for com.ceperman.sync.BackgroundWorker
2026-07-27 10:25:38.473 23800-24081 BackgroundWorker        com.ceperman.sync   D  doWork
2026-07-27 10:25:38.479 23800-24082 SyncTask                com.ceperman.sync   D  run
2026-07-27 10:25:38.480 23800-23816 WM-PackageManagerHelper com.ceperman.sync   D  androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
2026-07-27 10:25:38.491 23800-23816 WM-WorkerWrapper        com.ceperman.sync   I  Worker result SUCCESS for Work [ id=d032d43e-76a2-4d1e-9714-b0459ddd53db, tags={ com.ceperman.sync.BackgroundWorker } ]
2026-07-27 10:25:38.505 23800-23800 WM-Processor            com.ceperman.sync   D  Processor d032d43e-76a2-4d1e-9714-b0459ddd53db executed; reschedule = false
2026-07-27 10:25:38.505 23800-23800 WM-SystemJobService     com.ceperman.sync   D  d032d43e-76a2-4d1e-9714-b0459ddd53db executed on JobScheduler
2026-07-27 10:25:38.512 23800-23816 WM-GreedyScheduler      com.ceperman.sync   D  Cancelling work ID d032d43e-76a2-4d1e-9714-b0459ddd53db
2026-07-27 10:25:38.563 23800-23816 WM-PackageManagerHelper com.ceperman.sync   D  androidx.work.impl.background.systemalarm.RescheduleReceiver disabled
2026-07-27 10:25:38.711 23800-24082 SyncTask                com.ceperman.sync   D  responseKey: 2700efbfbdefbfbd27cea2efbfbd
2026-07-27 10:25:38.725 23800-24082 SyncTask                com.ceperman.sync   D  Request packet sent to: 255.255.255.255 (DEFAULT)
2026-07-27 10:25:38.735 23800-24082 SyncTask                com.ceperman.sync   D  Request packet sent to: 192.168.1.255; Interface: wlan0
2026-07-27 10:25:38.839 23800-24082 SyncTask                com.ceperman.sync   D  UDP discovery successful, address is 192.168.1.83
2026-07-27 10:25:38.860 23800-24082 SyncTask                com.ceperman.sync   D  Reading from PCSecrets...
2026-07-27 10:25:38.870 23800-24082 SyncTask                com.ceperman.sync   D  Metadata received: keylength=256 (13)
2026-07-27 10:25:38.871 23800-24082 SyncTask                com.ceperman.sync   D  Keyword value: keyLength - 256
2026-07-27 10:25:39.527 23800-24082 SyncTask                com.ceperman.sync   D  Ciphers created with keylength 256
2026-07-27 10:25:39.568 23800-24082 SyncTask                com.ceperman.sync   D  id added to request data: 1f3d5a38384f3f51
2026-07-27 10:25:39.597 23800-24082 SyncTask                com.ceperman.sync   D  Writing to PCSecrets...
2026-07-27 10:25:39.600 23800-24082 SyncTask                com.ceperman.sync   D  Data send: 01223456790100101fef521e59973780bb1286cb82fb20ff05c73375566d442d... (64585)
2026-07-27 10:25:43.530 23800-24082 SyncTask                com.ceperman.sync   D  Exception during request/response processing: java.net.SocketException: Software caused connection abort
2026-07-27 10:25:43.532 23800-24082 SyncTask                com.ceperman.sync   D  Communication problem:java.net.SocketException: Software caused connection abort
2026-07-27 10:25:43.565 23800-24082 SyncTask                com.ceperman.sync   D  All finally closed
2026-07-27 10:25:43.567 23800-24082 SyncTask                com.ceperman.sync   D  OSADataResponse broadcast sent

Any ideas?

0
Jul 27 at 11:50 AM
User Avatarceperman
#java#android#sockets

No answer found for this question yet.