Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
f8d0b21b
Unverified
Commit
f8d0b21b
authored
Apr 05, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Apr 05, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #75853 from roycaihw/fix/use-standard-stacktrace
apimachinery & apiserver: use stacktrace in the stdlib
parents
1ff857e1
999a02ce
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
24 deletions
+92
-24
runtime.go
staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime.go
+14
-19
runtime_test.go
.../src/k8s.io/apimachinery/pkg/util/runtime/runtime_test.go
+68
-0
rest.go
staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go
+3
-2
timeout.go
staging/src/k8s.io/apiserver/pkg/server/filters/timeout.go
+7
-3
No files found.
staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime.go
View file @
f8d0b21b
...
@@ -62,27 +62,18 @@ func HandleCrash(additionalHandlers ...func(interface{})) {
...
@@ -62,27 +62,18 @@ func HandleCrash(additionalHandlers ...func(interface{})) {
// logPanic logs the caller tree when a panic occurs.
// logPanic logs the caller tree when a panic occurs.
func
logPanic
(
r
interface
{})
{
func
logPanic
(
r
interface
{})
{
callers
:=
getCallers
(
r
)
// Same as stdlib http server code. Manually allocate stack trace buffer size
// to prevent excessively large logs
const
size
=
64
<<
10
stacktrace
:=
make
([]
byte
,
size
)
stacktrace
=
stacktrace
[
:
runtime
.
Stack
(
stacktrace
,
false
)]
if
_
,
ok
:=
r
.
(
string
);
ok
{
if
_
,
ok
:=
r
.
(
string
);
ok
{
klog
.
Errorf
(
"Observed a panic: %s
\n
%
v"
,
r
,
callers
)
klog
.
Errorf
(
"Observed a panic: %s
\n
%
s"
,
r
,
stacktrace
)
}
else
{
}
else
{
klog
.
Errorf
(
"Observed a panic: %#v (%v)
\n
%
v"
,
r
,
r
,
callers
)
klog
.
Errorf
(
"Observed a panic: %#v (%v)
\n
%
s"
,
r
,
r
,
stacktrace
)
}
}
}
}
func
getCallers
(
r
interface
{})
string
{
callers
:=
""
for
i
:=
0
;
true
;
i
++
{
_
,
file
,
line
,
ok
:=
runtime
.
Caller
(
i
)
if
!
ok
{
break
}
callers
=
callers
+
fmt
.
Sprintf
(
"%v:%v
\n
"
,
file
,
line
)
}
return
callers
}
// ErrorHandlers is a list of functions which will be invoked when an unreturnable
// ErrorHandlers is a list of functions which will be invoked when an unreturnable
// error occurs.
// error occurs.
// TODO(lavalamp): for testability, this and the below HandleError function
// TODO(lavalamp): for testability, this and the below HandleError function
...
@@ -155,13 +146,17 @@ func GetCaller() string {
...
@@ -155,13 +146,17 @@ func GetCaller() string {
// handlers to handle errors and panics the same way.
// handlers to handle errors and panics the same way.
func
RecoverFromPanic
(
err
*
error
)
{
func
RecoverFromPanic
(
err
*
error
)
{
if
r
:=
recover
();
r
!=
nil
{
if
r
:=
recover
();
r
!=
nil
{
callers
:=
getCallers
(
r
)
// Same as stdlib http server code. Manually allocate stack trace buffer size
// to prevent excessively large logs
const
size
=
64
<<
10
stacktrace
:=
make
([]
byte
,
size
)
stacktrace
=
stacktrace
[
:
runtime
.
Stack
(
stacktrace
,
false
)]
*
err
=
fmt
.
Errorf
(
*
err
=
fmt
.
Errorf
(
"recovered from panic %q. (err=%v) Call stack:
\n
%
v
"
,
"recovered from panic %q. (err=%v) Call stack:
\n
%
s
"
,
r
,
r
,
*
err
,
*
err
,
callers
)
stacktrace
)
}
}
}
}
...
...
staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime_test.go
View file @
f8d0b21b
...
@@ -17,7 +17,12 @@ limitations under the License.
...
@@ -17,7 +17,12 @@ limitations under the License.
package
runtime
package
runtime
import
(
import
(
"bytes"
"fmt"
"fmt"
"io"
"os"
"regexp"
"strings"
"testing"
"testing"
)
)
...
@@ -69,3 +74,66 @@ func TestCustomHandleError(t *testing.T) {
...
@@ -69,3 +74,66 @@ func TestCustomHandleError(t *testing.T) {
t
.
Errorf
(
"did not receive custom handler"
)
t
.
Errorf
(
"did not receive custom handler"
)
}
}
}
}
func
TestHandleCrashLog
(
t
*
testing
.
T
)
{
log
,
err
:=
captureStderr
(
func
()
{
defer
func
()
{
if
r
:=
recover
();
r
==
nil
{
t
.
Fatalf
(
"expected a panic to recover from"
)
}
}()
defer
HandleCrash
()
panic
(
"test panic"
)
})
if
err
!=
nil
{
t
.
Fatalf
(
"%v"
,
err
)
}
// Example log:
//
// ...] Observed a panic: test panic
// goroutine 6 [running]:
// command-line-arguments.logPanic(0x..., 0x...)
// .../src/k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime.go:69 +0x...
lines
:=
strings
.
Split
(
log
,
"
\n
"
)
if
len
(
lines
)
<
4
{
t
.
Fatalf
(
"panic log should have 1 line of message, 1 line per goroutine and 2 lines per function call"
)
}
if
match
,
_
:=
regexp
.
MatchString
(
"Observed a panic: test panic"
,
lines
[
0
]);
!
match
{
t
.
Errorf
(
"mismatch panic message: %s"
,
lines
[
0
])
}
// The following regexp's verify that Kubernetes panic log matches Golang stdlib
// stacktrace pattern. We need to update these regexp's if stdlib changes its pattern.
if
match
,
_
:=
regexp
.
MatchString
(
`goroutine [0-9]+ \[.+\]:`
,
lines
[
1
]);
!
match
{
t
.
Errorf
(
"mismatch goroutine: %s"
,
lines
[
1
])
}
if
match
,
_
:=
regexp
.
MatchString
(
`logPanic(.*)`
,
lines
[
2
]);
!
match
{
t
.
Errorf
(
"mismatch symbolized function name: %s"
,
lines
[
2
])
}
if
match
,
_
:=
regexp
.
MatchString
(
`runtime\.go:[0-9]+ \+0x`
,
lines
[
3
]);
!
match
{
t
.
Errorf
(
"mismatch file/line/offset information: %s"
,
lines
[
3
])
}
}
// captureStderr redirects stderr to result string, and then restore stderr from backup
func
captureStderr
(
f
func
())
(
string
,
error
)
{
r
,
w
,
err
:=
os
.
Pipe
()
if
err
!=
nil
{
return
""
,
err
}
bak
:=
os
.
Stderr
os
.
Stderr
=
w
defer
func
()
{
os
.
Stderr
=
bak
}()
resultCh
:=
make
(
chan
string
)
// copy the output in a separate goroutine so printing can't block indefinitely
go
func
()
{
var
buf
bytes
.
Buffer
io
.
Copy
(
&
buf
,
r
)
resultCh
<-
buf
.
String
()
}()
f
()
w
.
Close
()
return
<-
resultCh
,
nil
}
staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go
View file @
f8d0b21b
...
@@ -25,7 +25,6 @@ import (
...
@@ -25,7 +25,6 @@ import (
"net/http"
"net/http"
"net/url"
"net/url"
goruntime
"runtime"
goruntime
"runtime"
"strings"
"time"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/errors"
...
@@ -195,10 +194,12 @@ func finishRequest(timeout time.Duration, fn resultFunc) (result runtime.Object,
...
@@ -195,10 +194,12 @@ func finishRequest(timeout time.Duration, fn resultFunc) (result runtime.Object,
defer
func
()
{
defer
func
()
{
panicReason
:=
recover
()
panicReason
:=
recover
()
if
panicReason
!=
nil
{
if
panicReason
!=
nil
{
// Same as stdlib http server code. Manually allocate stack
// trace buffer size to prevent excessively large logs
const
size
=
64
<<
10
const
size
=
64
<<
10
buf
:=
make
([]
byte
,
size
)
buf
:=
make
([]
byte
,
size
)
buf
=
buf
[
:
goruntime
.
Stack
(
buf
,
false
)]
buf
=
buf
[
:
goruntime
.
Stack
(
buf
,
false
)]
panicReason
=
strings
.
TrimSuffix
(
fmt
.
Sprintf
(
"%v
\n
%s"
,
panicReason
,
string
(
buf
)),
"
\n
"
)
panicReason
=
fmt
.
Sprintf
(
"%v
\n
%s"
,
panicReason
,
buf
)
// Propagate to parent goroutine
// Propagate to parent goroutine
panicCh
<-
panicReason
panicCh
<-
panicReason
}
}
...
...
staging/src/k8s.io/apiserver/pkg/server/filters/timeout.go
View file @
f8d0b21b
...
@@ -92,23 +92,27 @@ func (t *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -92,23 +92,27 @@ func (t *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
return
}
}
errCh
:=
make
(
chan
interface
{})
// resultCh is used as both errCh and stopCh
resultCh
:=
make
(
chan
interface
{})
tw
:=
newTimeoutWriter
(
w
)
tw
:=
newTimeoutWriter
(
w
)
go
func
()
{
go
func
()
{
defer
func
()
{
defer
func
()
{
err
:=
recover
()
err
:=
recover
()
if
err
!=
nil
{
if
err
!=
nil
{
// Same as stdlib http server code. Manually allocate stack
// trace buffer size to prevent excessively large logs
const
size
=
64
<<
10
const
size
=
64
<<
10
buf
:=
make
([]
byte
,
size
)
buf
:=
make
([]
byte
,
size
)
buf
=
buf
[
:
runtime
.
Stack
(
buf
,
false
)]
buf
=
buf
[
:
runtime
.
Stack
(
buf
,
false
)]
err
=
fmt
.
Sprintf
(
"%v
\n
%s"
,
err
,
buf
)
err
=
fmt
.
Sprintf
(
"%v
\n
%s"
,
err
,
buf
)
}
}
err
Ch
<-
err
result
Ch
<-
err
}()
}()
t
.
handler
.
ServeHTTP
(
tw
,
r
)
t
.
handler
.
ServeHTTP
(
tw
,
r
)
}()
}()
select
{
select
{
case
err
:=
<-
errCh
:
case
err
:=
<-
resultCh
:
// panic if error occurs; stop otherwise
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
...
...
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