Skip to content
Myr

Avoid deadlocks by adding a 2 second timeout to GET / endpoint

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Because fzf processes HTTP GET requests in the main event loop, accessing the endpoint from within execute/transform actions would result in a deadlock and hang fzf indefinitely. This commit sets a 2 second timeout to avoid the deadlock.

authored by Junegunn Choi · committed by Junegunn Choi · parent 687c274 · Browse files · 2 files+18 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−6 removed

  • other:+18 added−6 removed

2 files+18 added−6 removed

modified:man/man1/fzf.1+5 added-3 removed
829 unmodified lines
830e.g.831 \fB# Start HTTP server on port 6266832 fzf --listen 6266833834 # Get program state in JSON format (experimental)835 curl localhost:6266836834 # Send action to the server835 curl -XPOST localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'836837 # Get program state in JSON format (experimental)838 # * Make sure NOT to access this endpoint from execute/transform actions839 # as it will result in a timeout840 curl localhost:6266841842 # Start HTTP server on port 6266 with remote connections allowed843 # * Listening on non-localhost address requires using an API key844 export FZF_API_KEY="$(head -c 32 /dev/urandom | base64)"845 fzf --listen 0.0.0.0:6266
620 unmodified lines
modified:src/server.go+13 added-3 removed
28 unmodified lines
29 crlf = "\r\n"30 httpOk = "HTTP/1.1 200 OK" + crlf31 httpBadRequest = "HTTP/1.1 400 Bad Request" + crlf32 httpUnauthorized = "HTTP/1.1 401 Unauthorized" + crlf33 httpUnavailable = "HTTP/1.1 503 Service Unavailable" + crlf34 httpReadTimeout = 10 * time.Second35 jsonContentType = "Content-Type: application/json" + crlf36 maxContentLength = 1024 * 102437)3839type httpServer struct {
102 unmodified lines
142 bad := func(message string) string {143 return answer(httpBadRequest, message)144 }145 good := func(message string) string {144 return answer(httpOk+"Content-Type: application/json"+crlf, message)146 return answer(httpOk+jsonContentType, message)147 }148 conn.SetReadDeadline(time.Now().Add(httpReadTimeout))149 scanner := bufio.NewScanner(conn)150 scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) {
15 unmodified lines
166 case 0:167 getMatch := getRegex.FindStringSubmatch(text)168 if len(getMatch) > 0 {169 server.actionChannel <- []*action{{t: actResponse, a: getMatch[1]}}168 response := <-server.responseChannel169 return good(response)170 select {171 case response := <-server.responseChannel:172 return good(response)173 case <-time.After(2 * time.Second):174 go func() {175 // Drain the channel176 <-server.responseChannel177 }()178 return answer(httpUnavailable+jsonContentType, `{"error":"timeout"}`)179 }180 } else if !strings.HasPrefix(text, "POST / HTTP") {181 return bad("invalid request method")182 }183 section++
71 unmodified lines