Commit ba10c1c0 authored by Daniel Wolf's avatar Daniel Wolf

Use lock to prevent clearing early

parent b686e108
......@@ -7,7 +7,7 @@ group = 'com.frostnerd.utilskt'
ext {
libraryGroupId = group
libraryArtifactId = rootProject.name
libraryVersion = '3.1.11'
libraryVersion = '3.1.12'
}
android {
......
......@@ -14,7 +14,10 @@ import java.io.*
import java.net.DatagramSocket
import java.net.Socket
import java.util.concurrent.locks.ReentrantLock
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.withLock
import kotlin.concurrent.write
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -66,6 +69,7 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
var forwardingMode:ForwardingMode = ForwardingMode.MIXED
private var proxyThreadActive:Boolean = false
private var cleanupFinalized:Boolean = false
private val clearLock = ReentrantReadWriteLock()
constructor(packetProxy: PacketProxy,
trafficStats: TrafficStats = TrafficStats(0, 0),
......@@ -141,6 +145,8 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
proxyThread = Thread {
proxyThreadActive = true
logger?.info("Proxy started on thread " + Thread.currentThread().name)
val lock = clearLock.readLock()
lock.lock()
try {
try {
while(shouldRun) {
......@@ -193,11 +199,14 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
} finally {
logger?.info("Proxy thread ended.")
proxyThreadActive = false
lock.unlock()
finalizeCleanup()
}
}
if(forwardingMode != ForwardingMode.ONLY_POLLABLE) nonPollableProxyThread = Thread {
logger?.info("Non-pollable proxy started on thread " + Thread.currentThread().name)
val lock = clearLock.readLock()
lock.lock()
try {
while (shouldRun) {
forwardedPacketStore?.processNewAnswers(false)
......@@ -217,6 +226,8 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
} catch (ex:java.lang.Exception) {
if (exceptionCallbacks.isEmpty()) throw ex
else errorOccurred(ex)
} finally {
lock.unlock()
}
logger?.info("Non-pollable proxy ended. Should run: $shouldRun")
}
......@@ -274,17 +285,19 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
}
private fun finalizeCleanup() {
proxyThread = null
nonPollableProxyThread = null
for (fileDescriptor in pipesToClose) {
fileDescriptor.tryClosing()
clearLock.write {
proxyThread = null
nonPollableProxyThread = null
for (fileDescriptor in pipesToClose) {
fileDescriptor.tryClosing()
}
pipesToClose.clear()
forwardedPacketStore?.clear(proxyStopped)
tunnelHandle?.clear(false)
cleanupFinalized = true
cleanupCallbacks.forEach { it() }
cleanupCallbacks.clear()
}
pipesToClose.clear()
forwardedPacketStore?.clear(proxyStopped)
tunnelHandle?.clear(false)
cleanupFinalized = true
cleanupCallbacks?.forEach { it() }
cleanupCallbacks.clear()
}
@Throws(ErrnoException::class)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment