Commit b709b5ef authored by Daniel Wolf's avatar Daniel Wolf

joining the thread might be interrupted, cleanup the resources in any case

parent 89a61c8c
......@@ -127,55 +127,60 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
proxyThread = Thread {
logger?.info("Proxy started on thread " + Thread.currentThread().name)
try {
while(shouldRun) {
if (tunnelHandle?.hasDataForDevice() == true) {
deviceInputPipe.wantedEvents(0)
deviceOutputPipe.wantedEvents(OsConstants.POLLOUT)
} else {
deviceOutputPipe.wantedEvents(0)
deviceInputPipe.wantedEvents(deviceInputPipe.events.toInt() or OsConstants.POLLIN)
}
val pollableAnswers = createRemoteReadingPipes(errorStruct, deviceOutputPipe, deviceInputPipe)
forwardedPacketStore?.processNewAnswers(true)
tunnelHandle?.needsLocking = forwardedPacketStore?.hasMixedAnswerType ?: true
polling = true
val pollFinished = poll(pollableAnswers.first, 5)
polling = false
if (shouldRun && pollFinished) {
if (deviceInputPipe.isError() || deviceOutputPipe.isError()) {
logger?.info("Device pipe has read event ${deviceInputPipe.revents} instead of 0.")
shouldRun = false
} else if (errorStruct.revents.toInt() != 0) {
logger?.info("Error struct read returned ${errorStruct.revents} instead of 0.")
shouldRun = false
} else if (!deviceInputPipe.fd.valid() || !deviceOutputPipe.fd.valid()) {
logger?.info("Device filed descriptors became invalid")
shouldRun = false
try {
while(shouldRun) {
if (tunnelHandle?.hasDataForDevice() == true) {
deviceInputPipe.wantedEvents(0)
deviceOutputPipe.wantedEvents(OsConstants.POLLOUT)
} else {
if (pollableAnswers.second.isNotEmpty()) processFutureAnswers(pollableAnswers.second, true)
if (deviceOutputPipe.actualEvent(OsConstants.POLLOUT)) fromLoopbackToDevice(deviceOutput) // There is data in loopback
if (deviceInputPipe.actualEvent(OsConstants.POLLIN)) fromDeviceToPacketProxy(deviceInput)
deviceOutputPipe.wantedEvents(0)
deviceInputPipe.wantedEvents(deviceInputPipe.events.toInt() or OsConstants.POLLIN)
}
val pollableAnswers = createRemoteReadingPipes(errorStruct, deviceOutputPipe, deviceInputPipe)
forwardedPacketStore?.processNewAnswers(true)
tunnelHandle?.needsLocking = forwardedPacketStore?.hasMixedAnswerType ?: true
polling = true
val pollFinished = poll(pollableAnswers.first, 5)
polling = false
if (shouldRun && pollFinished) {
if (deviceInputPipe.isError() || deviceOutputPipe.isError()) {
logger?.info("Device pipe has read event ${deviceInputPipe.revents} instead of 0.")
shouldRun = false
} else if (errorStruct.revents.toInt() != 0) {
logger?.info("Error struct read returned ${errorStruct.revents} instead of 0.")
shouldRun = false
} else if (!deviceInputPipe.fd.valid() || !deviceOutputPipe.fd.valid()) {
logger?.info("Device filed descriptors became invalid")
shouldRun = false
} else {
if (pollableAnswers.second.isNotEmpty()) processFutureAnswers(pollableAnswers.second, true)
if (deviceOutputPipe.actualEvent(OsConstants.POLLOUT)) fromLoopbackToDevice(deviceOutput) // There is data in loopback
if (deviceInputPipe.actualEvent(OsConstants.POLLIN)) fromDeviceToPacketProxy(deviceInput)
Thread.sleep(1)
}
} else if (shouldRun) {
Thread.sleep(1)
}
} else if (shouldRun) {
Thread.sleep(1)
}
stop()
packetProxy.clearTunnelHandle()
} catch (ex: InterruptedIOException) {
errorLogger?.logException(ex, false)
shouldRun = false
} catch (ex:InterruptedException) {
shouldRun = false
} catch (ex:java.lang.Exception) {
if (exceptionCallbacks.isEmpty()) throw ex
else errorOccurred(ex)
}
stop()
packetProxy.clearTunnelHandle()
} catch (ex: InterruptedIOException) {
errorLogger?.logException(ex, false)
shouldRun = false
} catch (ex:InterruptedException) {
shouldRun = false
} catch (ex:java.lang.Exception) {
if (exceptionCallbacks.isEmpty()) throw ex
else errorOccurred(ex)
nonPollableProxyThread?.join()
} catch (ignored:InterruptedException) {
} finally {
logger?.info("Proxy thread ended.")
finalizeCleanup()
}
logger?.info("Proxy thread ended.")
nonPollableProxyThread?.join()
finalizeCleanup()
}
if(forwardingMode != ForwardingMode.ONLY_POLLABLE) nonPollableProxyThread = Thread {
logger?.info("Non-pollable proxy started on thread " + Thread.currentThread().name)
......
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