air-verse / air

☁️ Live reload for Go apps
GNU General Public License v3.0
17.63k stars 802 forks source link

fix: Prevent duplicate processes when multiple buildRuns run simultaneously #457

Closed toga4 closed 12 months ago

toga4 commented 1 year ago

Hi maintainers, this PR fixes the behavior that air duplicates processses when multiple buildRuns run simultaneously. Closes #404, Closes #421, Closes #426.

To reproduce issue:

main.go

package main

import (
    "log"
    "net/http"
    "os"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World!"))
    })

    log.Print(os.Getpid(), " Starting server...")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(os.Getpid(), err)
    }
}

Run air -d and save a file multiple times in a short period of time, then air executes multiple processes. air

This behavior appears from v1.41.0, and seems to be brought by the following pull request.

I looked into what was making the difference, and it seems that the following code that was in place before the change was important. https://github.com/cosmtrek/air/blob/d0a697adcf852559ab6fff59aa164b18f62571a0/runner/engine.go#L486-L490

This PR revive that code, and makes

toga4 commented 1 year ago

Confirmed this change works as expected (on M1 Mac). image

toga4 commented 1 year ago

Hi, @cosmtrek @xiantang Is there any chance you could review this PR? I'd appreciate your feedback.

suntong commented 1 year ago

Hope the PR can be merged soon @cosmtrek @xiantang. thx!

xiantang commented 12 months ago

I know the root cause now, it's because when you save your code in a short period of time, air will start duplicate processes, but doesn't have any lock for it. thank you good catch bug