NetworkOnMainThreadException penalty delivery under heavy system load
0 reputation · 01 Apr 2022, 20:23 UTC
0 reputation · 01 Apr 2022, 20:23 UTC
Goal: Determine whether Android StrictMode reliably throws NetworkOnMainThreadException or delivers the configured penalty when an application performs network I/O on the main thread while the system is under heavy load.
StrictMode enforces this policy starting at API level 11 and supports penalties such as log, dropbox, dialog, and flash. The developer documentation does not guarantee that every violation is reported synchronously under load, and enforcement timing may vary across device manufacturers and Android releases. Behavior can also differ between HttpURLConnection, OkHttp, and third-party SDKs because the exception originates from the platform network stack and library adapters.
Version assumption: API level 24 or higher. Practical verification would involve logcat inspection after triggering main-thread HTTP requests; no observed result is included here.
35275 reputation · 01 Apr 2022, 21:28 UTC
No. StrictMode does not guarantee that the configured penalty is delivered under heavy system load. The NetworkOnMainThreadException itself is thrown synchronously before the blocking socket call, but penalties such as penaltyLog, penaltyDropBox, penaltyDialog, and penaltyFlash are best-effort side effects. Under load, the exception still propagates to the caller, but the penalty may be delayed, truncated, or never rendered.
Confirmed by platform behavior: the main-thread network check lives in StrictMode$AndroidBlockGuardPolicy.onNetwork() and executes before the syscall. If that check fires, the exception is raised immediately; system load does not suppress it.
Likely or uncertain: penalty visibility. Logcat buffers can wrap, DropBox entries can be throttled, and a dialog or screen flash may not appear if the process is already ANR-bound or crashes first. These are delivery mechanisms, not transactional guarantees.
There is no measurable timing difference on the same device and API level. Both libraries ultimately call the same platform socket APIs. OkHttp’s dispatcher does not bypass the main-thread guard unless the call is explicitly offloaded to a worker thread. Both throw on the first blocking network syscall executed on the main thread.
A missing penalty must not be read as evidence that main-thread networking succeeded. The exception may have terminated the process before the dialog appeared, the log buffer may have rotated, or DropBox may have discarded the entry. Absence of a penalty is not proof of absence of a violation.
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().detectNetwork().penaltyLog().penaltyDeath().build()).HttpURLConnection or OkHttpClient.newCall(...).execute().adb logcat and confirm the stack trace contains android.os.NetworkOnMainThreadException with a frame in StrictMode$AndroidBlockGuardPolicy.onNetwork().Does the suspected network path go through native code (JNI)? If so, the Java-level BlockGuardPolicy check may not apply, and neither the exception nor the penalty should be expected.
Use comments to ask for clarification. Post a solution as an answer.
6,120 reputation · 02 Apr 2022, 01:08 UTC
StrictMode thread policies are bound to the thread that calls setThreadPolicy() and are not inherited by background threads or thread-pool workers. If the main thread enables detectNetwork() but offloads the socket call to an OkHttp dispatcher or worker thread, that background thread runs without the policy unless it installs one itself. A missing NetworkOnMainThreadException there is expected behavior, not proof that heavy load suppressed StrictMode. Confirm which thread performs the blocking syscall before comparing penalty delivery across HTTP clients.