Skip to content

Commit

Permalink
Introduces the panicMu mutex
Browse files Browse the repository at this point in the history
Ensures that panic failpoints and serving of the http requests won't
be able to be executed at the same time.
  • Loading branch information
henrybear327 committed May 3, 2024
1 parent e51e11e commit 99477da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 5 additions & 8 deletions runtime/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ func serve(host string) error {
}

func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// This prevents all failpoints from being triggered. It ensures
// the server(runtime) doesn't panic due to any failpoints during
// processing the HTTP request.
// It may be inefficient, but correctness is more important than
// efficiency. Usually users will not enable too many failpoints
// at a time, so it (the efficiency) isn't a problem.
failpointsMu.Lock()
defer failpointsMu.Unlock()
// Ensures the server(runtime) doesn't panic due to the execution of
// panic failpoints during processing of the HTTP request
panicMu.Lock()
defer panicMu.Unlock()

// flush before unlocking so a panic failpoint won't
// take down the http server before it sends the response
defer flush(w)
Expand Down
5 changes: 4 additions & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ var (

failpoints map[string]*Failpoint
failpointsMu sync.RWMutex
envTerms map[string]string

envTerms map[string]string

panicMu sync.Mutex
)

func init() {
Expand Down
3 changes: 3 additions & 0 deletions runtime/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ func actSleep(t *term) interface{} {
}

func actPanic(t *term) interface{} {
panicMu.Lock()
defer panicMu.Unlock()

if t.val != nil {
panic(fmt.Sprintf("failpoint panic: %v", t.val))
}
Expand Down

0 comments on commit 99477da

Please sign in to comment.