Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
a3e9dcb0
Commit
a3e9dcb0
authored
Feb 08, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove hidden gotchas in GuardedProcessPool
parent
490e0432
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
24 deletions
+28
-24
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
+3
-1
core/src/main/java/com/github/shadowsocks/bg/GuardedProcessPool.kt
...main/java/com/github/shadowsocks/bg/GuardedProcessPool.kt
+23
-20
core/src/main/java/com/github/shadowsocks/bg/LocalDnsService.kt
...rc/main/java/com/github/shadowsocks/bg/LocalDnsService.kt
+2
-3
No files found.
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
View file @
a3e9dcb0
...
...
@@ -317,7 +317,7 @@ object BaseService {
data
.
processes
=
GuardedProcessPool
{
printLog
(
it
)
data
.
connectingJob
?.
apply
{
runBlocking
{
cancelAndJoin
()
}
}
data
.
connectingJob
?.
cancelAndJoin
()
stopRunner
(
false
,
it
.
localizedMessage
)
}
startProcesses
()
...
...
@@ -327,6 +327,8 @@ object BaseService {
RemoteConfig
.
fetch
()
data
.
changeState
(
CONNECTED
)
}
catch
(
_
:
CancellationException
)
{
// if the job was cancelled, it is canceller's responsibility to call stopRunner
}
catch
(
_
:
UnknownHostException
)
{
stopRunner
(
false
,
getString
(
R
.
string
.
invalid_server
))
}
catch
(
exc
:
Throwable
)
{
...
...
core/src/main/java/com/github/shadowsocks/bg/GuardedProcessPool.kt
View file @
a3e9dcb0
...
...
@@ -38,7 +38,7 @@ import java.io.IOException
import
java.io.InputStream
import
kotlin.concurrent.thread
class
GuardedProcessPool
(
private
val
onFatal
:
(
IOException
)
->
Unit
)
:
CoroutineScope
{
class
GuardedProcessPool
(
private
val
onFatal
:
suspend
(
IOException
)
->
Unit
)
:
CoroutineScope
{
companion
object
{
private
const
val
TAG
=
"GuardedProcessPool"
private
val
pid
by
lazy
{
...
...
@@ -59,7 +59,9 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
}
suspend
fun
abort
()
{
if
(!
abortChannel
.
isClosedForSend
)
abortChannel
.
send
(
Unit
)
if
(
abortChannel
.
isClosedForSend
)
return
abortChannel
.
send
(
Unit
)
abortChannel
.
close
()
}
suspend
fun
looper
(
onRestartCallback
:
(
suspend
()
->
Unit
)?)
{
...
...
@@ -88,31 +90,32 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
}
}
catch
(
e
:
IOException
)
{
Crashlytics
.
log
(
Log
.
WARN
,
TAG
,
"error occurred. stop guard: "
+
Commandline
.
toString
(
cmd
))
// calling callback without closing channel first will cause deadlock, therefore we defer it
GlobalScope
.
launch
(
Dispatchers
.
Main
)
{
onFatal
(
e
)
}
}
finally
{
abortChannel
.
close
()
}
if
(!
running
)
return
// process already exited, nothing to be done
if
(
Build
.
VERSION
.
SDK_INT
<
24
)
{
try
{
Os
.
kill
(
pid
.
get
(
process
)
as
Int
,
OsConstants
.
SIGTERM
)
}
catch
(
e
:
ErrnoException
)
{
if
(
e
.
errno
!=
OsConstants
.
ESRCH
)
throw
e
if
(!
running
)
return
// process already exited, nothing to be done
withContext
(
NonCancellable
)
{
// clean-up cannot be cancelled
if
(
Build
.
VERSION
.
SDK_INT
<
24
)
{
try
{
Os
.
kill
(
pid
.
get
(
process
)
as
Int
,
OsConstants
.
SIGTERM
)
}
catch
(
e
:
ErrnoException
)
{
if
(
e
.
errno
!=
OsConstants
.
ESRCH
)
throw
e
}
if
(
withTimeoutOrNull
(
500
)
{
exitChannel
.
receive
()
}
!=
null
)
return
@withContext
}
process
.
destroy
()
// kill the process
if
(
Build
.
VERSION
.
SDK_INT
>=
26
)
{
if
(
withTimeoutOrNull
(
1000
)
{
exitChannel
.
receive
()
}
!=
null
)
return
@withContext
process
.
destroyForcibly
()
// Force to kill the process if it's still alive
}
exitChannel
.
receive
()
}
if
(
withTimeoutOrNull
(
500
)
{
exitChannel
.
receive
()
}
!=
null
)
return
}
process
.
destroy
()
// kill the process
if
(
Build
.
VERSION
.
SDK_INT
>=
26
)
{
if
(
withTimeoutOrNull
(
1000
)
{
exitChannel
.
receive
()
}
!=
null
)
return
process
.
destroyForcibly
()
// Force to kill the process if it's still alive
}
exitChannel
.
receive
()
}
}
private
val
supervisor
=
Supervisor
Job
()
override
val
coroutineContext
get
()
=
Dispatchers
.
Main
+
super
visor
private
val
job
=
Job
()
override
val
coroutineContext
get
()
=
Dispatchers
.
Main
+
job
private
val
guards
=
ArrayList
<
Guard
>()
@MainThread
...
...
@@ -127,6 +130,6 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
@MainThread
suspend
fun
close
()
{
guards
.
forEach
{
it
.
abort
()
}
super
visor
.
children
.
forEach
{
it
.
join
()
}
// we can't cancel the supervisor as we need it to do clean up
job
.
cancelAndJoin
()
}
}
core/src/main/java/com/github/shadowsocks/bg/LocalDnsService.kt
View file @
a3e9dcb0
...
...
@@ -46,7 +46,7 @@ object LocalDnsService {
super
.
startProcesses
()
val
profile
=
data
.
proxy
!!
.
profile
val
dns
=
URI
(
"dns://${profile.remoteDns}"
)
servers
[
this
]
=
LocalDnsServer
(
this
::
resolver
,
LocalDnsServer
(
this
::
resolver
,
Socks5Endpoint
(
dns
.
host
,
if
(
dns
.
port
<
0
)
53
else
dns
.
port
),
DataStore
.
proxyAddress
).
apply
{
tcp
=
!
profile
.
udpdns
...
...
@@ -58,8 +58,7 @@ object LocalDnsService {
Acl
.
CHINALIST
->
{
}
else
->
forwardOnly
=
true
}
start
(
InetSocketAddress
(
DataStore
.
listenAddress
,
DataStore
.
portLocalDns
))
}
}.
also
{
servers
[
this
]
=
it
}.
start
(
InetSocketAddress
(
DataStore
.
listenAddress
,
DataStore
.
portLocalDns
))
}
override
suspend
fun
killProcesses
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment