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
fe9b773d
Commit
fe9b773d
authored
Jan 24, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Shutdown service on process exit too fast or restart failures
Also handles possible double shutdown.
parent
b6880efd
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
23 deletions
+40
-23
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
+10
-3
core/src/main/java/com/github/shadowsocks/bg/GuardedProcessPool.kt
...main/java/com/github/shadowsocks/bg/GuardedProcessPool.kt
+24
-19
core/src/main/java/com/github/shadowsocks/bg/LocalSocketListener.kt
...ain/java/com/github/shadowsocks/bg/LocalSocketListener.kt
+6
-1
No files found.
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
View file @
fe9b773d
...
...
@@ -77,6 +77,7 @@ object BaseService {
var
closeReceiverRegistered
=
false
val
binder
=
Binder
(
this
)
var
connectingJob
:
Job
?
=
null
fun
changeState
(
s
:
Int
,
msg
:
String
?
=
null
)
{
if
(
state
==
s
&&
msg
==
null
)
return
...
...
@@ -239,6 +240,7 @@ object BaseService {
}
fun
stopRunner
(
restart
:
Boolean
=
false
,
msg
:
String
?
=
null
)
{
if
(
data
.
state
==
STOPPING
)
return
// channge the state
data
.
changeState
(
STOPPING
)
GlobalScope
.
launch
(
Dispatchers
.
Main
,
CoroutineStart
.
UNDISPATCHED
)
{
...
...
@@ -302,14 +304,17 @@ object BaseService {
Core
.
analytics
.
logEvent
(
"start"
,
bundleOf
(
Pair
(
FirebaseAnalytics
.
Param
.
METHOD
,
tag
)))
data
.
changeState
(
CONNECTING
)
GlobalScope
.
launch
(
Dispatchers
.
Main
)
{
data
.
connectingJob
=
GlobalScope
.
launch
(
Dispatchers
.
Main
)
{
try
{
proxy
.
init
()
data
.
udpFallback
?.
init
()
killProcesses
()
data
.
processes
=
GuardedProcessPool
()
data
.
processes
=
GuardedProcessPool
{
printLog
(
it
)
data
.
connectingJob
?.
apply
{
runBlocking
{
cancelAndJoin
()
}
}
stopRunner
(
false
,
it
.
localizedMessage
)
}
startProcesses
()
proxy
.
scheduleUpdate
()
...
...
@@ -324,6 +329,8 @@ object BaseService {
printLog
(
exc
)
}
stopRunner
(
false
,
"${getString(R.string.service_failed)}: ${exc.localizedMessage}"
)
}
finally
{
data
.
connectingJob
=
null
}
}
return
Service
.
START_NOT_STICKY
...
...
core/src/main/java/com/github/shadowsocks/bg/GuardedProcessPool.kt
View file @
fe9b773d
...
...
@@ -38,7 +38,7 @@ import java.io.IOException
import
java.io.InputStream
import
kotlin.concurrent.thread
class
GuardedProcessPool
:
CoroutineScope
{
class
GuardedProcessPool
(
private
val
onFatal
:
(
IOException
)
->
Unit
)
:
CoroutineScope
{
companion
object
{
private
const
val
TAG
=
"GuardedProcessPool"
private
val
pid
by
lazy
{
...
...
@@ -47,14 +47,12 @@ class GuardedProcessPool : CoroutineScope {
}
private
inner
class
Guard
(
private
val
cmd
:
List
<
String
>)
{
val
abortChannel
=
Channel
<
Unit
>()
private
val
exitChannel
=
Channel
<
Int
>()
private
val
cmdName
=
File
(
cmd
.
first
()).
nameWithoutExtension
private
val
abortChannel
=
Channel
<
Unit
>()
private
var
startTime
:
Long
=
-
1
private
lateinit
var
process
:
Process
private
fun
streamLogger
(
input
:
InputStream
,
logger
:
(
String
,
String
)
->
In
t
)
=
try
{
input
.
bufferedReader
().
forEachLine
{
logger
(
TAG
,
it
)
}
private
fun
streamLogger
(
input
:
InputStream
,
logger
:
(
String
)
->
Uni
t
)
=
try
{
input
.
bufferedReader
().
forEachLine
(
logger
)
}
catch
(
_
:
IOException
)
{
}
// ignore
fun
start
()
{
...
...
@@ -62,14 +60,20 @@ class GuardedProcessPool : CoroutineScope {
process
=
ProcessBuilder
(
cmd
).
directory
(
Core
.
deviceStorage
.
noBackupFilesDir
).
start
()
}
suspend
fun
abort
()
{
if
(!
abortChannel
.
isClosedForSend
)
abortChannel
.
send
(
Unit
)
}
suspend
fun
looper
(
onRestartCallback
:
(()
->
Unit
)?)
{
var
running
=
true
val
cmdName
=
File
(
cmd
.
first
()).
nameWithoutExtension
val
exitChannel
=
Channel
<
Int
>()
try
{
while
(
true
)
{
thread
(
name
=
"stderr-$cmdName"
)
{
streamLogger
(
process
.
errorStream
,
Log
::
e
)
}
thread
(
name
=
"stderr-$cmdName"
)
{
streamLogger
(
process
.
errorStream
)
{
Log
.
e
(
cmdName
,
it
)
}
}
thread
(
name
=
"stdout-$cmdName"
)
{
runBlocking
{
streamLogger
(
process
.
inputStream
,
Log
::
i
)
streamLogger
(
process
.
inputStream
)
{
Log
.
i
(
cmdName
,
it
)
}
exitChannel
.
send
(
process
.
waitFor
())
// this thread also acts as a daemon thread for waitFor
}
}
...
...
@@ -78,21 +82,22 @@ class GuardedProcessPool : CoroutineScope {
exitChannel
.
onReceive
{
false
}
})
break
running
=
false
if
(
SystemClock
.
elapsedRealtime
()
-
startTime
<
1000
)
{
Crashlytics
.
log
(
Log
.
WARN
,
TAG
,
"process exit too fast, stop guard: $cmdName"
)
break
}
if
(
SystemClock
.
elapsedRealtime
()
-
startTime
<
1000
)
throw
IOException
(
"$cmdName exits too fast"
)
Crashlytics
.
log
(
Log
.
DEBUG
,
TAG
,
"restart process: "
+
Commandline
.
toString
(
cmd
))
start
()
running
=
true
onRestartCallback
?.
invoke
()
}
}
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
)
{
val
pid
=
pid
.
get
(
process
)
as
Int
try
{
Os
.
kill
(
pid
,
OsConstants
.
SIGTERM
)
Os
.
kill
(
pid
.
get
(
process
)
as
Int
,
OsConstants
.
SIGTERM
)
}
catch
(
e
:
ErrnoException
)
{
if
(
e
.
errno
!=
OsConstants
.
ESRCH
)
throw
e
}
...
...
@@ -123,7 +128,7 @@ class GuardedProcessPool : CoroutineScope {
@MainThread
suspend
fun
close
()
{
guards
.
forEach
{
it
.
abort
Channel
.
send
(
Unit
)
}
super
visor
.
children
.
forEach
{
it
.
join
()
}
guards
.
forEach
{
it
.
abort
(
)
}
super
visor
.
children
.
forEach
{
it
.
join
()
}
// we can't cancel the supervisor as we need it to do clean up
}
}
core/src/main/java/com/github/shadowsocks/bg/LocalSocketListener.kt
View file @
fe9b773d
...
...
@@ -23,6 +23,7 @@ package com.github.shadowsocks.bg
import
android.net.LocalServerSocket
import
android.net.LocalSocket
import
android.net.LocalSocketAddress
import
android.system.ErrnoException
import
android.system.Os
import
android.system.OsConstants
import
com.github.shadowsocks.utils.printLog
...
...
@@ -56,7 +57,11 @@ abstract class LocalSocketListener(name: String, socketFile: File) : Thread(name
override
fun
close
()
{
running
=
false
// see also: https://issuetracker.google.com/issues/36945762#comment15
try
{
Os
.
shutdown
(
localSocket
.
fileDescriptor
,
OsConstants
.
SHUT_RDWR
)
}
catch
(
e
:
ErrnoException
)
{
if
(
e
.
errno
!=
OsConstants
.
EBADF
)
throw
e
// suppress fd already closed
}
join
()
}
}
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