Skip to content

Commit c58265a

Browse files
committed
Only reset legacy TCP connections
1 parent 304b1e3 commit c58265a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

app/src/main/java/be/mygod/reactmap/util/UnblockCentral.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,4 @@ object UnblockCentral {
2727
Int::class.java)
2828
}
2929
}
30-
private val classStructLinger by lazy { init.let { Class.forName("android.system.StructLinger") } }
31-
val lingerReset by lazy {
32-
classStructLinger.getDeclaredConstructor(Int::class.java, Int::class.java).newInstance(1, 0)
33-
}
34-
val setsockoptLinger by lazy {
35-
Os::class.java.getDeclaredMethod("setsockoptLinger", FileDescriptor::class.java, Int::class.java,
36-
Int::class.java, classStructLinger)
37-
}
3830
}

app/src/main/java/be/mygod/reactmap/webkit/BaseReactMapFragment.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
398398

399399
fun terminate() {
400400
web.destroy()
401+
ReactMapHttpEngine.reset()
401402
try {
402403
for (file in File("/proc/self/fd").listFiles() ?: emptyArray()) try {
403404
val fdInt = file.name.toInt()
@@ -408,10 +409,9 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
408409
if (e.errno == OsConstants.EBADF || e.errno == OsConstants.ENOTSOCK) continue else throw e
409410
}
410411
if (endpoint !is InetSocketAddress) continue
411-
val isTcp = when (val type = UnblockCentral.getsockoptInt(null, fd, OsConstants.SOL_SOCKET,
412-
OsConstants.SO_TYPE)) {
413-
OsConstants.SOCK_STREAM -> true
414-
OsConstants.SOCK_DGRAM -> false
412+
when (val type = UnblockCentral.getsockoptInt(null, fd, OsConstants.SOL_SOCKET, OsConstants.SO_TYPE)) {
413+
OsConstants.SOCK_STREAM -> { }
414+
OsConstants.SOCK_DGRAM -> continue
415415
else -> {
416416
Timber.w(Exception("Unknown $type to $endpoint"))
417417
continue
@@ -423,12 +423,8 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
423423
Timber.w(e)
424424
0
425425
} else 0
426-
Timber.d("Resetting $fdInt owned by $ownerTag if is 0 -> $endpoint $isTcp")
427-
if (ownerTag != 0L) continue
428-
if (isTcp) {
429-
UnblockCentral.setsockoptLinger(null, fd, OsConstants.SOL_SOCKET,
430-
OsConstants.SO_LINGER, UnblockCentral.lingerReset)
431-
} else Os.dup2(nullFd, fdInt)
426+
Timber.d("Resetting $fdInt owned by $ownerTag if is 0 -> $endpoint")
427+
if (ownerTag == 0L) Os.dup2(nullFd, fdInt)
432428
} catch (e: Exception) {
433429
Timber.w(e)
434430
}

app/src/main/java/be/mygod/reactmap/webkit/ReactMapHttpEngine.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package be.mygod.reactmap.webkit
22

3+
import android.annotation.SuppressLint
34
import android.net.Uri
45
import android.net.http.ConnectionMigrationOptions
56
import android.net.http.HttpEngine
@@ -33,10 +34,11 @@ object ReactMapHttpEngine {
3334

3435
val isCronet get() = Build.VERSION.SDK_INT >= 34 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
3536
SdkExtensions.getExtensionVersion(Build.VERSION_CODES.S) >= 7
37+
private var engineInternal: HttpEngine? = null
3638
@get:RequiresExtension(Build.VERSION_CODES.S, 7)
37-
val engine by lazy @RequiresExtension(Build.VERSION_CODES.S, 7) {
38-
val cache = File(app.deviceStorage.cacheDir, "httpEngine")
39-
HttpEngine.Builder(app.deviceStorage).apply {
39+
val engine get() = synchronized(ReactMapHttpEngine) {
40+
engineInternal ?: HttpEngine.Builder(app.deviceStorage).apply {
41+
val cache = File(app.deviceStorage.cacheDir, "httpEngine")
4042
if (cache.mkdirs() || cache.isDirectory) {
4143
setStoragePath(cache.absolutePath)
4244
setEnableHttpCache(HttpEngine.Builder.HTTP_CACHE_DISK, 512 * 1024 * 1024)
@@ -46,7 +48,21 @@ object ReactMapHttpEngine {
4648
setPathDegradationMigration(ConnectionMigrationOptions.MIGRATION_OPTION_ENABLED)
4749
}.build())
4850
setEnableBrotli(true)
49-
}.build()
51+
}.build().also {
52+
engineInternal = it
53+
Timber.d("HttpEngine ${HttpEngine.getVersionString()} initialized")
54+
}
55+
}
56+
@SuppressLint("NewApi")
57+
fun reset() = synchronized(ReactMapHttpEngine) {
58+
engineInternal?.apply {
59+
try {
60+
shutdown()
61+
} catch (e: IllegalStateException) {
62+
Timber.d(e)
63+
}
64+
engineInternal = null
65+
}
5066
}
5167

5268
fun apiUrl(base: Uri) = base.buildUpon().apply {

0 commit comments

Comments
 (0)